[英]Not able to Open a Camera for iOS
I am trying to create a barcode reader feature for an app. 我正在尝试为应用程序创建条形码阅读器功能。 For which I am not able to get any camera devices.
为此,我无法获得任何相机设备。
I am using AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera], mediaType: AVMediaType.video, position:. back)
to get the list of devices. 我正在使用
AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera], mediaType: AVMediaType.video, position:. back)
来获取设备列表。
I am trying to get the first device from the above list 我想从上面的列表中获取第一个设备
But it does not returns any cameras. 但它不会返回任何相机。
I have also added a Privacy - Camera Usage Description in info.plist
我还在
info.plist
添加了一个隐私 - 相机使用说明
func viewDidLoad() {
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera], 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
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
videoPreviewLayer?.frame = view.layer.bounds
view.layer.addSublayer(videoPreviewLayer!)
//Start video capture.
captureSession?.startRunning()
} catch {
// if any error occurs, print it out and don't continue any more
print(error)
return
}
}
I expect it to start a camera; 我希望它可以启动相机; however, it rather gives me following error:
但是,它反过来给我以下错误:
2019-05-05 16:17:46.609442-0500 BarCode[964:302791] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2019-05-05 16:17:46.609442-0500 BarCode [964:302791] [MC] systemgroup.com.apple.configurationprofiles路径的系统组容器是/private/var/containers/Shared/SystemGroup/systemgroup.com.apple .configurationprofiles
2019-05-05 16:17:46.609962-0500 BarCode[964:302791] [MC] Reading from public effective user settings.
2019-05-05 16:17:46.609962-0500 BarCode [964:302791] [MC]从公共有效用户设置中读取。
Failed to get the camera device. 无法获得相机设备。
It could be just as the error mentions. 它可能就像错误提到的那样。 There isn't any
.builtInDualCamera
option available for the device. 没有任何
.builtInDualCamera
选项可用于该设备。
The type for common camera that you can try is 您可以尝试的普通相机类型
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back)
If you really do require the .builtInDualCamera
, you can follow as the Apple document suggested which is to implement an if...else
check for a fallback plan. 如果你确实需要
.builtInDualCamera
,你可以按照Apple文档建议的那样执行if...else
检查后备计划。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.