ios - 如何使用 Swift 3 获取扫描的条形码照片?

标签 ios swift barcode

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/

相关文章:

使用 Zxing 的 Android 条码扫描器

ios - 如何在 iOS 应用程序中显示 Web 服务调用的进度条

ios - 有没有办法从 React Native 中的 chrome 控制台重新加载设备,这样我就不必不断地摇动它?

ios - 如何删除位于可见区域之外的图 block 图层 Google map SDK for iOS

ios - TableView 项目在滚动时刷新

swift - DateComponents 和通知未显示

c# - 如何在 C# 中打印 itf 条形码

python - 如何在python 3.7中生成条形码

ios - 如何找出核心数据模型iOS中特定属性索引的值

iphone: View 隐藏后重新启动循环动画,然后重新出现?