iOS Swift - 如何正确使用rectOfInterest(条形码扫描仪)

时间:2017-05-02 12:52:13

标签: ios iphone swift3 xcode8

我有一个可以扫描条形码的iOS应用程序。

现在我想定义一个特定的扫描区域。为此,我使用了rectOfInterest属性和metadataOutputRectOfInterest方法。

我面临的问题

如果我只使用下面的代码,则无法扫描任何内容,如果我删除barcodeAreaView扫描在整个显示屏上都能正常工作

class BarcodeReaderViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

    // Camera view
    var cameraView: AVCaptureVideoPreviewLayer?
    // AV capture session and dispatch queue
    let captureSession = AVCaptureSession()
    let sessionQueue = DispatchQueue(label: AVCaptureSession.self.description(), attributes: [], target: nil)
    var barcodeAreaView: UIView?

    let supportedCodeTypes = [
        AVMetadataObjectTypeEAN8Code,
        AVMetadataObjectTypeEAN13Code,
        AVMetadataObjectTypeUPCECode,
        AVMetadataObjectTypeCode39Code,
        AVMetadataObjectTypeCode93Code,
        AVMetadataObjectTypeCode128Code,
        AVMetadataObjectTypeCode39Mod43Code,
        AVMetadataObjectTypeInterleaved2of5Code
    ]
    let metadataOutput = AVCaptureMetadataOutput()
    var barcodeArea:CGRect!

    override func viewDidLoad() {
        super.viewDidLoad()

        let width = 250
        let height = 100

        print(self.view.frame.size.width)

        let xPos = (CGFloat(self.view.frame.size.width) / CGFloat(2)) - (CGFloat(width) / CGFloat(2))
        let yPos = (CGFloat(self.view.frame.size.height) / CGFloat(2)) - (CGFloat(height) / CGFloat(2))
        barcodeArea = CGRect(x: Int(xPos), y: Int(yPos), width: width, height: height)

        barcodeAreaView = UIView()
        barcodeAreaView?.layer.borderColor = UIColor.red.cgColor
        barcodeAreaView?.layer.borderWidth = 1
        barcodeAreaView?.frame = barcodeArea
        view.addSubview(barcodeAreaView!)

        captureSession.beginConfiguration()

        let videoDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

        if videoDevice != nil {

            let videoDeviceInput = try? AVCaptureDeviceInput(device: videoDevice)

            if videoDeviceInput != nil {

                if captureSession.canAddInput(videoDeviceInput) {

                    captureSession.addInput(videoDeviceInput)
                }
            }

            if captureSession.canAddOutput(metadataOutput) {

                captureSession.addOutput(metadataOutput)

                metadataOutput.metadataObjectTypes = supportedCodeTypes
                metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)

                cameraView = AVCaptureVideoPreviewLayer(session: captureSession)
                cameraView?.videoGravity = AVLayerVideoGravityResizeAspectFill
                cameraView?.frame = view.layer.bounds

                metadataOutput.rectOfInterest = cameraView!.metadataOutputRectOfInterest(for: barcodeArea)
                view.layer.addSublayer(cameraView!)
            }
        }

        captureSession.commitConfiguration()
        view.bringSubview(toFront: barcodeAreaView!)
    }

    override func viewWillAppear(_ animated: Bool) {

        super.viewWillAppear(animated)

        // Start AV capture session
        sessionQueue.async {
            self.captureSession.startRunning()
        }
    }

    override func viewWillDisappear(_ animated: Bool) {

        super.viewWillDisappear(animated)

        // Stop AV capture session
        sessionQueue.async {
            self.captureSession.stopRunning()
        }
    }

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

        if let metadataObject = metadataObjects.first {

            let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject            
            let message = readableObject.stringValue
            print(message)
        }
    }
}

我已经看过这个问题了

How do I use the metadataOutputRectOfInterestForRect method and rectOfInterest property to scan a specific area? (QR Code)

我在这里面临的问题是,扫描有点滞后,我的navigationController无法正常工作(因为听众等待扫描)

更新

解决了这个问题,如本回答所示

https://stackoverflow.com/a/37603743/2319900

我在self.captureSession.startRunning()

之后添加了以下一行
metadataOutput.rectOfInterest = cameraView!.metadataOutputRectOfInterest(for: barcodeArea)

0 个答案:

没有答案