iOS开发中 二维码扫描
前言
在移动开发中二维码扫描这种事情越来越常见了,在iOS中我选择了用ZBar这个第三方来实现。
原因有三:好用,好用,好用。
好了来点正经的干货吧。首先献上下载地址:快来点我
然后你就可以把整个 ZBar文件夹拉到你的项目的Lib之类的目录下面了。
如果用pod,就直接 pod 'ZBarSDK', '~> 1.3.1' , 那么你就可以越过下面那个添加库类的步骤了。
ZBar的使用给我们俩种选择,一种是自定义一个二维码视图,另一种是用他们自带的二维码视图(奇丑无比)。
添加库类
如下添加这些库类。
ZBar库类自定义二维码视图
一般都是给一个按钮绑定一个点击事件,然后当按钮点击的时候 就会调用二维码扫描,然后扫描成功就返回。
就像下面这个demo截图一样。
然后就是正题了,我们首先要#import "ZBarSDK.h"
.然后自定义的话 就要遵循这个代理<ZBarReaderViewDelegate>
然后就可以在点击的回调函数中设置如下:
// Do any additional setup after loading the view.
//初始化照相机窗口 这个readview 就是要美化的view
ZBarReaderView *readview = [ZBarReaderView new];
//自定义大小
readview.frame = CGRectMake(100, 100, 300, 300);
//自定义添加相关指示.........发挥各自的APP的想象力
//此处省略美化10000行代码...................
//………………………..
// 好进入正题—— 接着设置好代理
readview.readerDelegate = self;
//将其照相机拍摄视图添加到要显示的视图上
[self.view addSubview:readview];
//二维码/条形码识别设置
ZBarImageScanner *scanner = readview.scanner;
[scanner setSymbology: ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
//启动,必须启动后,手机摄影头拍摄的即时图像菜可以显示在readview上
[readview start];
还要实现如下delegate方法,监听扫描成功回调:
-(void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
{
ZBarSymbol *symbol = nil;
for (symbol in symbols)
{
break;
}
//扫描成功,让readerView消失
[readerView removeFromSuperview];
//这个是二维码得到的数据
NSString *data = symbol.data;
//数据处理
//.......
}
自带的二维码视图
和上面不同的是,这次我们要遵从的delegate变了是<ZBarReaderDelegate>
.
然后再cilck方法里实现以下代码:
//初始化相机控制器
ZBarReaderViewController *reader = [ZBarReaderViewController new];
//设置代理
reader.readerDelegate = self;
//基本适配
reader.supportedOrientationsMask = ZBarOrientationMaskAll;
//二维码/条形码识别设置
ZBarImageScanner *scanner = reader.scanner;
[scanner setSymbology: ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
//弹出系统照相机,全屏拍摄
[self presentModalViewController: reader animated: YES];
然后实现以下的delegate.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
id<NSFastEnumeration> symbols = [info objectForKey:ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for (symbol in symbols)
{
break;
}
[picker dismissModalViewControllerAnimated:YES];
//数据
NSString *data = symbol.data;
}
我自定义的 二维码View
- (void)setOverlayPickerView:(ZBarReaderView *)reader
{
//清除原有控件
for (UIView *temp in [reader subviews]) {
for (UIButton *button in [temp subviews]) {
if ([button isKindOfClass:[UIButton class]]) {
[button removeFromSuperview];
}
}
for (UIToolbar *toolbar in [temp subviews]) {
if ([toolbar isKindOfClass:[UIToolbar class]]) {
[toolbar setHidden:YES];
[toolbar removeFromSuperview];
}
}
}
/*
//画中间的基准线 可有可无 个人爱好
UIView* line = [[UIView alloc] initWithFrame:CGRectMake(40, 220, 240, 1)];
line.backgroundColor = [UIColor redColor];
[reader addSubview:line];
*/
//最上部view
UIView* upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
upView.alpha = 0.3;
upView.backgroundColor = [UIColor blackColor];
[reader addSubview:upView];
//左侧的view
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 80, 20, 280)];
leftView.alpha = 0.3;
leftView.backgroundColor = [UIColor blackColor];
[reader addSubview:leftView];
//右侧的view
UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(300, 80, 20, 280)];
rightView.alpha = 0.3;
rightView.backgroundColor = [UIColor blackColor];
[reader addSubview:rightView];
//底部view
UIView * downView = [[UIView alloc] initWithFrame:CGRectMake(0, 360, 320, 120)];
downView.alpha = 0.3;
downView.backgroundColor = [UIColor blackColor];
[reader addSubview:downView];
//用于说明的label
UILabel * labIntroudction= [[UILabel alloc] init];
labIntroudction.backgroundColor = [UIColor clearColor];
labIntroudction.frame=CGRectMake((ScreenW - 280)*0.5,8, 280, 40);
labIntroudction.numberOfLines=2;
labIntroudction.textColor=[UIColor whiteColor];
labIntroudction.font = Font(15);
labIntroudction.text=@"将二维码图像置于矩形方框内";
labIntroudction.textAlignment = NSTextAlignmentCenter;
[downView addSubview:labIntroudction];
/*
//用于取消操作的button 可有可无
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cancelButton.alpha = 0.4;
[cancelButton setFrame:CGRectMake(20, 390, 280, 40)];
[cancelButton setTitle:@"将二维码图像置于矩形方框内" forState:UIControlStateNormal];
[cancelButton.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[cancelButton addTarget:self action:@selector(dismissOverlayView:)forControlEvents:UIControlEventTouchUpInside];
[reader addSubview:cancelButton];
*/
}
总结
俩种方式都挺简单的,建议用自定义的,因为灵活 漂亮。
网友评论