如何从静态图像中读取QR码 [英] How to read QR code from static image
问题描述
我知道您可以使用 AVFoundation
使用设备的相机扫描QR码。现在问题是,如何从静态 UIImage
对象中执行此操作?
I know that you can use AVFoundation
to scan a QR code using the device's camera. Now here comes the problem, how can I do this from an static UIImage
object?
推荐答案
iOS API提供CoreImage框架中的CIDetector类。
CIDetector让你在图像中找到特定的模式,比如面部,微笑,眼睛,或者在我们的例子中:QRCodes。
The iOS API provides the CIDetector class from CoreImage framework. CIDetector let you find specific patterns in images, like faces, smiles, eyes, or in our case : QRCodes.
这是检测QRCode的代码来自Objective-C中的UIImage:
Here is the code to detect a QRCode from an UIImage in Objective-C:
-(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 *
将是 nil
。如果QRCode解码失败, NSArray *
将没有元素。
The returned NSArray*
will contain CIFeature*
if a QRCode is present and detected. If there was no QRCode, the NSArray*
will be nil
. If the QRCode decoding fails, the NSArray*
will have no element.
获取编码的字符串:
if (features != nil && features.count > 0) {
for (CIQRCodeFeature* qrFeature in features) {
NSLog(@"QRFeature.messageString : %@ ", qrFeature.messageString);
}
}
在@ Duncan-C的回答中,你然后可以提取QRCode角并在图像上绘制QRCode的封闭边界框。
As in the answer of @Duncan-C, you can then extract QRCode corners and draw an enclosing bounding box of the QRCode on the image.
注意:
在iOS10.0下测试版6,调用 [qrDetector featuresInImage:ciImage选项:选项]
当使用来自cameraSampleBuffer的图像时,会记录此内部警告(它运行顺利,但会通过此消息向控制台发送垃圾邮件,我现在找不到摆脱它的方法):
Note :
Under iOS10.0 beta 6, the call to [qrDetector featuresInImage:ciImage options:options]
when using images coming from the cameraSampleBuffer logs this internal warning (it runs smoothly but spam the console with this message, and I could not find a way to get rid of it for now):
锁定计数为1时,最终确定CVPixelBuffer 0x170133e20。
Finalizing CVPixelBuffer 0x170133e20 while lock count is 1.
资料来源:
这篇关于如何从静态图像中读取QR码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!