
CIDetector可让您在图像中找到特定的图案,如面部,微笑,眼睛,或者在我们的案例中:QRCodes.
以下是在Objective-C中从UIImage检测QRCode的代码:
-(NSArray *)detectQRCode:(UIImage *) image{ @autoreleasepool { NSLog(@"%@ :: %@",NsstringFromClass([self class]),NsstringFromSelector(_cmd)); NSCAssert(image != nil,@"**Assertion Error** detectQRCode : image is nil"); CIImage* ciImage = UIImage.CIImage; // assuming underlying data is a CIImage //CIImage* ciImage = [CIImage initWithCGImage: UIImage.CGImage]; // to use if the underlying data is a CGImage NSDictionary* options; CIContext* context = [CIContext context]; options = @{ CIDetectorAccuracy : CIDetectorAccuracyHigh }; // Slow but thorough //options = @{ CIDetectorAccuracy : CIDetectorAccuracyLow}; // Fast but superficial CIDetector* qrDetector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:context options:options]; if ([[ciImage propertIEs] valueForKey:(Nsstring*) kCGImagePropertyOrIEntation] == nil) { options = @{ CIDetectorImageOrIEntation : @1}; } else { options = @{ CIDetectorImageOrIEntation : [[ciImage propertIEs] valueForKey:(Nsstring*) kCGImagePropertyOrIEntation]}; } NSArray * features = [qrDetector featuresInImage:ciImage options:options]; return features; }} 如果存在并检测到QRCode,则返回的NSArray *将包含CIFeature *.如果没有QRCode,NSArray *将为零.如果QRCode解码失败,NSArray *将没有元素.
要获取编码的字符串:
if (features != nil && features.count > 0) { for (CiqrCodeFeature* qrFeature in features) { NSLog(@"QRFeature.messageString : %@ ",qrFeature.messageString); }} 在@ Duncan-C的答案中,您可以提取QRCode角并在图像上绘制QRCode的封闭边界框.
注意 :
在iOS10.0 beta 6下,当使用来自cameraSampleBuffer的图像时,调用[qrDetector featuresInImage:ciImage选项:选项]会记录此内部警告(它运行顺利但是垃圾邮件控制台带有此消息,我找不到办法现在摆脱它):
Finalizing CVPixelBuffer 0x170133e20 while lock count is 1.
资源 :
Apple Dev API Reference – CIDetector
Apple Dev API Programming guide – Face detection
总结以上是内存溢出为你收集整理的ios – 如何从静态图像中读取QR码全部内容,希望文章能够帮你解决ios – 如何从静态图像中读取QR码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)