AVFoundation PDF417 扫描仪并不总是有效

AVFoundation PDF417 scanner doesn't always work

我正在使用 Swift 4 和 Xcode 9 创建一个使用 AVFoundation 扫描 PDF417 条形码的应用程序。扫描仪可以处理某些代码,但无法识别您会在 CA 彩票刮刮乐票正面找到的 PDF417 条形码。

我是否缺少任何让它工作的东西?下面是我的代码:

let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera], mediaType: AVMediaType.video, position: .back)

    guard let captureDevice = deviceDiscoverySession.devices.first else {
        print("Failed to get the camera device")
        return
    }

    do {
        captureSession = AVCaptureSession()

        let input = try AVCaptureDeviceInput(device: captureDevice)
        captureSession!.addInput(input)

        let captureMetadataOutput = AVCaptureMetadataOutput()

        captureSession!.addOutput(captureMetadataOutput)
        captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
        captureMetadataOutput.metadataObjectTypes = [AVMetadataObject.ObjectType.pdf417]

        videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
        videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
        videoPreviewLayer?.frame = view.layer.bounds
        view.layer.addSublayer(videoPreviewLayer!)
        captureSession?.startRunning()
     } catch {
        print(error)
        return
    }

 func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {

    //Get the metadata object
    let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
    if scanType.contains(metadataObj.type) {
        let barCodeObj = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)

        if(metadataObj.stringValue != nil) {
            callDelegate(metadataObj.stringValue)
            captureSession?.stopRunning()
            AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
            navigationController?.popViewController(animated: true)
        }
    }
}

谢谢!

在您的 viewDidLoad 或您希望它位于的某些方法中,用以下代码替换扫描仪的初始化代码

// Global vars used in init below
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!


func setupCaptureInputDevice() {
    let cameraMediaType = AVMediaType.video

    captureSession = AVCaptureSession()

    // get the video capture device, which should be of type video
    guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else {

        // if there is an error then something is wrong, so dismiss
        dismiss(animated: true, completion: nil)
        return

    }

    let videoInput: AVCaptureDeviceInput

    // create a capture input for the above device input that was created
    do {
        videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
    } catch {
        return
    }

    // this is important to check for if we are able to add the input
    // because adding before this could cause a crash or it could not happen
    if (captureSession.canAddInput(videoInput)) {
        captureSession.addInput(videoInput)
    } else {
        // dismiss or display error
        return
    }

    // get ready to capture output somewhere
    let metadataOutput = AVCaptureMetadataOutput()

    // again check to make sure that we can do this operation before doing it
    if (captureSession.canAddOutput(metadataOutput)) {
        captureSession.addOutput(metadataOutput)

        // setting the metadataOutput's delegate to be self and then requesting it run on the main thread
        metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
        // specify your code type
        metadataOutput.metadataObjectTypes = [.pdf417]
    } else {
        // dismiss or display error
        return
    }

    // the preview layer now becomes the capture session
    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

    // just add it to the screen
    previewLayer.frame = view.layer.bounds
    previewLayer.videoGravity = .resizeAspectFill
    view.layer.addSublayer(previewLayer)

    // and begin input
    captureSession.startRunning()
}