解决从相册中识别二维码图片失败的问题

CIDetector类读取相册中的二维码遇到的问题是手机截屏的图片可以读取成功,手机拍照的图片取出成功率低。并且不支持读取条形码。

解决办法:读取相册中的二维码/条形码,使用ZBarSDK,下面介绍一下怎么在工程中引入ZBarSDK,因为我的Demo工程没有使用CocoaPods去管理第三方库,所以介绍手动将ZBarSDK导入工程并配置


步骤一.在工程中建一个Frameworks文件夹将ZBarSDK文件夹导入,如图



步骤二.在工程中添加依赖库,如图:


步骤三.导入头文件并调用对应方法。

#import "ZBarSDK.h"

/**
 *  获取相册中的二维码图片
 */
- (void)getQRcodeImage {
    
    // ZBarsSDK 初始化


    ZBarReaderController *imagePicker = [ZBarReaderController new];
    
    imagePicker.showsHelpOnFail = NO; // 禁止显示读取失败页面
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    [self presentViewController:imagePicker animated:YES completion:nil];
}


/**
 *  选中图片的回调
 */
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];
    
    ZBarSymbol *symbol = nil;
    
    for(symbol in results) {
        
        break;
    }
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
        //二维码字符串
        NSString *QRCodeString =  symbol.data;
        
        // 触发回调
        [self getScanDataString:QRCodeString];
        
}];
}


/**
 *  读取二维码/条码失败的回调
 */
-(void)readerControllerDidFailToRead:(ZBarReaderController *)reader withRetry:(BOOL)retry{
    
    if (retry) { //retry == 1 选择图片为非二维码。
        [self dismissViewControllerAnimated:YES completion:^{
            
            [self getScanDataString:@"未发现二维码/条码"];
        }];
        
    }
    return;
    
}



以上是我实际开发中找到的最快速解决读取相册中二维码失败的方法,也是对原文链接的精简版处理。希望对大家有帮助


分享ZBarSDK的下载地址

原文链接:http://blog.csdn.net/gaomingyangc/article/details/54017879



猜你喜欢

转载自blog.csdn.net/niumanxx/article/details/79300634
今日推荐