swift4 qr阅读器工作但没有结果

时间:2018-03-03 16:51:49

标签: swift qr-code

swift4 qr-code reader并假设弹出一个警报,但它没有显示弹出警报。 会话开始正常运行,但是当qr代码扫描时,func captureOutput()似乎不会触发。

enter image description here

我的代码:

import UIKit
import AVFoundation

class ScannerViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

    @IBOutlet weak var square: UIImageView!
    //Creating session
    var video = AVCaptureVideoPreviewLayer()
    override func viewDidLoad() {
        super.viewDidLoad()

        //Creating session
        let session = AVCaptureSession()

        //Define capture devcie
        let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)

        do
        {let input = try AVCaptureDeviceInput(device: captureDevice!)
            session.addInput(input)}
        catch
        { print ("ERROR")}
        let output = AVCaptureMetadataOutput()
        session.addOutput(output)
        output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
        output.metadataObjectTypes = [AVMetadataObject.ObjectType.qr]
        video = AVCaptureVideoPreviewLayer(session: session)
        video.frame = view.layer.bounds
        view.layer.addSublayer(video)
        self.view.bringSubview(toFront: square)
        session.startRunning()
    }

    func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {

        if metadataObjects != nil && metadataObjects.count != 0
        {
            if let object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject
            {
                if object.type == AVMetadataObject.ObjectType.qr
                {
                    let alert = UIAlertController(title: "QR Code", message: object.stringValue, preferredStyle: .alert)
                    alert.addAction(UIAlertAction(title: "Retake", style: .default, handler: nil))
                    alert.addAction(UIAlertAction(title: "Copy", style: .default, handler: { (nil) in
                        UIPasteboard.general.string = object.stringValue
                    }))

                    present(alert, animated: true, completion: nil)
                }
            }
        }
    }

}

1 个答案:

答案 0 :(得分:1)

您的扫描代码检查将在以下代理中:

func metadataOutput(_ output:AVCaptureMetadataOutput,didOutput metadataObjects:[AVMetadataObject],来自connection:AVCaptureConnection)

您的代码将是这样的:

import UIKit
import AVFoundation

class ScannerViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

@IBOutlet weak var square: UIImageView!
//Creating session
var video = AVCaptureVideoPreviewLayer()
override func viewDidLoad() {
    super.viewDidLoad()

    //Creating session
    let session = AVCaptureSession()

    //Define capture devcie
    let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)

    do
    {let input = try AVCaptureDeviceInput(device: captureDevice!)
        session.addInput(input)}
    catch
    { print ("ERROR")}
    let output = AVCaptureMetadataOutput()
    session.addOutput(output)
    output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
    output.metadataObjectTypes = [AVMetadataObject.ObjectType.qr]
    video = AVCaptureVideoPreviewLayer(session: session)
    video.frame = view.layer.bounds
    view.layer.addSublayer(video)
    self.view.bringSubview(toFront: square)
    session.startRunning()
}

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
            {
                let alert = UIAlertController(title: "QR Code", message: object.stringValue, preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "Retake", style: .default, handler: nil))
                alert.addAction(UIAlertAction(title: "Copy", style: .default, handler: { (nil) in
                    UIPasteboard.general.string = object.stringValue
                }))

                present(alert, animated: true, completion: nil)
            }
        }
    }
}

}