CIDetector没有检测到QRCodes

时间:2016-01-24 20:33:36

标签: ios swift uiimage ios9 qr-code

我尝试使用CIDetector从UIImage中检测QRCode,以便从中提取 messageString

我有一个基本上接收UIImage作为输入的函数,并返回解码的 messageString

我传递给此函数的UIImage是使用PHAsset,PHAssetCollection,PHImageRequestOptions等从用户库中提取的。请求是同步的,我得到的所有图像质量都很好(我试图呈现) UIImageView 中的图像只是为了确保图像可读。)

在这里,您可以看到请求图片的代码片段:

let _: PHImageRequestID = PHImageManager.defaultManager().requestImageForAsset(asset as! PHAsset, targetSize: size, contentMode: PHImageContentMode.AspectFit, options: options, resultHandler: { (image: UIImage?, info:[NSObject : AnyObject]?) -> Void in
                    images.append(image!)
                })

获取图像后,我想检测QRCodes,我使用以下代码:

func scanImage(image: CIImage) -> String {

    let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: [CIDetectorAccuracy:CIDetectorAccuracyHigh])
    var image = CIImage(image: uiimage)

    var decode = ""
    let features = detector.featuresInImage(image!)
    for feature in features as! [CIQRCodeFeature] {
        decode = feature.messageString
    }
    return decode
}

我发现了这个(Core Image Detector(CIDetector) is not detecting QRCodes)和其他类似的问题,但似乎都没有。

这是我跟着https://www.shinobicontrols.com/blog/ios8-day-by-day-day-13-coreimage-detectors

的教程

我做错了吗?

提前致谢

1 个答案:

答案 0 :(得分:3)

The problem is you are passing CIImage in your function

func scanImage(image: CIImage)

and then you add another image variable with undefined image in

var image = CIImage(image: uiimage)

remove this line and it should be ok I was writing a QRCode for myself and if you interested you can check it out.

// open photo library 
func loadGallery() {
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = .PhotoLibrary
    self.presentViewController(imagePicker,
        animated: true,
        completion: nil)
}

// read QRCode from selecting image
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let qrImg:UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage

    if #available(iOS 8.0, *) {
        let detector:CIDetector = CIDetector(ofType: CIDetectorTypeQRCode, context:nil, options:[CIDetectorAccuracy: CIDetectorAccuracyHigh])
        let ciimg: CIImage = CIImage(image: qrImg)!
        let features = detector.featuresInImage(ciimg)

        for feature in features as! [CIQRCodeFeature] {
            print(feature.messageString)
        }


    } else {
        // Fallback on earlier versions
    }

    dismissViewControllerAnimated(true, completion: nil)
}

Update to Swift 3 xcode 8.1

// read QRCode from selecting image
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {


    let qrImg:UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage

    if #available(iOS 8.0, *) {
        let detector:CIDetector = CIDetector(ofType: CIDetectorTypeQRCode, context:nil, options:[CIDetectorAccuracy: CIDetectorAccuracyHigh])!
        let ciimg: CIImage = CIImage(image: qrImg)!
        let features = detector.features(in: ciimg)

        for feature in features as! [CIQRCodeFeature] {
            print(feature.messageString!)
        }


    } else {
        // Fallback on earlier versions
    }
    dismiss(animated: true, completion: nil)
}

Hope this is be helpfull