我試圖從UIImage中使用CIDetector檢測QRCode以便從中提取messageString。CIDetector沒有檢測到QRCode
我有一個函數,基本上接收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
難道我做錯了什麼?
在此先感謝
...我已經測試上面的代碼.....特徵對象返回的數組大小0 .....我錯過了什麼? – Cloy
我檢查了代碼並將其更新到新版本的swift,它在我的項目中工作正常。看看這篇文章,也許這將有助於[http://stackoverflow.com/questions/39009889/xcode-8-creating-an-image-format-with-an-unknown-type-是一個錯誤) – Arashk
我還得到了一個大小爲0的數組。將上面的代碼更改爲'CIDetector.init(...)'而不是'CIDetector(...)'解決了問題,現在CIDetector讀取QRCodes正好。 –