我正在使用swift4开发扫描条形码应用程序。
我尝试过使用2个流行的开源,MTBarcode(使用AVFoundation)和iOS Vision(使用GoogleVision Framework),但扫描速度并不像我预期的那么快,需要大约2个或者3秒钟检测真实的条形码。
有些人建议我将sessionPreset更改为AVCaptureSessionPresetMedium,但它无效。
任何人都可以建议我如何提高扫描速度。我们应该更新其他相机配置还是使用其他开源?
答案 0 :(得分:0)
我已经完成了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
之前不要忘记检查相机权限希望这会对你有所帮助
答案 1 :(得分:0)
帮助我加快条形码扫描速度的原因是将sessionPreset
的{{1}}属性设置为高:
AVCaptureSession
希望有帮助。