jone2
jone2

Reputation: 191

swift Avcapture session for barcode scanning is not working

I am trying to build a barcode scanner. I adapted some of this tutorial. The video capture session is working but it is not detecting any barcode. I have gone through the code multiple times and still could not find what the problem could be. Here is my code for detecting the barcode

class ScanController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

var captureSession: AVCaptureSession?
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
var qrCodeFrameView: UIView?

let supportedCodeTypes = [AVMetadataObject.ObjectType.upce,
                          AVMetadataObject.ObjectType.code39,

                          AVMetadataObject.ObjectType.qr]

override func viewDidLoad() {
    super.viewDidLoad()

//Get an instance of the AVCaptureDevice class  a device object and provide the video as the media type parameter
    let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)

    do {

        // Get an instance of the AVCaptureDeviceInput class using the previous device object.
        let input = try AVCaptureDeviceInput(device: captureDevice!)

        // Initialize the captureSession object.
        captureSession = AVCaptureSession()

        // Set the input device on the capture session.
        captureSession?.addInput(input)

        let captureMetadataOutput = AVCaptureMetadataOutput()
        captureSession?.addOutput(captureMetadataOutput)

        // Set delegate and use the default dispatch queue to execute the call back
        captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
        captureMetadataOutput.metadataObjectTypes = supportedCodeTypes
        // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
        videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
        videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
        videoPreviewLayer?.frame = view.layer.bounds
        view.layer.addSublayer(videoPreviewLayer!)
        // Start video capture.
        captureSession?.startRunning()
        // Add the message label
        self.view.addSubview(messageLabel)


        //initialize QR Code Frame to highlight the QR Code
        qrCodeFrameView = UIView()
        if let qrCodeFrameView = qrCodeFrameView {

            qrCodeFrameView.layer.borderColor = UIColor.green.cgColor
            qrCodeFrameView.layer.borderWidth = 2
            view.addSubview(qrCodeFrameView)
            view.bringSubview(toFront: qrCodeFrameView)

        }
    } catch {

        // If any error occurs, simply print it out and don't continue any more.
        print("THERE IS A PROBLEM WITH THE CAPTURE SESSION *****************")
        print(error)
        return
    }
}

 }

what am I missing ?

Upvotes: 0

Views: 969

Answers (1)

Osman
Osman

Reputation: 1586

maybe you missing the Delegate Methods? In the Tutorial is the delegate method :

optional func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection)

under the section Decoding the QR Code

Upvotes: 1

Related Questions