代码之家  ›  专栏  ›  技术社区  ›  addzo

AVFoundation、DataMatrix、Swift 4

  •  2
  • addzo  · 技术社区  · 7 年前

    啊。我在Swift 4 AVFoundation中与变化的斗争仍在继续。

    我有一个数据矩阵“QR”代码,我正在尝试读取。

    在Swift 3编译时,它读起来很好,但在Swift 4对代码进行更改时,它不会被拾取。

    还请注意,苹果提供的本应与Swift 4配合使用的示例也没有读取DataMatrix代码

    当我打印出可用类型时,数据矩阵可用。

    print("types available:\n \(metadataOutput.availableMetadataObjectTypes)")
    

    产量:

    types available:
    [__ObjC.AVMetadataObject.ObjectType(_rawValue: face), ... 
    __ObjC.AVMetadataObject.ObjectType(_rawValue: org.iso.DataMatrix), ...
    

    然而,当我运行代码时,从未调用didOutput metadataObjects:。然而,它确实被称为其他类型。

    metadataOutput.metadataObjectTypes = [AVMetadataObject.ObjectType.dataMatrix]
    

    没有什么不同。

    有人有在Swift 4中扫描DataMatrix的经验吗?

    import UIKit
    import AVFoundation
    
    class ViewController: UIViewController,AVCaptureMetadataOutputObjectsDelegate {
    
    var videoCaptureDevice: AVCaptureDevice = AVCaptureDevice.default(for: AVMediaType.video)!
    var device = AVCaptureDevice.default(for: AVMediaType.video)
    var output = AVCaptureMetadataOutput()
    var previewLayer: AVCaptureVideoPreviewLayer?
    
    var captureSession = AVCaptureSession()
    var code: String?
    
    var scannedCode = UILabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.setupCamera()
        self.addLabelForDisplayingCode()
    }
    
    private func setupCamera() {
    
        let input = try? AVCaptureDeviceInput(device: videoCaptureDevice)
    
        if self.captureSession.canAddInput(input!) {
            self.captureSession.addInput(input!)
        }
    
        self.previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    
        if let videoPreviewLayer = self.previewLayer {
            videoPreviewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
            videoPreviewLayer.frame = self.view.bounds
            view.layer.addSublayer(videoPreviewLayer)
        }
    
        let metadataOutput = AVCaptureMetadataOutput()
        if self.captureSession.canAddOutput(metadataOutput) {
            self.captureSession.addOutput(metadataOutput)
    
            metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
    
            print("types available:\n \(metadataOutput.availableMetadataObjectTypes)")
    
            metadataOutput.metadataObjectTypes = metadataOutput.availableMetadataObjectTypes
    //            metadataOutput.metadataObjectTypes = [AVMetadataObject.ObjectType.dataMatrix]
        } else {
            print("Could not add metadata output")
        }
    }
    
    private func addLabelForDisplayingCode() {
        view.addSubview(scannedCode)
        scannedCode.translatesAutoresizingMaskIntoConstraints = false
        scannedCode.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20.0).isActive = true
        scannedCode.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20.0).isActive = true
        scannedCode.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20.0).isActive = true
        scannedCode.heightAnchor.constraint(equalToConstant: 50).isActive = true
        scannedCode.font = UIFont.preferredFont(forTextStyle: .title2)
        scannedCode.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        scannedCode.textAlignment = .center
        scannedCode.textColor = UIColor.white
        scannedCode.text = "Scanning...."
    }
    
    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 metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
        print(metadataObjects)
        for metadata in metadataObjects {
            let readableObject = metadata as! AVMetadataMachineReadableCodeObject
            let code = readableObject.stringValue
            scannedCode.text = code
    
        }
    }
    }
    

    非常感谢。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Andy Jacobs    5 年前

    您必须确保连接没有镜像。 数据矩阵需要以原始格式读取。

    https://developer.apple.com/documentation/avfoundation/avcaptureconnection/1389172-isvideomirrored