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;
}