我正在开发一个二维码和矩阵码阅读器应用程序。我得到了
AVCaptureDeviceInput
AvCaptureDevice.DiscoverySession
. 我的问题是它只有在iOS 10.0之后才可用。我怎样才能得到它的回退版本?
// Get the back-facing camera for capturing videos
if #available(iOS 10.0, *) {
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
}
} else {
// Fallback on earlier versions
}
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)
//TODO: Decide the data types!
captureMetadataOutput.metadataObjectTypes = [AVMetadataObject.ObjectType.dataMatrix]
//captureMetadataOutput.metadataObjectTypes = [AVMetadataObject.ObjectType.qr]
} catch {
// If any error occurs, simply print it out and don't continue any more.
print(error)
return
}