gpt4 book ai didi

swift - 将 UIImage 转换为 CIImage 时,UIImage 的 QR 码检测失败

转载 作者:行者123 更新时间:2023-11-30 11:15:32 29 4
gpt4 key购买 nike

我正在尝试解码二维码以获取其内容。以下是它的代码:

func detectQRCode(_ recImage: UIImage?) -> [CIFeature]?
{
if let img = recImage,
let ciImage = CIImage.init(image: img)
{
var options: [String: Any]
let context = CIContext()
options = [CIDetectorAccuracy: CIDetectorAccuracyHigh]
let qrDetector = CIDetector(ofType: CIDetectorTypeQRCode, context: context, options: options)
if ciImage.properties.keys.contains((kCGImagePropertyOrientation as String))
{
options = [CIDetectorImageOrientation: ciImage.properties[(kCGImagePropertyOrientation as String)] ?? 1]
}
else
{
options = [CIDetectorImageOrientation: 1]
}
let features = qrDetector?.features(in: ciImage, options: options)
return features
}
return nil
}

if let 语句在 let ciImage = CIImage.init(image: img) 处失败。请提出建议。

最佳答案

请尝试从 UIImage 的属性中获取 ciImage,例如 -

if let ciImage = recImage?.ciImage

更新后的代码应如下所示 -

func detectQRCode(_ recImage: UIImage?) -> [CIFeature]?
{
if let ciImage = recImage?.ciImage
{
var options: [String: Any]
let context = CIContext()
options = [CIDetectorAccuracy: CIDetectorAccuracyHigh]
let qrDetector = CIDetector(ofType: CIDetectorTypeQRCode, context: context, options: options)
if ciImage.properties.keys.contains((kCGImagePropertyOrientation as String))
{
options = [CIDetectorImageOrientation: ciImage.properties[(kCGImagePropertyOrientation as String)] ?? 1]
}
else
{
options = [CIDetectorImageOrientation: 1]
}
let features = qrDetector?.features(in: ciImage, options: options)
return features
}
return nil
}

关于swift - 将 UIImage 转换为 CIImage 时,UIImage 的 QR 码检测失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51784748/

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