在Swift 4上的条形码



我正在尝试将Mi应用程序升级到Swift 4,但是条形码读取器不起作用。

我已经隔离了条形码读取器代码,但仍无法正常工作。相机工作起来,但无法检测到条形码。

该代码在Swift 3 iOS 10中工作正常。

这是完整的代码

import AVFoundation
import UIKit
class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!
override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor.black
    captureSession = AVCaptureSession()
    let videoCaptureDevice = AVCaptureDevice.default(for: AVMediaType.video)
    let videoInput: AVCaptureDeviceInput
    do {
        videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice!)
    } catch {
        return
    }
    if (captureSession.canAddInput(videoInput)) {
        captureSession.addInput(videoInput)
    } else {
        failed();
        return;
    }
    let metadataOutput = AVCaptureMetadataOutput()
    if (captureSession.canAddOutput(metadataOutput)) {
        captureSession.addOutput(metadataOutput)
        metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
        metadataOutput.metadataObjectTypes = [AVMetadataObject.ObjectType.ean8, AVMetadataObject.ObjectType.ean13, AVMetadataObject.ObjectType.pdf417]
    } else {
        failed()
        return
    }
    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession);
    previewLayer.frame = view.layer.bounds;
    previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill;
    view.layer.addSublayer(previewLayer);
    captureSession.startRunning();
}
func failed() {
    let ac = UIAlertController(title: "Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.", preferredStyle: .alert)
    ac.addAction(UIAlertAction(title: "OK", style: .default))
    present(ac, animated: true)
    captureSession = nil
}
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if (captureSession?.isRunning == false) {
        captureSession.startRunning();
    }
}
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    if (captureSession?.isRunning == true) {
        captureSession.stopRunning();
    }
}
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
    captureSession.stopRunning()
    if let metadataObject = metadataObjects.first {
        let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject;
        AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
        found(code: readableObject.stringValue!);
    }
    dismiss(animated: true)
}
func found(code: String) {
    print(code)
}
override var prefersStatusBarHidden: Bool {
    return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}
}

我在iPhone上使用iOS 11,升级为Beta 9。

有什么想法吗?谢谢。

我弄清楚了,但是苹果并没有那么明显。来自代表AvcapturemetadataOutputobjectsdelegate的回调函数已重命名,参数名称不同!

所以,更换

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!)

to

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

我的视图控制器现在像以前一样扫描QR码。它具有相同的参数,但第一个参数名称不同。更改功能和参数名称并构建/运行。

更改代表回电后:

来自

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!)

to

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

我也需要为MetadataObjectTypes设置所有可用类型 -

output.metadataObjectTypes=output.availableMetadataObjectTypes

从:

更改代码后
func metadataOutput(captureOutput: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {}

to:

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

一切都可以再次工作。

您可以使用qrcodescanner83进行扫描条形码:

import QRCodeScanner83
import AVFoundation
...
guard let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "CodeScannerViewController") as? CodeScannerViewController else {
    return
}
vc.callbackCodeScanned = { code in
    print("SCANNED CODE: (code)")
    vc.dismiss(animated: true, completion: nil)
}
self.present(vc, animated: true, completion: nil)

如果您需要自定义UI,则可以从CodeScannerViewController筑巢并设置CodeScannerViewController.delegate以接收扫描仪状态的更新。

相关内容

  • 没有找到相关文章

最新更新