func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
captureSession.stopRunning()
if let metadataObject = metadataObjects.first {
let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject;
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
found(code: readableObject.stringValue!);
}
}
上述功能完美运行,但我尝试获取扫描结果并将其作为照片写入设备中,但目前似乎不可能。
最佳答案
实现这一点的方法是使用CoreImage
。当您获取 readableObject.stringValue
时,您可以使用过滤器 CICode128BarcodeGenerator
生成图像。
因此,在您的示例中:
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
captureSession.stopRunning()
if let metadataObject = metadataObjects.first {
let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject;
let barcodeFilter = CIFilter(name: "CICode128BarcodeGenerator")
barcodeFilter?.setValue(readableObject.stringValue!.data(using: .ascii), forKey: "inputMessage")
let image = UIImage(ciImage: (barcodeFilter?.outputImage)!)
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
found(code: readableObject.stringValue!);
}
}
记住在文件开头导入CoreImage
。
关于ios - 如何使用 Swift 3 获取扫描的条形码照片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47822892/