我目前正在使用AVCaptureSession
和AVCaptureMetadataOutput
。
它完美无缺,但我只是想知道如何指示仅在AVCaptureVideoPreviewLayer
的特定区域扫描和分析元数据对象?
答案 0 :(得分:13)
以下是我所拥有的可帮助您走上正确轨道的项目代码示例
// 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];
答案 1 :(得分:4)
在iOS 9.3.2中,我有" CGAffineTransformInvert:奇异矩阵"调用metadataoutputRectOfInterestForRect
时出错。我能够在startRunning
的{{1}}方法之后立即调用它:
AVCaptureSession