ios – AVCaptureSession条形码扫描

前端之家收集整理的这篇文章主要介绍了ios – AVCaptureSession条形码扫描前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在使用AVCaptureSession和AVCaptureMetadataOutput.

它工作得很好,但我只是想知道如何指示仅在AVCaptureVideoPreviewLayer的特定区域扫描和分析元数据对象?

解决方法

以下是我所拥有的项目代码示例,可以帮助您走上正确的轨道
// where 'self.session' is prevIoUsly setup  AVCaptureSession

    // setup Metadata capture
    AVCaptureMetadataOutput *MetadataOutput = [[AVCaptureMetadataOutput alloc] init];
    [self.session addOutput:MetadataOutput];
    [MetadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [MetadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeEAN13Code]];

    // setup preview layer
    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
    previewLayer.frame = self.previewView.bounds;
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

    // we only want the visible area of the previewLayer to accept
    // barcode input (ignore the rest)
    // we need to convert rects coordinate system
    CGRect visibleMetadataOutputRect = [previewLayer MetadataOutputRectOfInterestForRect:previewLayer.bounds];
    MetadataOutput.rectOfInterest = visibleMetadataOutputRect;

    // add the previewLayer as a sublayer of the displaying UIView
    [self.previewView.layer addSublayer:previewLayer];
原文链接:https://www.f2er.com/iOS/330911.html

猜你在找的iOS相关文章