扫描成功后AVFoundation停止

时间:2016-11-07 08:11:16

标签: ios swift xcode

我正在为iOS 10构建一个带有Swift 3(Xcode 8.1)的QR码扫描器应用程序,我正在使用AVFoundation Framework。有没有人知道如何在扫描成功后停止扫描并在点击屏幕之后重新激活扫描仪等等?

编辑: 这是我的代码片段(没有返回值的代码):

import UIKit
import AVFoundation

@available(iOS 10.0, *)
class QRCodeViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var captureSession:AVCaptureSession?
var videoPreviewLayer:AVCaptureVideoPreviewLayer?
var qrCodeFrameView:UIView?

let supportedBarCodes = [AVMetadataObjectTypeQRCode, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeUPCECode, AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeAztecCode]

override func viewDidLoad() {
    super.viewDidLoad()

let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

    do {
        let input = try AVCaptureDeviceInput(device: captureDevice)

        captureSession = AVCaptureSession()
        captureSession?.addInput(input)

        let captureMetadataOutput = AVCaptureMetadataOutput()
        captureSession?.addOutput(captureMetadataOutput)

        captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)

        captureMetadataOutput.metadataObjectTypes = supportedBarCodes

        videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
        videoPreviewLayer?.frame = view.layer.bounds
        view.layer.addSublayer(videoPreviewLayer!)

        captureSession?.startRunning()


        qrCodeFrameView = UIView()

        if case (qrCodeFrameView?.isHidden)! = true       {
            qrCodeFrameView?.layer.borderColor = UIColor.green.cgColor
            qrCodeFrameView?.layer.borderWidth = 2
            view.addSubview(qrCodeFrameView!)
            view.bringSubview(toFront: qrCodeFrameView!)
        }

    } catch {
        print(error)
        return
    }

}

override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.portrait
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

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

    if metadataObjects == nil || metadataObjects.count == 0 {
        qrCodeFrameView?.frame = CGRect.zero
        return
    }




    let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject


    if supportedBarCodes.contains(metadataObj.type) {

        let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
        qrCodeFrameView?.frame = barCodeObject!.bounds
  print(metadataObj.stringValue)

}

1 个答案:

答案 0 :(得分:3)

在委托方法

中尝试此操作
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {

if metadataObjects == nil || metadataObjects.count == 0 {
    qrCodeFrameView?.frame = CGRect.zero
    return
}

let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject


if supportedBarCodes.contains(metadataObj.type) {

    let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
    qrCodeFrameView?.frame = barCodeObject!.bounds
    print(metadataObj.stringValue)

    // Stop capture session 
    videoPreviewLayer?.isHidden = true
    qrCodeFrameView?.isHidden = true
    self.captureSession?.stopRunning()
}