【问题标题】:AVCaptureSession barcode scanAVCaptureSession 条码扫描
【发布时间】:2014-01-11 03:55:46
【问题描述】:

我目前正在与AVCaptureSessionAVCaptureMetadataOutput 合作。

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

【问题讨论】:

    标签: ios avfoundation barcode avcapturesession


    【解决方案1】:

    这是我的一个项目的代码示例,它可能会帮助你走上正轨

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

    【讨论】:

    • 对于这个:[previewLayer metadataOutputRectOfInterestForRect:previewLayer.bounds],结果是:{{nan, nan}, {nan, nan}}
    • 注意previewLayer.frame的边界来自self.previewView.bounds(是一些之前实例化的UIView)。此时您应该检查此 UIView 是否有边界(例如,您是否在自动布局定义您的 self.previewView 的大小之前使用此代码??)
    【解决方案2】:

    在 iOS 9.3.2 中,调用 metadataoutputRectOfInterestForRect 时出现“CGAffineTransformInvert:奇异矩阵”错误。我能够在AVCaptureSessionstartRunning 方法之后立即调用它:

    captureSession.startRunning()
    let visibleRect = previewLayer.metadataOutputRectOfInterestForRect(previewLayer.bounds)
    captureMetadataOutput.rectOfInterest = visibleRect
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多