Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

swift qrcode scanner

import AVFoundation
import UIKit

class ScannerViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
    var captureSession: AVCaptureSession!
    var previewLayer: AVCaptureVideoPreviewLayer!

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.black
        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.addSublayer(previewLayer)

        captureSession.startRunning()
    }

    func failed() {
        let ac = UIAlertController(title: "Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.", preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", style: .default))
        present(ac, animated: true)
        captureSession = nil
    }

    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) {
        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)
        }

        dismiss(animated: true)
    }

    func found(code: String) {
        print(code)
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
}
Comment

PREVIOUS NEXT
Code Example
Swift :: swift collectionview scrolltoitem 
Swift :: How to set back button text in Swift 
Swift :: how to change background color of ui button swift 
Swift :: and in swift1 
Swift :: swift date difference in days 
Swift :: convert dictionary to array swift 
Swift :: make preivew in dark mode swiftui 
Swift :: swift string to dictionary 
Swift :: manifest merger failed in android studio 
Swift :: custom tab bar swift ios 
Swift :: get height of navigation bar swift 
Swift :: how to low case string swift 
Swift :: swift 5 get first character of string 
Swift :: swiftui popover 
Swift :: symfony swiftmailer 
Swift :: make optional protocol swift 
Swift :: how to clear text file swift 
Swift :: swift extension Array of element 
Swift :: can you pass an enum as a parameter to a function swift 
Swift :: swiftui line break text 
Swift :: The Swift pod `qr_code_scanner` depends upon `MTBBarcodeScanner`, which does not define modules 
Swift :: swift combine 2 sets 
Swift :: Log httpurlresponse swift 
Swift :: uitableview total number of rows 
Swift :: Swift for vs while loop 
Swift :: Swift Escape Sequences 
Swift :: Swift Create static type Singleton Object 
Swift :: Assign values to enum variables Swift 
Swift :: Arithmetic Operators in Swift 
Swift :: Swift Logical Operators 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =