Swift Qrcode Scanner With Code Examples

  • Updated
  • Posted in Programming
  • 5 mins read


Swift Qrcode Scanner With Code Examples

With this text, we’ll have a look at some examples of Swift Qrcode Scanner issues in programming.

import AVFoundation
import UIKit

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

    override func viewDidLoad() {
        tremendous.viewDidLoad()

        view.backgroundColor = UIColor.black
        captureSession = AVCaptureSession()

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

        do {
            videoInput = attempt AVCaptureDeviceEnter(gadget: videoCaptureDevice)
        } catch {
            return
        }

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

        let metadataOutput = AVCaptureMetadataOutput()

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

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

        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        previewLayer.body = view.layer.bounds
        previewLayer.videoGravity = .resizeAspectFill
        view.layer.addSublayer(previewLayer)

        captureSession.startRunning()
    }

    func failed() {
        let ac = UIAlertController(title: "Scanning not supported", message: "Your gadget doesn't help scanning a code from an merchandise. Please use a tool with a digital camera.", preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", model: .default))
        current(ac, animated: true)
        captureSession = nil
    }

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

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

    override func viewWillDisappear(_ animated: Bool) {
        tremendous.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))
            discovered(code: stringValue)
        }

        dismiss(animated: true)
    }

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

    override var prefersStatusBarHidden: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
}

We have offered a wealth of illustrative examples to point out how the Swift Qrcode Scanner drawback could be solved, and we have now additionally defined how to take action.

How do I scan a QR code with Swift?

Set the important thing to Privacy – Camera Usage Description, and worth to We have to entry your digital camera for scanning QR code. Once you end the modifying, deploy the app and run it on an actual gadget once more. Tapping the scan button ought to carry up the built-in digital camera and begin capturing video.02-Oct-2018

How do I create a QR code in Swift?

QR codes are generated in Swift utilizing the Core Image framework with CIFilters.To generate the CIImage of your QR code:

  • Get the String you need to encode.
  • Get an NSData object (ascii-encoded) from that String.
  • Initialize a CIFilter with the QR code preset.
  • Add your information to the filter.
  • Generate a CIImage from the filter.

How do I scan a QR code with a route?

Scan it to check.How to find an tackle with a QR Code and google maps?

  • Go to https://maps.google.com/
  • Enter the vacation spot. Make certain the map is displayed as you prefer to it to seem when anyone scans the QR code.
  • Click on the chain button.
  • Go to the QR code generator and enter your URL.

Are QR mills free?

All generated QR Codes are 100% free and can be utilized for no matter you need.

Can you simply scan a QR code with out an app?

Yes. Just like iPhones, Android 9 (Android Pie) and Android 10 have an in-built QR Code reader. Even the Android 8 or Oreo doesn’t want an app to scan QR Codes.27-May-2022

How do I scan a QR code with IOS?

Scan a QR code along with your iPhone, iPad, or iPod contact

  • Open the Camera app from the Home Screen, Control Center, or Lock Screen.
  • Select the rear going through digital camera. Hold your gadget in order that the QR code seems within the viewfinder within the Camera app.
  • Tap the notification to open the hyperlink related to the QR code.

How do I make an auto QR code?

How to Create a QR Code on Any Device

  • Step 1: Head to qr-code-generator.com.
  • Step 2: Enter your URL.
  • Step 3: Customize your QR code’s body, form, and colour.
  • Step 4: Click “Download” and save the file to your pc.
  • Step 5: Add the QR code to print collateral.

Can I construct my very own QR code?

To create a QR code, use the QR Code Generator web site. You can even create QR codes to any web site utilizing the Chrome cellular app on iOS or Android. You can ship the generated QR code to others through textual content, e-mail, messaging app, and extra.23-Sept-2022

Can I design my very own QR code?

Yes, you’ll be able to create a QR Code the common manner and make it round via the templates out there on the QR Code platform. Essentially the sq. QR Code has some additional design bits throughout it to make it a round QR Code.22-Sept-2022

Can a QR code direct you to a web site?

QR (Quick Response) codes are an important device to drive site visitors from print media, comparable to fliers, newspaper advertisements, and billboards, to an internet webpage like a weblog or social media profile. When somebody makes use of a smartphone to scan your QR code, they’re introduced on to any webpage you specify: Create a typical QR Code.05-May-2021

Leave a Reply