ios - 无法从静态图像检测到QR码

标签 ios

我想从静止图像中检测二维码
这是代码

let ciimage =  <Load image from asset and covert to CIImage>
let detector = CIDetector(ofType: CIDetectorTypeQRCode, context:nil, options:nil)
let features = detector.featuresInImage(ciimage)
print(features.count)

该图像是具有QRCode的正确CIImage实例,但是我始终没有任何功能,有人遇到过相同的问题吗?

非常感谢你。

最佳答案

我想我找到了答案

  • 该代码仅在游乐场真正的iPhone 中起作用,而在模拟器
  • 上则不起作用
  • 我使用 CIQRCodeGenerator CoreImage过滤器生成了QRCode图像。

  • 我希望有人可以在这里查看我的回答并提供建议。

    下面是我使用的源代码
    enum QRCodeUtilError:ErrorType{
        case GenerationFailed
    }
    
    extension UIImage {
        /**
         This function trys to return CIImage even the image is CGImageRef based.
         - Returns: CIImage equal to current image
        */
        func ciImage() -> CoreImage.CIImage?{
            // if CIImage is not nil then return it
            if self.CIImage != nil {
                return self.CIImage
            }
    
            guard let cgImage = self.CGImage else {
                return nil
            }
            return CoreImage.CIImage(CGImage: cgImage)
        }
    }
    
    /**
    QRCode utility class
    */
    class QRCodeUtil{
        /**
         Generates 'H' mode qrcode image
         - Parameter qrCode: QRCode String
         - Returns: Optional UIImage instance of QRCode
         */
        class func imageForQrCode(qrCode:String) throws -> UIImage{
            guard let filter = CIFilter(name: "CIQRCodeGenerator") else {
                throw QRCodeUtilError.GenerationFailed
            }
            guard let data = qrCode.dataUsingEncoding(NSISOLatin1StringEncoding) else {
                throw QRCodeUtilError.GenerationFailed
            }
            filter.setValue(data, forKey: "inputMessage")
            filter.setValue("H", forKey: "inputCorrectionLevel")
    
            guard let outputImage = filter.outputImage else {
                throw QRCodeUtilError.GenerationFailed
            }
    
            /* 
        Convert the image to CGImage based because
        it crashes when saving image using
        UIImagePNGRepresentation or UIImageJPEGRepresentation
        if image is CIImage based. 
        */
            let context = CIContext(options: nil)
            let cgImage = context.createCGImage(outputImage, fromRect: outputImage.extent)
            return UIImage(CGImage: cgImage)
        }
    
        /**
         Detects QRCode from string
         - Parameter qrImage: UIImage which has QRCode
         - Returns: QRCode detected
        */
        class func qrCodeFromImage(qrImage:UIImage) -> String {
            let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy:CIDetectorAccuracyHigh])
            guard let ciImage = qrImage.ciImage() else {
                return ""
            }
            guard let feature = detector.featuresInImage(ciImage).last as? CIQRCodeFeature else {
                return ""
            }
            return feature.messageString
        }
    }
    

    关于ios - 无法从静态图像检测到QR码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34205773/

    相关文章:

    ios - UI 测试手动通过,自动运行时失败

    ios - 如何复制self.title?

    ios - Cordova 中是否有关于应用程序即将卸载或更新的事件?

    ios - AFNetworking iOS

    ios - 不能替换所有出现的引号 (")

    ios - 推送通知已发送到苹果 apns 服务器,但设备未收到任何信息

    ios - CLGeocoder 从给定位置反向地理编码

    ios - UIImageView 的深层复制没有复制 iOS 中应用于原始 View 的所有委托(delegate)和手势

    ios - Swift中使用自定义分段控件时CollectionCell数据显示问题

    ios - 将 Storyboard View 添加为 subview 时发送到已释放实例的消息