ios - AVFoundation、AVCaptureMetadataOutput 截屏

标签 ios avfoundation avcapturesession avcapture capture-output

我正在使用 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);
}];

引用使用CodeScanViewController

就是这样@Enjoy

关于ios - AVFoundation、AVCaptureMetadataOutput 截屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20782584/

相关文章:

swift - 如何自定义大小 AVcapture

ios - 将 UITableViewCell 恢复到正常编辑模式

ios - DJI SDK 用户界面 : Customizing contentViewController in DULDefaultViewController

ios - 视频预览期间的屏幕截图失败

swift - AVAssetWriter 连续段

iOS - 使用 AVFoundation Framework 从相机模糊预览

ios - 需要向我的 iOS 应用程序添加一个 "Embed Watch Content"构建阶段

ios - PushViewController 详细信息?

ios - `[AVCaptureSession canAddOutput:output]` 间歇性地返回 NO。我能找出原因吗?

iphone - iOS - 尝试了解电影是如何运作的