gpt4 book ai didi

ios - Swift4 条形码(ean13)不工作

转载 作者:行者123 更新时间:2023-11-28 14:38:27 27 4
gpt4 key购买 nike

我尝试让程序从下面的代码中读取 EAN13,但它不起作用

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


if metadataObjects != nil && metadataObjects.count != 0 {
if let object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject {
print(object.type)
if object.type == AVMetadataObject.ObjectType.ean13{
let alert = UIAlertController(title: "Your code is:", message: object.stringValue, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Retake", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Copy", style: .default, handler: { (nil) in
UIPasteboard.general.string = object.stringValue
}))
present(alert, animated: true, completion: nil)
}
}
}

但对于 QR 码 AVMetadataObject.ObjectType.QR 它工作正常。

我试过 enter image description here

最佳答案

我能够使用此示例读取您使用的条形码:

    var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!

@IBAction func scanBarCode(_ sender: UIButton){
captureSession = AVCaptureSession()

guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else { return }
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 = [.qr, .aztec,.code128, .code39, .code39Mod43, .code93, .dataMatrix, .ean13,.ean8]
} else {
failed()
return
}

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = view.layer.bounds
previewLayer.videoGravity = .resizeAspectFill
view.layer.addSublayer(previewLayer)

captureSession.startRunning()
}

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

if let metadataObject = metadataObjects.first {
guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
guard let stringValue = readableObject.stringValue else { return }
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
found(code: stringValue)
}

dismiss(animated: true)
}

func found(code: String) {
print(code)

if !code.isEmpty{
scannerResultLabel.text = code
}
if let previewLayer = previewLayer{
previewLayer.removeFromSuperlayer()
}
}

关于ios - Swift4 条形码(ean13)不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50705068/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com