MK_ MK_ - 1 month ago 16
Swift Question

AVFoundation stop after successful scan

I am building a QR Code Scanner App with Swift 3(Xcode 8.1) for iOS 10 and I am using the AVFoundation Framework. Does anybody know how to stop scanning after a successful scan and reactivate the Scanner after something like tapping the screen or whatever?

EDIT:
Here is a snippet of my code (no code of what happens with the return value):

import UIKit
import AVFoundation

@available(iOS 10.0, *)
class QRCodeViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var captureSession:AVCaptureSession?
var videoPreviewLayer:AVCaptureVideoPreviewLayer?
var qrCodeFrameView:UIView?

let supportedBarCodes = [AVMetadataObjectTypeQRCode, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeUPCECode, AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeAztecCode]

override func viewDidLoad() {
super.viewDidLoad()

let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

do {
let input = try AVCaptureDeviceInput(device: captureDevice)

captureSession = AVCaptureSession()
captureSession?.addInput(input)

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

captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)

captureMetadataOutput.metadataObjectTypes = supportedBarCodes

videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
videoPreviewLayer?.frame = view.layer.bounds
view.layer.addSublayer(videoPreviewLayer!)

captureSession?.startRunning()


qrCodeFrameView = UIView()

if case (qrCodeFrameView?.isHidden)! = true {
qrCodeFrameView?.layer.borderColor = UIColor.green.cgColor
qrCodeFrameView?.layer.borderWidth = 2
view.addSubview(qrCodeFrameView!)
view.bringSubview(toFront: qrCodeFrameView!)
}

} catch {
print(error)
return
}

}

override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

}

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

if metadataObjects == nil || metadataObjects.count == 0 {
qrCodeFrameView?.frame = CGRect.zero
return
}




let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject


if supportedBarCodes.contains(metadataObj.type) {

let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
qrCodeFrameView?.frame = barCodeObject!.bounds
print(metadataObj.stringValue)

}

Answer

Try this in your delegate method

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

if metadataObjects == nil || metadataObjects.count == 0 {
    qrCodeFrameView?.frame = CGRect.zero
    return
}

let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject


if supportedBarCodes.contains(metadataObj.type) {

    let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
    qrCodeFrameView?.frame = barCodeObject!.bounds
    print(metadataObj.stringValue)

    // Stop capture session 
    videoPreviewLayer?.isHidden = true
    qrCodeFrameView?.isHidden = true
    self.captureSession?.stopRunning()
}