我正在使用swift4开发扫描条形码应用程序。
我试过使用两种流行的开源软件,分别是MTBarcode (使用AVFoundation)和iOS Vision (使用GoogleVision框架),但扫描速度并不像我预期的那样快,大约需要2到3秒才能检测到真正的条形码。
有人建议我把sessionPreset改成AVCaptureSessionPresetMedium,但效果不好。
任何人都可以建议我如何提高扫描速度。我们应该更新另一个摄像头配置还是使用另一个开源软件?
发布于 2019-05-06 02:22:46
帮助我加速条形码扫描的是将AVCaptureSession的sessionPreset属性设置为high:
captureSession = AVCaptureSession()
captureSession.sessionPreset = .high希望能有所帮助。
更新
您也可以尝试使用Firebase ML Kit。当我测试它的时候,它工作得非常快。
发布于 2017-11-28 21:42:43
我做了QRCode和BarCodeScanner,我将分享下面的代码片段
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)")
}
}
}在你的viewWillAppear中
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
self.startVideoCapture()
}在调用startViewCapture之前,别忘了检查摄像头权限
希望这能对你有所帮助
https://stackoverflow.com/questions/47532758
复制相似问题