- (void)viewDidLoad {
[super viewDidLoad];
NSString * file = [[NSBundle mainBundle] pathForResource:@"qrcode.png" ofType:nil];
CIImage * ciImage = [CIImage imageWithData:[NSData dataWithContentsOfFile:file]];
NSString *qr = [self stringFromCiImage:ciImage];
NSLog(@"qr: %@",qr);
}
- (NSString *)stringFromCiImage:(CIImage *)ciimage {
NSString *content = nil;
if (!ciimage) {
return nil;
}
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode
context:[CIContext contextWithOptions:nil]
options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];
NSArray *features = [detector featuresInImage:ciimage];
if (features.count) {
for (CIFeature *feature in features) {
if ([feature isKindOfClass:[CIQRCodeFeature class]]) {
content = ((CIQRCodeFeature *)feature).messageString;
break;
}
}
} else {
NSLog(@"未正常解析二维码图片, 请确保iphone5/5c以上的设备");
}
return content;
}
[super viewDidLoad];
NSString * file = [[NSBundle mainBundle] pathForResource:@"qrcode.png" ofType:nil];
CIImage * ciImage = [CIImage imageWithData:[NSData dataWithContentsOfFile:file]];
NSString *qr = [self stringFromCiImage:ciImage];
NSLog(@"qr: %@",qr);
}
- (NSString *)stringFromCiImage:(CIImage *)ciimage {
NSString *content = nil;
if (!ciimage) {
return nil;
}
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode
context:[CIContext contextWithOptions:nil]
options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];
NSArray *features = [detector featuresInImage:ciimage];
if (features.count) {
for (CIFeature *feature in features) {
if ([feature isKindOfClass:[CIQRCodeFeature class]]) {
content = ((CIQRCodeFeature *)feature).messageString;
break;
}
}
} else {
NSLog(@"未正常解析二维码图片, 请确保iphone5/5c以上的设备");
}
return content;
}