Building a Barcode and QR Code Reader in Swift 3.

In this tutorial we are study about to Scan barcode and read the barcode value. 




Step : 1 Add the AVFoundation framework in your swift file as below.
import AVFoundation
Step : 2  Add the delegate AVCaptureMetadataOutputObjectsDelegate delegate which is use to get the metadata of the barcode. Here we can read the barcode value and use it.
class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
Step : 3 Take one view for Scan the barcode. Button btnStartStop  for start and stop the barcode scan. and label for display value of barcode.
@IBOutlet weak var viewPreview: UIView!
@IBOutlet weak var lblString: UILabel!
@IBOutlet weak var btnStartStop: UIButton!
Step : 4 Create an object of AVCaptureSession and  AVCaptureVideoPreviewLayer. Also take one variable isReading to check barcode scanning is start or stop.
var captureSession: AVCaptureSession?
var videoPreviewLayer: AVCaptureVideoPreviewLayer!
var isReading: Bool = false
Step : 5 In ViewDidLoad method. Set the property of components(views) and initialize the variable. See the below code.
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    viewPreview.layer.cornerRadius = 5;
    btnStartStop.layer.cornerRadius = 5;
    captureSession = nil;
    lblString.text = "Barcode discriptio...";
}
Step : 6 Start Scan 
func startReading() -> Bool {
    let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
    do {
        let input = try AVCaptureDeviceInput(device: captureDevice)
        captureSession = AVCaptureSession()
        captureSession?.addInput(input)
        // Do the rest of your work...
    } catch let error as NSError {
        // Handle any errors
        print(error)
        return false
    }

    videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
    videoPreviewLayer.frame = viewPreview.layer.bounds
    viewPreview.layer.addSublayer(videoPreviewLayer)

    /* Check for metadata */
    let captureMetadataOutput = AVCaptureMetadataOutput()
    captureSession?.addOutput(captureMetadataOutput)
    captureMetadataOutput.metadataObjectTypes = captureMetadataOutput.availableMetadataObjectTypes
    print(captureMetadataOutput.availableMetadataObjectTypes)
    captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
    captureSession?.startRunning()

    return true
}
Step : 7 Stop Scan 
func stopReading() {
    captureSession?.stopRunning()
    captureSession = nil
    videoPreviewLayer.removeFromSuperlayer()
}
Step : 8 After the start scan successfully , The AVCaptureMetadataOutputObjectsDelegate method gives the metadata. So we can read the value of barcode and use the barcode value.
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
    for data in metadataObjects {
        let metaData = data as! AVMetadataObject
        print(metaData.description)
        let transformed = videoPreviewLayer?.transformedMetadataObject(for: metaData) as? AVMetadataMachineReadableCodeObject
        if let unwraped = transformed {
            print(unwraped.stringValue)
            lblString.text = unwraped.stringValue
            btnStartStop.setTitle("Start", for: .normal)
            self.performSelector(onMainThread: #selector(stopReading), with: nil, waitUntilDone: false)
            isReading = false;
        }
    }
}
Step : 9  Start/Stop button will start the barcode and stop to read barcode.  when stop then object set to be nil.
@IBAction func startStopClick(_ sender: UIButton) {
    if !isReading {
        if (self.startReading()) {
        btnStartStop.setTitle("Stop", for: .normal)
        lblString.text = "Scanning for QR Code..."
        }
    }
    else {
        stopReading()
        btnStartStop.setTitle("Start", for: .normal)
    }
    isReading = !isReading
}
Step : 10 Adding the camera permission in info.plist file. See the below image. Also refer the link How To Add Permission in Info.plist file.



Step : 11 You can create free barcode here. Open the link : barcode




OUTPUT : 




Demo : Download

Thanks.


Building a Barcode and QR Code Reader in Swift 3. Building a Barcode and QR Code Reader in Swift 3. Reviewed by Kirit Modi on 22:21:00 Rating: 5

No comments:

Powered by Blogger.