我目前正在使用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];