Try MAXST AR Fusion Tracker Now ✨

QR / Barcode Scanner

QR / Barcode Scanner recognizes QR / Barcode.

Create Instants
Start / Stop Scanner
Use Scanning Information

Create Instants

>QR_BarcodeViewController.swift

var cameraDevice:MasCameraDevice = MasCameraDevice()
var trackingManager:MasTrackerManager = MasTrackerManager()

Start / Stop Scanner

To start / stop Scanner, refer to the following code.

>QR_BarcodeViewController.swift

@objc func resumeAR()
{
    ...
    trackingManager.start(.TRACKER_TYPE_CODE_SCANNER)
}

@objc func pauseAR()
{
    trackingManager.stopTracker()
    ...
}

Use Scanning Information

When you illuminate QR / Barcode, the code name is displayed on the screen. Refer to the following code to use the scanning information.

You must be used Convert Matrix for Metal.('metalMatrixConverter')

>QR_BarcodeViewController.swift

func draw(in view: MTKView) {
    ...
    
    let backgroundImage:MasTrackedImage = trackingState.getImage()
    var backgroundProjectionMatrix:matrix_float4x4 = cameraDevice.getBackgroundPlaneProjectionMatrix()
    
    let metalMatrixConverter:matrix_float4x4 = getScaleMatrix(1.0, y: -1.0, z: 1.0)
    backgroundProjectionMatrix = backgroundProjectionMatrix * metalMatrixConverter
    
    if let cameraQuad = backgroundCameraQuad {
        cameraQuad.setProjectionMatrix(projectionMatrix: backgroundProjectionMatrix)
        cameraQuad.draw(commandEncoder: commandEncoder, image: backgroundImage)
    }
    
    let codeScanResult:String = trackingState.getCodeScanResult()
    
    if codeScanResult != "" {
        let objectData:Data = codeScanResult.data(using: String.Encoding.utf8)!
        let json = try! JSONSerialization.jsonObject(with: objectData, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String: String]
        
        self.formatLabel.text = json["Format"]
        self.codeLabel.text = json["Value"]
        
        trackingManager.stopTracker()
        self.scanButton.alpha = 1.0
        self.scanButton.isEnabled = true
        self.scanButton.setTitle("Start Scan", for: .normal)
    }
    ...
}