2014-05-11 8 views
9

Skanuję kod QR & Skanuję kod kreskowy za pomocą AVCaptureMetadataOutput. Kiedy kamera jest już skupiona na kodzie kreskowym, wywoływana jest delegata didOutputMetadataObjects i mogę uzyskać ciąg metadanych kodów kreskowych. Ale zastanawiam się, jak uzyskać zeskanowany obraz (obraz kodu kreskowego) od delegata didOutputMetadataObjects.UIImage od delegata AVCaptureMetadataOutput (didOutputMetadataObjects)

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

góry dzięki ..

+2

Czy otrzymałeś rozwiązanie w tej sprawie? Też szukam tego samego. – Mrug

Odpowiedz

0

To będzie Ci UIImage, które można zrobić, jak wybrać z.

- (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; 
    } 
}