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];

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...