//首先下载一个文件ZBarSDK,之后导入文件到工程,然后导入框架如下
//托控件如下
//导入头文件
#import "ZBarSDK.h"
//添加代理
@interface ViewController () <ZBarReaderDelegate, UIAlertViewDelegate>
//拖拽的imageview控件
@property (weak, nonatomic) IBOutlet UIImageView *myImgaeView;
//拖拽的label控件
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
//提示框
@property (nonatomic, strong)UIAlertView *myAlertView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//扫描按钮方法的实现
- (IBAction)saomiao:(id)sender {
// 弹出扫描界面
ZBarReaderViewController *readerVC = [ZBarReaderViewController new];
readerVC.readerDelegate = self;
//开始扫描图片
ZBarImageScanner *sanner = readerVC.scanner;
[sanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];
//展示图片
readerVC.showsZBarControls = YES;
[self presentViewController:readerVC animated:YES completion:^{
}];
}
//使用图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//获取结果
id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];
ZBarSymbol *symbol;
//搜寻结果
for (symbol in results) {
break;
}
//设置图片
self.myImgaeView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:^{
}];
//判断结果是二维码还是条形码
if ([[symbol.data substringToIndex:4] isEqualToString:@"http"]) {
self.myAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:symbol.data delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"前往", nil];
[self.myAlertView show];
} else {
self.myAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:symbol.data delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[self.myAlertView show];
}
self.myLabel.text = symbol.data;
}
//前往按钮方法的实现
- (IBAction)go:(id)sender {
}
在真机中测试扫描二维码
本文介绍如何在iOS应用中集成ZBarSDK进行二维码和条形码的扫描,并展示了如何解析扫描结果及进行相应操作。
1192

被折叠的 条评论
为什么被折叠?



