简体   繁体   English

CIDetector没有检测到QRCodes

[英]CIDetector not detecting QRCodes

I'm trying to detect a QRCode from an UIImage using the CIDetector in order to extract the messageString from it. 我正在尝试使用CIDetector从UIImage中检测QRCode,以便从中提取messageString

I have a function that basically receives an UIImage as input and returns the decoded messageString . 我有一个函数,基本上接收UIImage作为输入并返回解码的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). 我传递给这个函数的UIImage是使用PHAsset,PHAssetCollection,PHImageRequestOptions等从用户Gallery中提取的。请求是同步的,我得到的所有图像质量都很好(我试图在UIImageView中显示图像)只是为了确保图像可读)。

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: 获取图像后,我想检测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
}

I found this ( Core Image Detector(CIDetector) is not detecting QRCodes ) and other similar questions but none of them seems to work. 我发现这( 核心图像探测器(CIDetector)没有检测到QRCodes )和其他类似的问题,但似乎没有一个工作。

This was tutorial I followed https://www.shinobicontrols.com/blog/ios8-day-by-day-day-13-coreimage-detectors 这是我遵循的教程https://www.shinobicontrols.com/blog/ios8-day-by-day-day-13-coreimage-detectors

Am I doing something wrong? 难道我做错了什么?

Thanks in advance 提前致谢

The problem is you are passing CIImage in your function 问题是你在函数中传递CIImage

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. 删除这一行,它应该没问题我正在为自己写一个QRCode,如果你感兴趣,你可以检查出来。

// 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 更新到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 希望这是有帮助的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM