Core Image Detector(CIDetector) is not detecting Q

2019-09-12 16:25发布

问题:

I am trying to give the user of my app the ability to take an image from their library and scan the QRCode from it. Here is the relevant code:

This is the function that returns from the Image Picker with the selected image.

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
    let thisImage:CIImage? = CIImage(image: image)
    let code = performQRCodeDetection(thisImage!)

    self.dismissViewControllerAnimated(true, completion: nil)

    self.performSegueWithIdentifier("goBackSegue", sender: code)


}

Inside that function I call this function to do the QRCode scanning:

func performQRCodeDetection(image: CIImage) -> String {
    var decode = ""
    let options = [CIDetectorAccuracy: CIDetectorAccuracyHigh]
    let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: nil, options: options)

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

The variable features always comes back empty. The image definitely has a QRCode in it. I took the image from all orientations and sizes. I cannot get any results.

Any thoughts?

Thanks!

回答1:

the issue is on photo quality in the photo album. if you put a qr image file from bundle replacing "image" e.g.. [UIImage imageNamed:@"foobar.png"], the qr result will appear. By the way, I tried may photos, no luck to get it works.