#import "ViewController.h"
#import "ZBarSDK/Headers/ZBarSDK/ZBarSDK.h"
@interface ViewController ()<ZBarReaderDelegate>
@property (weak, nonatomic) IBOutlet UILabel *showLabel;
@property (weak, nonatomic) IBOutlet UIImageView *showImgView;
- (IBAction)scanQRImage:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)scanQRImage:(id)sender {
// 实例化扫描控制器
ZBarReaderViewController *readVC = [[ZBarReaderViewController alloc] init];
// 设置代理
readVC.readerDelegate=self;
// 获取控制器中的扫描器对象
ZBarImageScanner*scanner = readVC.scanner;
// 设置扫描器识别的图片类型
[scannersetSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];
// 跳转至扫描控制器
[self presentViewController:readVC animated:YES completion:nil];
}
#pragma mark ---- ZBarReaderDelegate -----
// 获取到多媒体信息(图片、二维码、视频)后回调的方法
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionaryid> *)info {
// 隐藏当前的扫描控制器视图
[pickerdismissViewControllerAnimated:YES completion:nil];
/*
获取扫描到的图片数据
*/
// 获取扫描到的图片数据
UIImage*img = [infoobjectForKey:UIImagePickerControllerOriginalImage];
// 将图片显示在图像视图上
self.showImgView.image= img;
/*
获取扫描得到的二维码图片信息
*/
// 得到扫描到的图片信息数据枚举
id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];
// 获取集合中的信息
ZBarSymbol*symbol =nil;
for(symbolinresults) {
break;
}
if(symbol ==nil) {
NSLog(@"扫描失败");
return;
}
// 如果不为空,获取扫描到的结果
NSString*resStr = symbol.data;
// 把数据展示在lable上
self.showLabel.text= resStr;
// 判断是否是一个网址字符串,是的话做app跳转
if([symbol.datahasPrefix:@"http://"] || [symbol.datahasPrefix:@"https://"]) {
//
NSURL*url = [NSURLURLWithString:symbol.data];
[[UIApplication sharedApplication] openURL:url options:nil completionHandler:nil];
}
}