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];
}
{
_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 ];
}
}