Reputation: 255
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
Upvotes: 1
Views: 2233
Reputation: 627
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
Upvotes: 3