gpt4 book ai didi

ios - CIDetector 未检测到 QRCode

转载 作者:搜寻专家 更新时间:2023-11-01 06:41:16 26 4
gpt4 key购买 nike

我正在尝试使用 CIDetector 从 UIImage 中检测 QRCode,以便从中提取 messageString

我有一个基本上接收 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

我做错了什么吗?

提前致谢

最佳答案

问题是你在函数中传递了 CIImage

func scanImage(image: CIImage)

然后你在

中添加另一个带有未定义图像的图像变量
var image = CIImage(image: uiimage)

去掉这一行应该就可以了我正在为自己写一个二维码,如果你有兴趣可以看看。

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

更新到 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)
}

希望对你有帮助

关于ios - CIDetector 未检测到 QRCode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34981112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com