iOS:扫描条形码非常慢

iOS:扫描条形码非常慢,ios,avfoundation,barcode,swift4,google-vision,Ios,Avfoundation,Barcode,Swift4,Google Vision,我正在使用swift4开发扫描条形码应用程序 我试过使用两种流行的开源软件,它们是(使用AVFoundation)和(使用GoogleVision框架),但扫描速度并不像我想象的那么快,检测真正的条形码需要2到3秒 有些人建议我将sessionPreset更改为AVCaptureSessionPresetMedium,但它无效 任何人都可以建议我如何提高扫描速度。我们应该更新另一个摄像头配置还是使用另一个开源软件?我已经做了QRCode和条形码扫描仪,我将在下面分享这段代码 import AVF

我正在使用swift4开发扫描条形码应用程序

我试过使用两种流行的开源软件,它们是(使用AVFoundation)和(使用GoogleVision框架),但扫描速度并不像我想象的那么快,检测真正的条形码需要2到3秒

有些人建议我将sessionPreset更改为AVCaptureSessionPresetMedium,但它无效


任何人都可以建议我如何提高扫描速度。我们应该更新另一个摄像头配置还是使用另一个开源软件?

我已经做了QRCode和条形码扫描仪,我将在下面分享这段代码

import AVFoundation
添加代理

class YOUR_VIEW_CONTROLLER: UIViewController,AVCaptureMetadataOutputObjectsDelegate 
初始化所需的变量

    var captureSession:AVCaptureSession?
    var videoPreviewLayer:AVCaptureVideoPreviewLayer?
    var qrCodeFrameView:UIView?
    var qrCodeDelegate:QRCodeScannerVCDelegate?

    let supportedCodeTypes = [AVMetadataObjectTypeUPCECode,
                              AVMetadataObjectTypeCode39Code,
                              AVMetadataObjectTypeCode39Mod43Code,
                              AVMetadataObjectTypeCode93Code,
                              AVMetadataObjectTypeCode128Code,
                              AVMetadataObjectTypeEAN8Code,
                              AVMetadataObjectTypeEAN13Code,
                              AVMetadataObjectTypeAztecCode,
                              AVMetadataObjectTypePDF417Code,
                              AVMetadataObjectTypeQRCode]
将以下函数添加到viewController中

   func startVideoCapture(){
        // 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 {
            // Get an instance of the AVCaptureDeviceInput class using the previous device object.
            let input = try AVCaptureDeviceInput(device: captureDevice)

            // Initialize the captureSession object.
            captureSession = AVCaptureSession()

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

            // Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
            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 = supportedCodeTypes

            // Initialize 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 = self.qrCodeView.bounds
            self.qrCodeView.layer.addSublayer(videoPreviewLayer!)
            self.qrCodeView.clipsToBounds = true
            // Start video capture.
            captureSession?.startRunning()
            qrCodeFrameView = UIView()

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

        } catch {
            // If any error occurs, simply print it out and don't continue any more.
            print(error)
            return
        }
    }

    // MARK: - AVCaptureMetadataOutputObjectsDelegate Methods

    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
            print("No QR/barcode is detected")
            return
        }

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

        if supportedCodeTypes.contains(metadataObj.type) {
            // 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)
            if barCodeObject != nil{
                qrCodeFrameView?.frame = barCodeObject!.bounds
            }

            if metadataObj.stringValue != nil {
                print("\(metadataObj.stringValue)")

            }
        }
    }
并且在您的
视图中将出现

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    self.startVideoCapture()
}
在打电话给startViewCapture之前,别忘了检查摄像机的许可


希望这能帮助您

帮助我加快条形码扫描速度的是将
AVCaptureSession的
sessionPreset
属性设置为高:

captureSession = AVCaptureSession()
captureSession.sessionPreset = .high
希望能有帮助

更新


你也可以试试。当我测试它时,它运行得非常快。

你正在扫描哪种条形码?我已经为EAN-8和EAN-13使用了内置的AVMetadataOutput,并且发现它非常可靠和快速。不确定是否有人已经这样做了,但是强制聚焦可能会有很大帮助,因为很多时候只是在尝试获取焦点而不是实际的条形码标识时浪费了很多时间。除此之外,你还需要提供更多的细节,包括条形码的类型、大小、距离、材质类型、对比度是否良好等。实际使用的手机也会有所帮助。当然,我们假设您使用的是后置摄像头。@jcaron:我要扫描的条形码是128码类型。尺寸约为1厘米x 4厘米。我试过iphone6、6s和5。我想我已经试过调整相机的对比度,但扫描速度并没有更快。你能告诉我如何直接将焦点集中在条形码上吗?@ThangBM面临同样的问题。条形码扫描速度慢。解决方案是什么?你对调焦模式有什么建议吗?我必须移动相机以获得焦点,条形码总是失去焦点。你是说覆盖还是自动对焦?我是说我想要相机快速自动对焦条形码。在某些扫描条形码应用程序中,它非常快速地关注条形码。