iOS中使用ZBar扫描二维码自定义扫描界面

之前在Android中使用过ZXing识别二维码,ZXing也有对应的iOS版本,经过了解,ZBar也是一个常用的二维码识别软件,并分别提供了iOS和Android的SDK可供使用,最终我选择了ZBar进行二维码识别,它的注释清晰,便于使用。

ZBar为我们提供了两种使用方式,一种是直接调用ZBar提供的ZBarReaderViewController打开一个扫描界面,另一种方式是使用ZBar提供的可以嵌在其他视图中的ZBarReaderView,实际项目中我们更可能会使用第二种方式,这可以让我们对界面做更多的定制。

ZBar使用起来也非常简单,将ZBarSDK导入项目,在需要使用ZBar的文件中导入ZBarSDK.h头文件即可

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #pragma mark 初始化扫描  
  2. - (void)InitScan  
  3. {  
  4.     readview = [ZBarReaderView new];  
  5.     readview.backgroundColor = [UIColor clearColor];  
  6.     readview.frame = CGRectMake(00self.view.frame.size.widthself.view.frame.size.height);  
  7.     readview.readerDelegate = self;  
  8.     readview.allowsPinchZoom = YES;//使用手势变焦  
  9.     readview.trackingColor = [UIColor redColor];  
  10.     readview.showsFPS = NO;// 显示帧率  YES 显示  NO 不显示  
  11.     //readview.scanCrop = CGRectMake(0, 0, 1, 1);//将被扫描的图像的区域  
  12.       
  13.     UIImage *hbImage=[UIImage imageNamed:@"pick_bg.png"];  
  14.     scanZomeBack=[[UIImageView alloc] initWithImage:hbImage];  
  15.     //添加一个背景图片  
  16.     CGRect mImagerect=CGRectMake((readview.frame.size.width-200)/2.0, (readview.frame.size.height-200)/2.0200200);  
  17.     [scanZomeBack setFrame:mImagerect];  
  18.     readview.scanCrop = [self getScanCrop:mImagerect readerViewBounds:readview.bounds];//将被扫描的图像的区域  
  19.       
  20.     [readview addSubview:scanZomeBack];  
  21.     [readview addSubview:readLineView];  
  22.     [self.view addSubview:readview];  
  23.     [readview start];  
  24.       
  25. }  

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #pragma mark 获取扫描区域  
  2. -(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds  
  3. {  
  4.     CGFloat x,y,width,height;  
  5.       
  6.     x = rect.origin.x / readerViewBounds.size.width;  
  7.     y = rect.origin.y / readerViewBounds.size.height;  
  8.     width = rect.size.width / readerViewBounds.size.width;  
  9.     height = rect.size.height / readerViewBounds.size.height;  
  10.       
  11.     return CGRectMake(x, y, width, height);  
  12. }  

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #pragma mark 扫描动画  
  2. -(void)loopDrawLine  
  3. {  
  4.     CGRect  rect = CGRectMake(scanZomeBack.frame.origin.x, scanZomeBack.frame.origin.y, scanZomeBack.frame.size.width2);  
  5.     if (readLineView) {  
  6.         [readLineView removeFromSuperview];  
  7.     }  
  8.     readLineView = [[UIImageView alloc] initWithFrame:rect];  
  9.     [readLineView setImage:[UIImage imageNamed:@"line.png"]];  
  10.     [UIView animateWithDuration:3.0  
  11.                           delay0.0  
  12.                         options: UIViewAnimationOptionCurveEaseIn  
  13.                      animations:^{  
  14.                          //修改fream的代码写在这里  
  15.                          readLineView.frame =CGRectMake(scanZomeBack.frame.origin.x, scanZomeBack.frame.origin.y+scanZomeBack.frame.size.height, scanZomeBack.frame.size.width2);  
  16.                          [readLineView setAnimationRepeatCount:0];  
  17.                            
  18.                      }  
  19.                      completion:^(BOOL finished){  
  20.                          if (!is_Anmotion) {  
  21.                                
  22.                              [self loopDrawLine];  
  23.                          }  
  24.                            
  25.                      }];  
  26.       
  27.     [readview addSubview:readLineView];  
  28.       
  29. }  
  30. #pragma mark 获取扫描结果  
  31. - (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image  
  32. {  
  33.     // 得到扫描的条码内容  
  34.     const zbar_symbol_t *symbol = zbar_symbol_set_first_symbol(symbols.zbarSymbolSet);  
  35.     NSString *symbolStr = [NSString stringWithUTF8String: zbar_symbol_get_data(symbol)];  
  36.     if (zbar_symbol_get_type(symbol) == ZBAR_QRCODE) {  
  37.         // 是否QR二维码  
  38.     }  
  39.       
  40.     for (ZBarSymbol *symbol in symbols) {  
  41.         [sTxtField setText:symbol.data];  
  42.         break;  
  43.     }  
  44.       
  45.     [readerView stop];  
  46.     [readerView removeFromSuperview];  
  47.       
  48.       
  49. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值