我正在使用 AVCaptureMetadataOutput
以使用 iOS QRCode、条形码扫描功能。这很好用,我得到了通过 AVCaptureMetadataOutput
委托(delegate)方法扫描的结果
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
但我不知道如何用我在这个委托(delegate)中的数据捕获扫描的二维码、条形码的图像。
最佳答案
我有 captured image
同时scanning QRCode
像这样:
1) 首先添加AVCaptureStillImageOutput's
的属性
@property (strong, nonatomic) AVCaptureStillImageOutput *stillImageOutput;
2) 在AVCaptureSession
中添加 session 预设初始化之后
[self.session setSessionPreset:AVCaptureSessionPreset640x480];
3)现在添加AVCaptureStillImageOutput's
作为 AVCaptureSession
中的输出
// Prepare an output for snapshotting
self.stillImageOutput = [AVCaptureStillImageOutput new];
[self.session addOutput:self.stillImageOutput];
self.stillImageOutput.outputSettings = @{AVVideoCodecKey: AVVideoCodecJPEG};
4) 添加以下代码以在委托(delegate)方法中捕获扫描图像captureOutput:didOutputMetadataObjects:fromConnection:connection
__block UIImage *scannedImg = nil;
// Take an image of the face and pass to CoreImage for detection
AVCaptureConnection *stillConnection = [self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:stillConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if(error) {
NSLog(@"There was a problem");
return;
}
NSData *jpegData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
scannedImg = [UIImage imageWithData:jpegData];
NSLog(@"scannedImg : %@",scannedImg);
}];
就是这样@Enjoy
关于ios - AVFoundation、AVCaptureMetadataOutput 截屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20782584/