1.首先要导入CoreImage.framework
框架
2.引入<CoreImage/CoreImage.h>
头文件
#import <CoreImage/CoreImage.h>
从图片中识别二维码
-(NSArray \*)detectQRCode:(UIImage \*)qrcodeImage
{
NSDictionary \*opts=@{CIDetectorAccuracy:CIDetectorAccuracyHigh};
CIDetector \*detector=[CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:opts];
CIImage \*cimage=[CIImage imageWithCGImage:qrcodeImage.CGImage];
NSArray \*features=[detector featuresInImage:cimage];
return features;
}
3.使用方法如下
条码或二维码识别
- (void)viewDidLoad
{
[super viewDidLoad];
//带二维码或条码的照片
UIImage \*image=[UIImage imageNamed:@"code.png"];
NSArray \*features=[self detectQRCode:image];
//可能会存在多个二维码,这里只考虑一个的情况
if(features.count>0)
{
CIQRCodeFeature \*code=[features firstObject];
NSLog(@"识别结果:%@",code.messageString);
}
}
网友评论