首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVFoundation,DataMatrix,Swift 4

AVFoundation,DataMatrix,Swift 4
EN

Stack Overflow用户
提问于 2017-10-13 20:21:43
回答 1查看 1K关注 0票数 2

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

我有一个数据矩阵"QR“代码,我正在试图阅读。

它在Swift 3编译中读得很好,但是随着Swift 4对代码的更改,它就不会被选中。

还注意到,苹果提供的使用Swift 4的示例既不读取DataMatrix代码,也不读取

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

代码语言:javascript
复制
print("types available:\n \(metadataOutput.availableMetadataObjectTypes)")

产量:

代码语言:javascript
复制
types available:
[__ObjC.AVMetadataObject.ObjectType(_rawValue: face), ... 
__ObjC.AVMetadataObject.ObjectType(_rawValue: org.iso.DataMatrix), ...

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

此外,显式地添加类型:

代码语言:javascript
复制
metadataOutput.metadataObjectTypes = [AVMetadataObject.ObjectType.dataMatrix]

没什么不同的。

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

代码:

代码语言:javascript
复制
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

    }
}
}

非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-10 07:51:29

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46737504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档