我目前正在使用 AVCaptureSession
和 AVCaptureMetadataOutput
进行工作。
它的功能非常完美,但我想知道如何指示仅在 AVCaptureVideoPreviewLayer
的特定区域扫描和分析元数据对象?
我目前正在使用 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];
metadataoutputRectOfInterestForRect
时,我遇到了“CGAffineTransformInvert:singular matrix”错误。我成功解决了这个问题,在AVCaptureSession
的startRunning
方法后立即调用它。captureSession.startRunning()
let visibleRect = previewLayer.metadataOutputRectOfInterestForRect(previewLayer.bounds)
captureMetadataOutput.rectOfInterest = visibleRect