首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS :扫描条形码非常慢

iOS :扫描条形码非常慢
EN

Stack Overflow用户
提问于 2017-11-28 21:27:37
回答 2查看 2.3K关注 0票数 2

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

我试过使用两种流行的开源软件,分别是MTBarcode (使用AVFoundation)和iOS Vision (使用GoogleVision框架),但扫描速度并不像我预期的那样快,大约需要2到3秒才能检测到真正的条形码。

有人建议我把sessionPreset改成AVCaptureSessionPresetMedium,但效果不好。

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

EN

回答 2

Stack Overflow用户

发布于 2019-05-06 02:22:46

帮助我加速条形码扫描的是将AVCaptureSessionsessionPreset属性设置为high:

代码语言:javascript
复制
captureSession = AVCaptureSession()
captureSession.sessionPreset = .high

希望能有所帮助。

更新

您也可以尝试使用Firebase ML Kit。当我测试它的时候,它工作得非常快。

票数 3
EN

Stack Overflow用户

发布于 2017-11-28 21:42:43

我做了QRCode和BarCodeScanner,我将分享下面的代码片段

代码语言:javascript
复制
import AVFoundation

添加委派

代码语言:javascript
复制
class YOUR_VIEW_CONTROLLER: UIViewController,AVCaptureMetadataOutputObjectsDelegate 

初始化所需变量

代码语言:javascript
复制
    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中

代码语言:javascript
复制
   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)")

            }
        }
    }

在你的viewWillAppear

代码语言:javascript
复制
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    self.startVideoCapture()
}

在调用startViewCapture之前,别忘了检查摄像头权限

希望这能对你有所帮助

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

https://stackoverflow.com/questions/47532758

复制
相关文章

相似问题

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