20

I have an iOS App which scans a barcode.

Now I wanted to define a specific scan area. For this purpose I am using the rectOfInterest property and metadataOutputRectOfInterest method.

The problems I face

When I use just this code below nothing will be scanned, if I remove barcodeAreaView scanning works fine all over the display

class BarcodeReaderViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

    // Camera view
    var cameraView: AVCaptureVideoPreviewLayer?
    // AV capture session and dispatch queue
    let captureSession = AVCaptureSession()
    let sessionQueue = DispatchQueue(label: AVCaptureSession.self.description(), attributes: [], target: nil)
    var barcodeAreaView: UIView?

    let supportedCodeTypes = [
        AVMetadataObjectTypeEAN8Code,
        AVMetadataObjectTypeEAN13Code,
        AVMetadataObjectTypeUPCECode,
        AVMetadataObjectTypeCode39Code,
        AVMetadataObjectTypeCode93Code,
        AVMetadataObjectTypeCode128Code,
        AVMetadataObjectTypeCode39Mod43Code,
        AVMetadataObjectTypeInterleaved2of5Code
    ]
    let metadataOutput = AVCaptureMetadataOutput()
    var barcodeArea:CGRect!

    override func viewDidLoad() {
        super.viewDidLoad()

        let width = 250
        let height = 100

        print(self.view.frame.size.width)

        let xPos = (CGFloat(self.view.frame.size.width) / CGFloat(2)) - (CGFloat(width) / CGFloat(2))
        let yPos = (CGFloat(self.view.frame.size.height) / CGFloat(2)) - (CGFloat(height) / CGFloat(2))
        barcodeArea = CGRect(x: Int(xPos), y: Int(yPos), width: width, height: height)

        barcodeAreaView = UIView()
        barcodeAreaView?.layer.borderColor = UIColor.red.cgColor
        barcodeAreaView?.layer.borderWidth = 1
        barcodeAreaView?.frame = barcodeArea
        view.addSubview(barcodeAreaView!)

        captureSession.beginConfiguration()

        let videoDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

        if videoDevice != nil {

            let videoDeviceInput = try? AVCaptureDeviceInput(device: videoDevice)

            if videoDeviceInput != nil {

                if captureSession.canAddInput(videoDeviceInput) {

                    captureSession.addInput(videoDeviceInput)
                }
            }

            if captureSession.canAddOutput(metadataOutput) {

                captureSession.addOutput(metadataOutput)

                metadataOutput.metadataObjectTypes = supportedCodeTypes
                metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)

                cameraView = AVCaptureVideoPreviewLayer(session: captureSession)
                cameraView?.videoGravity = AVLayerVideoGravityResizeAspectFill
                cameraView?.frame = view.layer.bounds

                metadataOutput.rectOfInterest = cameraView!.metadataOutputRectOfInterest(for: barcodeArea)
                view.layer.addSublayer(cameraView!)
            }
        }

        captureSession.commitConfiguration()
        view.bringSubview(toFront: barcodeAreaView!)
    }

    override func viewWillAppear(_ animated: Bool) {

        super.viewWillAppear(animated)

        // Start AV capture session
        sessionQueue.async {
            self.captureSession.startRunning()
        }
    }

    override func viewWillDisappear(_ animated: Bool) {

        super.viewWillDisappear(animated)

        // Stop AV capture session
        sessionQueue.async {
            self.captureSession.stopRunning()
        }
    }

    func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {

        if let metadataObject = metadataObjects.first {

            let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject            
            let message = readableObject.stringValue
            print(message)
        }
    }
}

I already took a look on this question

How do I use the metadataOutputRectOfInterestForRect method and rectOfInterest property to scan a specific area? (QR Code)

The problem I face here, the scan is a bit laggy and my navigationController is not working (because the listener awaits a scan)

UPDATE:

solved the issue, as suggested in this answer

https://stackoverflow.com/a/37603743/2319900

I added the following line right after my self.captureSession.startRunning()

metadataOutput.rectOfInterest = cameraView!.metadataOutputRectOfInterest(for: barcodeArea)
Community
  • 1
  • 1
beks6
  • 222
  • 3
  • 12
  • Why this needs to be written after `self.captureSession.startRunning()`? Same code will not work if we write it before this line. – RJ168 May 14 '19 at 12:04

0 Answers0