1、选择图片
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imagePicker.delegate = self;
[viewController presentViewController:imagePicker animated:YES completion:nil];
2、识别图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage *image = info[UIImagePickerControllerOriginalImage];
CIImage *ciimage = [[CIImage alloc] initWithImage:image];
NSDictionary *options = @{CIDetectorAccuracy: CIDetectorAccuracyHigh};
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:options];
NSArray *features = [detector featuresInImage:ciimage];
NSString *message = nil;
for (CIFeature *item in features) {
if ([item isKindOfClass:[CIQRCodeFeature class]]) {
message = [(CIQRCodeFeature *)item messageString];
break;
}
}
if ([self.delegate respondsToSelector:@selector(recognitionString:)]) {
[self.delegate recognitionString:message];
}
}
参考:iOS开发教程之识别图片中二维码功能的实现
|