Ios AVCaptureSession条形码扫描

Ios AVCaptureSession条形码扫描,ios,avfoundation,barcode,avcapturesession,Ios,Avfoundation,Barcode,Avcapturesession,我目前正在使用AVCaptureSession和avcapturemataoutput 它工作得很好,但我只想知道如何指示仅在AVCaptureVideoPreviewLayer?的特定区域上扫描和分析元数据对象这是我拥有的一个项目中的代码示例,它可能会帮助您走上正确的道路 // where 'self.session' is previously setup AVCaptureSession // setup metadata capture AVCaptureMe

我目前正在使用
AVCaptureSession
avcapturemataoutput


它工作得很好,但我只想知道如何指示仅在
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 9.3.2中,我在调用MetadataOutputTransformInvert:singular matrix时出现了“CGAffineTransformInvert:singular matrix”错误。我能够在
AVCaptureSession的
startRunning
方法之后立即调用它:

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

对于此:[previewLayer metadataOutputRectOfInterestForRect:previewLayer.bounds],结果是:{nan,nan},{nan,nan}}请注意,previewLayer.frame的边界来自self.previewView.bounds(这是以前实例化的UIView)。您应该检查此UIView在这一点上是否有边界(例如,在自动布局定义self.previewView的大小之前,您是否使用此代码??)