2017-04-13 126 views
1

我試圖讓應用程序掃描ios 10和Swift 3中的QR碼。但是,我的QRScannerController無法檢測QR碼,但顯示相機視圖。無法檢測QR碼

我不明白代碼有什麼問題。這裏是控制器的實現:

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. 
    } 

回答

1

您的留言信息標籤視圖控制器視圖去下。只要將消息標籤放在視圖的末尾,確實會有幫助。

我創建了一個示例項目,它在iPhone上正常工作。請看看here

+0

哇,我修好了。謝謝Bluewings! –