ios ZBar识别选中相册的图片二维码

1.识别选中的单张二维码图片

- (void)viewDidLoad {

   _photoBtn =[UIButton buttonWithType:UIButtonTypeCustom];

   _photoBtn.frame=CGRectMake(30, 30, 44, 44);
    [_photoBtn setEnabled:YES];
    [_photoBtn setTitle:@"" forState:UIControlStateNormal];
    [_photoBtn addTarget:self action:@selector(openPhoto) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_photoBtn];

}

- (void)openPhoto
{

    if (!self.picker) {
        self.picker = [[UIImagePickerController alloc] init];
        _picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        _picker.delegate = self;
        //设置选择后的图片可被编辑
        _picker.allowsEditing = YES;
    }
    [self presentViewController:_picker animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{
    
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {

        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        [self decodeImage:image];
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
// 处理扫描到的图
- (void)decodeImage:(UIImage *)image
{
    ZBarReaderController *reader=[ZBarReaderController new];
    CGImageRef photo_image=image.CGImage;
    ZBarSymbol *symbol = nil;
    for (symbol in [reader scanImage:photo_image])
        break;
    NSString *code=symbol.data;
    if (text!=nil) {
        CLog(@"扫描结果====:%@",code);
        [self saoCodeResult:code];
    }
}
-(void)saoCodeResult:(NSString)code{

nslog(@"处理码图");

}

2.识别相册中的任意一张二维码图(不显示图片大图)

- (void)viewDidLoad {

   _photoBtn =[UIButton buttonWithType:UIButtonTypeCustom];

   _photoBtn.frame=CGRectMake(30, 30, 44, 44);
    [_photoBtn setEnabled:YES];
    [_photoBtn setTitle:@"" forState:UIControlStateNormal];
    [_photoBtn addTarget:self action:@selector(openPhoto) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_photoBtn];

}

- (void)openPhoto
{

  
    _reader = [ZBarReaderController new];
     _reader.showsHelpOnFail = NO;
     _reader.readerDelegate = self;
    _reader.allowsEditing=NO;
    _reader.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:_reader animated:YES completion:nil];


}

- (void) readerControllerDidFailToRead: (ZBarReaderController*) reader
                             withRetry: (BOOL) retry{
    CLog(@"扫码失败了");
    
    if (retry==NO) {
        [self dismissViewControllerAnimated:YES completion:nil ];
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值