识别本地图片中的二维码

267 阅读1分钟

1. 简单说下需求 本地图片中包含二维码包含在本地相册中 从相册中选中该图片并识别到二维码中的有效信息

  1. 废话补多少贴一段有效的代码 选取本地相册中的图片方法这里就不再讲了
UIImage *pickImage = assets.firstObject;
/// 识别的核心类  CIDetectorTypeQRCode 表示自己具体识别的图片类型  CIDetectorAccuracyHigh 识别精度最高
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy: CIDetectorAccuracyHigh}]; 
// 获取选择图片中识别结果
// 拿到实例化的对象 开始执行识别方法
NSArray *features = [detector featuresInImage:[CIImage imageWithData:UIImagePNGRepresentation(pickImage)]];
if (features.count > 0) { // 这里说明识别到了有效信息

    CIQRCodeFeature *feature = features[0];
    NSString *stringValue = feature.messageString;
    DLog(@"识别的最终结果===%@",stringValue);    
} else {
    // 没有识别到二维码走这里处理自己的逻辑
    DLog(@"未识别的具体单号");
    [self showMsg:@"未识别到具体内容"];
}

这里试试识别的后半部分 我是拿到UIImage对象放到当前对象中直接识别的