无法为 iOS 打开相机
Not able to Open a Camera for iOS
我正在尝试为应用程序创建条形码 reader 功能。为此,我无法获得任何相机设备。
我正在使用 AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera], mediaType: AVMediaType.video, position:. back)
获取设备列表。
我正在尝试从上面的列表中获取第一个设备
但它没有 returns 任何相机。
我还在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
}
}
我希望它能启动相机;但是,它反而给我以下错误:
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.609962-0500 BarCode[964:302791] [MC] Reading from public effective user settings.
获取摄像头设备失败
也可能和错误所说的一样。该设备没有任何可用的 .builtInDualCamera
选项。
您可以尝试的常见相机型号是
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back)
如果您确实需要 .builtInDualCamera
,您可以按照 Apple 文档的建议执行 if...else
检查后备计划。
我正在尝试为应用程序创建条形码 reader 功能。为此,我无法获得任何相机设备。
我正在使用 AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera], mediaType: AVMediaType.video, position:. back)
获取设备列表。
我正在尝试从上面的列表中获取第一个设备
但它没有 returns 任何相机。
我还在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
}
}
我希望它能启动相机;但是,它反而给我以下错误:
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.609962-0500 BarCode[964:302791] [MC] Reading from public effective user settings.
获取摄像头设备失败
也可能和错误所说的一样。该设备没有任何可用的 .builtInDualCamera
选项。
您可以尝试的常见相机型号是
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back)
如果您确实需要 .builtInDualCamera
,您可以按照 Apple 文档的建议执行 if...else
检查后备计划。