首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法检测QR码

无法检测QR码
EN

Stack Overflow用户
提问于 2017-04-13 08:02:25
回答 1查看 102关注 0票数 1

我试图制作一个应用程序来扫描ios 10和Swift 3中的QR代码。然而,我的QRScannerController无法检测到QR代码,而是显示相机视图。

我不明白密码有什么问题。以下是控制器的实现:

代码语言:javascript
复制
import UIKit
import AVFoundation

class QRScannerController: UIViewController, AVCaptureMetadataOutputObjectsDelegate  {

    @IBOutlet var messageLabel:UILabel!
    @IBOutlet var topbar: UIView!
    //TESTING
    var captureSession: AVCaptureSession?
    var videoPreviewLayer: AVCaptureVideoPreviewLayer?
    var qrCodeFrameView: UIView?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video as the media type parameter
        let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

        do {

            view.bringSubview(toFront: messageLabel)
            view.bringSubview(toFront: topbar)

            // Get an instance of the AVCaptureDeviceInput class using the previous deivce object
            let input = try AVCaptureDeviceInput(device: captureDevice)

            // Initialize the captureSession object
            captureSession = AVCaptureSession()

            // Set the input devcie on the capture session
            captureSession?.addInput(input)

            // Initialize a AVCaptureMetadataOutput object and set it as the input device 
            let captureMetadataOutput = AVCaptureMetadataOutput()
            captureSession?.addOutput(captureMetadataOutput)

            // Set delegate and use the default dispatch queue to execute the call back
            captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
            captureMetadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode]

            //Initialise the video preview layer and add it as a sublayer to the viewPreview view's layer
            videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
            videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
            videoPreviewLayer?.frame = view.layer.bounds
            view.layer.addSublayer(videoPreviewLayer!)

            //start video capture
            captureSession?.startRunning()

            //Initialize QR Code Frame to highlight the QR code
            qrCodeFrameView = UIView()

            if let qrCodeFrameView = qrCodeFrameView {
                qrCodeFrameView.layer.borderColor = UIColor.green.cgColor
                qrCodeFrameView.layer.borderWidth = 2
                view.addSubview(qrCodeFrameView)
                view.bringSubview(toFront: qrCodeFrameView)
            }

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

                // Check if the metadataObjects array is not nil and it contains at least one object.
                if metadataObjects == nil || metadataObjects.count == 0 {
                    qrCodeFrameView?.frame = CGRect.zero
                    messageLabel.text = "No QR code is detected"
                    return
                }

                // Get the metadata object.
                let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject

                if metadataObj.type == AVMetadataObjectTypeQRCode {
                    // If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
                    let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
                    qrCodeFrameView?.frame = barCodeObject!.bounds

                    if metadataObj.stringValue != nil {
                        messageLabel.text = metadataObj.stringValue
                    }
                }
            }

        } catch {
            //If any error occurs, simply print it out
            print(error)
            return
        }


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-14 05:48:01

您的消息标签位于视图控制器视图下。只需将消息标签放在最前面的视图末尾,load就会有所帮助。

我创建了一个示例项目,它在iPhone上运行得很好。请看一下这里

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43386697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档