【问题标题】:How to get scanned barcode photo with Swift 3?如何使用 Swift 3 获取扫描的条码照片?
【发布时间】:2017-12-14 22:13:28
【问题描述】:
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!);
    }
}

上述功能完美运行,但我试图获取扫描结果并将其作为照片写入设备,但目前似乎不可能。

【问题讨论】:

    标签: ios swift barcode


    【解决方案1】:

    这样做的方法是使用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!);
        }
    }
    

    记得在文件开头import CoreImage

    【讨论】:

    • 有什么办法吗?如果我从相机保存扫描的图像,是否可以?
    • 我不明白你的意思。您想从图片中获取条形码字符串吗?还是要保存图片的一部分?
    • 我想保存扫描的条码图像部分。
    • 我想保存扫描的条形码图像的一部分,并在我扫描时获取条形码字符串。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多