Search code examples
iosswiftcameraqr-code

How to scan only certain areas through the camera with Swift5?


I'd like to scan QRcode through the camera. There is no problem scanning QRcode,

but I want to scan only certain areas. How can I do this?

I am currently aware of the QR code anywhere in the entire camera area.

import Foundation
import UIKit
import AVFoundation


class ScannerViewController : UIViewController, AVCaptureMetadataOutputObjectsDelegate {

    @IBOutlet weak var qrcodeView: UIView!
    @IBOutlet weak var mainText: UITextView!
    @IBOutlet weak var headerBar: UINavigationBar!

    var captureSession: AVCaptureSession!
    var previewLayer: AVCaptureVideoPreviewLayer!

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.black
        self.qrcodeView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        captureSession = AVCaptureSession()

        guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else { return }
        let videoInput: AVCaptureDeviceInput

        do {
            videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
        } catch {
            return
        }

        if (captureSession.canAddInput(videoInput)) {
            captureSession.addInput(videoInput)
        } else {
            failed()
            return
        }

        let metadataOutput = AVCaptureMetadataOutput()

        if (captureSession.canAddOutput(metadataOutput)) {
            captureSession.addOutput(metadataOutput)

            metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
            metadataOutput.metadataObjectTypes = [.qr]
        } else {
            failed()
            return
        }

        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        previewLayer.frame = view.layer.bounds
        previewLayer.videoGravity = .resizeAspectFill
        view.layer.insertSublayer(previewLayer, at: 0)
        captureSession.startRunning()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if (captureSession?.isRunning == false) {
            captureSession.startRunning()
        }
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        if (captureSession?.isRunning == true) {
            captureSession.stopRunning()
        }
    }

    func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
//        let scanRect = CGRect(x: 0, y: 0, width: 200, height: 200)
//        let rectOfInterest = layer.metadataOutputRectConverted(fromLayerRect: scanRect)
//        metadataObjects.rectOfInterest = rectOfInterest

        captureSession.stopRunning()

        if let metadataObject = metadataObjects.first {
            guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
            guard let stringValue = readableObject.stringValue else { return }
            AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
            found(code: stringValue)
        } else {
            print("not support")
        }
    }


    func found(code: String) {
        print(code)
        self.dismiss(animated: true, completion: nil)
    }


    func failed() {
        captureSession = nil
    } 


}

enter image description here

Like the picture above, I would like to scan only within the square area.

I desperately need this.

Thanks in advance.


Solution

  • You can use rectOfInterest property to achieve this

    add following code after captureSession.startRunning()

    First you need to convert using rect using

        let rectOfInterest = videoPreviewLayer?.metadataOutputRectConverted(fromLayerRect: self.viewAreaOfScan.frame) //  videoPreviewLayer is AVCaptureVideoPreviewLayer 
    

    after that you can assign it to rectOfInterest of metadataOutput

        metadataOutput.rectOfInterest = rectOfInterest ?? CGRect(x: 0, y: 0, width: 1, height: 1)