gpt4 book ai didi

用于条形码扫描的 swift Avcapture session 无法正常工作

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

我正在尝试构建条形码扫描器。我改编了其中的一些 tutorial .视频捕获 session 正在运行,但未检测到任何条形码。我已经多次检查代码,但仍然找不到问题所在。这是我检测条形码的代码

class ScanController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

var captureSession: AVCaptureSession?
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
var qrCodeFrameView: UIView?

let supportedCodeTypes = [AVMetadataObject.ObjectType.upce,
AVMetadataObject.ObjectType.code39,

AVMetadataObject.ObjectType.qr]

override func viewDidLoad() {
super.viewDidLoad()

//Get an instance of the AVCaptureDevice class a device object and provide the video as the media type parameter
let captureDevice = AVCaptureDevice.default(for: AVMediaType.video)

do {

// Get an instance of the AVCaptureDeviceInput class using the previous device object.
let input = try AVCaptureDeviceInput(device: captureDevice!)

// Initialize the captureSession object.
captureSession = AVCaptureSession()

// Set the input device on the capture session.
captureSession?.addInput(input)

let captureMetadataOutput = AVCaptureMetadataOutput()
captureSession?.addOutput(captureMetadataOutput)

// Set delegate and use the default dispatch queue to execute the call back
captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
captureMetadataOutput.metadataObjectTypes = supportedCodeTypes
// Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
videoPreviewLayer?.frame = view.layer.bounds
view.layer.addSublayer(videoPreviewLayer!)
// Start video capture.
captureSession?.startRunning()
// Add the message label
self.view.addSubview(messageLabel)


//initialize QR Code Frame to highlight the QR Code
qrCodeFrameView = UIView()
if let qrCodeFrameView = qrCodeFrameView {

qrCodeFrameView.layer.borderColor = UIColor.green.cgColor
qrCodeFrameView.layer.borderWidth = 2
view.addSubview(qrCodeFrameView)
view.bringSubview(toFront: qrCodeFrameView)

}
} catch {

// If any error occurs, simply print it out and don't continue any more.
print("THERE IS A PROBLEM WITH THE CAPTURE SESSION *****************")
print(error)
return
}
}

}

我错过了什么?

最佳答案

也许您缺少委托(delegate)方法?在教程中是委托(delegate)方法:

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

解码二维码部分下

关于用于条形码扫描的 swift Avcapture session 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50343071/

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