Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVCaptureSession barcode scan

I'm currently working with AVCaptureSession and AVCaptureMetadataOutput.

It works perfectly, but I just want to know how to indicate to scan and analyze metadata objects only on a specific region of the AVCaptureVideoPreviewLayer?

like image 346
Seb Thiebaud Avatar asked Dec 22 '13 22:12

Seb Thiebaud


People also ask

How do I scan a barcode to check authenticity?

Use the camera on your phone to scan barcodes or the QR Code on the packaging or product sticker. The application will display all information that the business has provided, such as: Full product name.

Can I scan barcode online?

With Aspose.Barcode Scanner you can scan barcodes online using your mobile phone's camera. You can scan qr code, scan datamatrix and many more 2D and 1D barcode types. Choose 1D or 2D scanning mode, capture barcode with your camera and let our sophisticated alghorythms to determine symbology itself and recognize.

Can I scan a barcode to find a product?

Every product sold in a market comes with a unique identifier. You can scan these codes using a barcode scanner app and find the associated product on the web. Other than finding a product on the internet, a code scanner has a number of use cases.


2 Answers

Here is a sample of code from a project I have that may help you on the right track

    // 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];
like image 131
So Over It Avatar answered Sep 28 '22 04:09

So Over It


In iOS 9.3.2 I had "CGAffineTransformInvert: singular matrix" error when calling metadataoutputRectOfInterestForRect. I was able to make it work calling it right after startRunning method of AVCaptureSession:

captureSession.startRunning()
let visibleRect = previewLayer.metadataOutputRectOfInterestForRect(previewLayer.bounds)
captureMetadataOutput.rectOfInterest = visibleRect
like image 38
Avt Avatar answered Sep 28 '22 04:09

Avt