扫描QR码并放置SCNNode

时间:2019-03-02 19:18:15

标签: swift qr-code arkit

我正在尝试扫描QR码,然后将结果显示在UIWebView中。我不能执行两个过程(如下所示);映射和QR码。

我该怎么做

import UIKit
import SceneKit
import ARKit
import AVFoundation
import SafariServices

class ViewController: UIViewController, ARSCNViewDelegate, AVCaptureMetadataOutputObjectsDelegate {

    @IBOutlet weak var backview: UIView!

    @IBOutlet weak var sceneView: ARSCNView!

    var planeGeometry:SCNPlane!
    var anchors = [ARAnchor]()
    var sceneLight:SCNLight!

    var video = AVCaptureVideoPreviewLayer()

    override func viewDidLoad() {
        super.viewDidLoad()

        let sessiont = AVCaptureSession()
        let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)

        do {
            let input = try AVCaptureDeviceInput(device: captureDevice!)
            sessiont.addInput(input)
        } catch {
            print("error")
        }

        let output = AVCaptureMetadataOutput()
        sessiont.addOutput(output)

        output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
        output.metadataObjectTypes = [AVMetadataObject.ObjectType.qr]
        video = AVCaptureVideoPreviewLayer(session: sessiont)
        video.frame = view.layer.frame
        backview.layer.addSublayer(video)
        backview.isHidden = true

        sessiont.startRunning() //HERE!!!

    }

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)

        let configuration = ARWorldTrackingConfiguration()
        configuration.planeDetection = .horizontal
        configuration.isLightEstimationEnabled = true


        // Run the view's session
       sceneView.session.run(configuration) //HERE!!
    }

    func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
        if metadataObjects != nil && metadataObjects.count != 0 {
            if let object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject {
                if object.type == AVMetadataObject.ObjectType.qr {
                    //Pass to AR
                }
            }
        }
    }
}

我没有包括AR创建代码,但我认为它不是必需的。我需要扫描QR码,因此它们可以同时运行。我尝试将QR代码嵌入到其他UIView中,但无济于事。

非常感谢您的帮助!

Niall

0 个答案:

没有答案