1

I'm trying to detect a QRCode from an UIImage using the CIDetector in order to extract the messageString from it.

I have a function that basically receives an UIImage as input and returns the decoded messageString.

The UIImage that I pass to this function is extracted from the user Gallery using the PHAsset, PHAssetCollection, PHImageRequestOptions etc. The request is made synchronously and all the images I get are with good quality (I've tried to present the image in an UIImageView just to make sure the image is readable).

Here you can see a snippet of the code to request the images:

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!)
                })

After getting the images I want to detect the QRCodes and I'm using the following code:

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
}

I found this (Core Image Detector(CIDetector) is not detecting QRCodes) and other similar questions but none of them seems to work.

This was tutorial I followed https://www.shinobicontrols.com/blog/ios8-day-by-day-day-13-coreimage-detectors

Am I doing something wrong?

Thanks in advance

Community
  • 1
  • 1
mvcs
  • 255
  • 2
  • 11

1 Answers1

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

Arashk
  • 617
  • 5
  • 16
  • 1
    ...I have tested the above code .....features object return an array of size 0.....Am i missing something? – Cloy Apr 29 '17 at 06:48
  • I checked the code and update it to newer version of swift and it is works fine in my project. Look at this post maybe this will help [http://stackoverflow.com](http://stackoverflow.com/questions/39009889/xcode-8-creating-an-image-format-with-an-unknown-type-is-an-error) – Arashk Apr 30 '17 at 17:00
  • I was also getting an array of size 0. Changing the above code to `CIDetector.init(...)` instead of `CIDetector(...)` solved the problem and now CIDetector reads QRCodes just fine. – Luiz Dias Sep 23 '17 at 22:42