2013-12-22 17 views
5

私は現在AVCaptureSessionAVCaptureMetadataOutputで作業しています。AVCaptureSessionバーコードスキャン

これは完全に機能しますが、AVCaptureVideoPreviewLayerの特定の地域でのみメタデータオブジェクトをスキャンして分析する方法を知りたいのですが? metadataoutputRectOfInterestForRectを呼び出すときにエラー:ここ

答えて

13

は、私はそれは私が「特異行列CGAffineTransformInvert」を持っていた正しい軌道に乗ってのiOS 9.3.2で

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

プロジェクトます。https:[previewLayer metadataOutputRectOfInterestForRect::previewLayer.bounds] //github.com/jpwidmer/iOS7-BarcodeScanner –

+1

このために結果は次のとおりです。{{nan、nan}、{nan、nan}} –

+0

previewLayer.frameの境界は、self.previewView.bounds(これは以前にインスタンス化されたUIViewです)に由来します。あなたは、このUIViewがこの時点で境界を持っていることを確認する必要があります(例えば、自動レイアウトがあなたのself.previewViewのサイズを定義する前にこのコードを使用していますか?) –

2

あなたを助けるかもしれない持っているプロジェクトからのコードのサンプルです。私はそれが正しいAVCaptureSessionstartRunning方法の後に、それを呼び出す動作させることができました:

captureSession.startRunning() 
let visibleRect = previewLayer.metadataOutputRectOfInterestForRect(previewLayer.bounds) 
captureMetadataOutput.rectOfInterest = visibleRect 
関連する問題