2014-05-11 29 views
9

QR Kodunu tarama & AVCaptureMetadataOutput kullanarak barkod. Daha sonra kamera didOutputMetadataObjects barkoduna odaklandığında delege çağrılır ve barkod meta veri dizesini elde edebilirim. Ancak, taranan görüntüyü (barkod resmi) didOutputMetadataObjects temsilcisinden nasıl alacağımı merak ediyorum. peşinUIImage from AVCaptureMetadataOutput delege (didOutputMetadataObjects)

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{ 
// How to get the scanned image from this delegate ? 
} 

Teşekkür ..

+2

Bu konuda bir çözüm buldunuz mu? Ben de aynı şeyi arıyorum. – Mrug

cevap

0

Bu size olan seçerken yapabileceğiniz UIImage alacak.

- (void)captureOutput:(AVCaptureOutput *)captureOutput 
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
     fromConnection:(AVCaptureConnection *)connection { 
    // You only want this to run after the barcode is captured, so I use a bool value to control entry 
    if (_captured) { 
     _captured = NO; 
     [_session stopRunning]; 
     CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
     CVPixelBufferLockBaseAddress(imageBuffer,0); 
     uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer); 
     size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
     size_t width = CVPixelBufferGetWidth(imageBuffer); 
     size_t height = CVPixelBufferGetHeight(imageBuffer); 

     // Take the image buffer you have and create a CGImageRef 
     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
     CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 
     CGImageRef newImage = CGBitmapContextCreateImage(newContext); 
     baseAddress = nil; 
     // Clean up 
     CGContextRelease(newContext); 
     CGColorSpaceRelease(colorSpace); 
     // Start with a base image to original scale 
     UIImage *imageBase = [UIImage imageWithCGImage:newImage scale:1.0 orientation:UIImageOrientationRight]; 
     // You can resize image if you want 
     UIImage *imageFinal = [UIImage resizeImage:imageBase scaledToSize:CGSizeMake(480.0, 640.0)]; 
     imageBase = nil; 
     CGImageRelease(newImage); 
     CVPixelBufferUnlockBaseAddress(imageBuffer,0); 
     imageBuffer = nil; 
    } 
}