iOS Zbar获取相册本地图片扫描二维码

Zbar获取相册本地图片扫描二维码

iOS原生的二维码扫描好像不能从相册中获取图片进行扫描,查了很多都没找到相关的代码,也有人说不能扫描图片。所以换用Zbar来扫描图片了。

直接上代码

#pragma mark - 获取相册照片
/*
 扫描二维码部分:
     导入ZBarSDK文件并引入一下框架
     AVFoundation.framework
     CoreMedia.framework
     CoreVideo.framework
     QuartzCore.framework
     libiconv.dylib
     引入头文件#import “ZBarSDK.h” 即可使用
     当找到二维码时,会执行代理方法
     - (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info
     最后读取并显示了条形码的图片和内容。
 */
- (IBAction)PhotoAction:(id)sender {

    imagePicker = [ZBarReaderController new];
    imagePicker.delegate = self;

    imagePicker.allowsEditing = YES;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [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) {
        NSLog(@"symbol:%@", symbol);
        break;
    }

    [imagePicker dismissViewControllerAnimated:YES completion:nil];


    //二维码字符串
    NSString *QRCodeString =  symbol.data;

    ScanResultViewController *scanResultVC = [[ScanResultViewController alloc]init];

    //将扫描到的文字显示
    scanResultVC.textStr = QRCodeString;

    //跳转页面
    [self.navigationController pushViewController:scanResultVC animated:YES];

}



#pragma mark - 相册图片未检测到条码
/*
 @protocol ZBarReaderDelegate <UIImagePickerControllerDelegate>
 @optional

 // called when no barcode is found in an image selected by the user.
 // if retry is NO, the delegate *must* dismiss the controller
 - (void) readerControllerDidFailToRead: (ZBarReaderController*) reader
 withRetry: (BOOL) retry;

 @end

 */

-(void)readerControllerDidFailToRead:(ZBarReaderController *)reader withRetry:(BOOL)retry{

    if (retry) {
        //retry == 1 选择图片为非二维码。

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"臣妾找不到啊_(:з)∠)_" delegate:self cancelButtonTitle:nil otherButtonTitles:@"朕知道了", nil];

        [alert show];

        [self dismissViewControllerAnimated:YES completion:nil];

    }
        return;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值