gpt4 book ai didi

swift 4上的条形码

转载 作者:搜寻专家 更新时间:2023-11-01 05:33:55 24 4
gpt4 key购买 nike

我正在尝试将 mi app 升级到 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。

有什么想法吗?谢谢。

最佳答案

我想通了,但 Apple 并没有把它说得那么明显。来自委托(delegate) AVCaptureMetadataOutputObjectsDelegate 的回调函数已重命名且参数名称不同!

所以,替换

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

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

在此之后,我的 View Controller 现在像以前一样扫描二维码。它具有相同的参数,但第一个参数名称不同。更改函数和参数名称并构建/运行。

关于swift 4上的条形码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46308843/

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