gpt4 book ai didi

ios - 发现不支持的类型 - AVMetadataObject

转载 作者:搜寻专家 更新时间:2023-11-01 06:28:16 25 4
gpt4 key购买 nike

我正在尝试在我的 iOS 应用程序中实现条形码扫描,但由于某种原因我无法打开相机。我正在使用 Apple 的 AVFoundation 框架进行扫描。

这是我启动相机和实现扫描仪的代码:

// Get the back-facing camera for capturing videos
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back)

guard let captureDevice = deviceDiscoverySession.devices.first else {
print("Failed to get the camera device")
return
}

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

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

// Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
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 = [AVMetadataObject.ObjectType.qr]

// 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()

// 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(error)
return
}

当屏幕加载时,我立即将以下消息打印到控制台:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVCaptureMetadataOutput setMetadataObjectTypes:] Unsupported type found - use -availableMetadataObjectTypes'
*** First throw call stack:
(0x1824d2d8c 0x18168c5ec 0x18803eb44 0x1013f035c 0x1013f12ec 0x18c23e64c 0x18c35f870 0x18c244700 0x18c37a1a8 0x18c2c19e0 0x18c2b6890 0x18c2b51d0 0x18ca96d1c 0x18ca992c8 0x18ca92368 0x18247b404 0x18247ac2c 0x18247879c 0x182398da8 0x18437d020 0x18c3b5758 0x1013da1d8 0x181e29fc0)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

我在 Stack Overflow 或任何其他网站上找不到任何解决方案。

你能帮我解决这个问题吗?

最佳答案

所以我有同样的错误,我通过做一些小的改动解决了它。希望这对您有所帮助(如果您还没有解决它!)。

    let input = try AVCaptureDeviceInput(device: captureDevice)

//Make sure you have initialized the captureSession object
captureSession = AVCaptureSession()
captureSession?.addInput(input)

videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
videoPreviewLayer?.frame = view.layer.bounds
view.layer.addSublayer(videoPreviewLayer!)

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


// Main Change
captureMetadataOutput.metadataObjectTypes = captureMetadataOutput.availableMetadataObjectTypes
//[AVMetadataObject.ObjectType.qr]

captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)


captureSession?.startRunning()

关于ios - 发现不支持的类型 - AVMetadataObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51020651/

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