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

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

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

[objc] view plain copy print ?
  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 print ?
  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 print ?
  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. }  

你可能感兴趣的:(iOS中使用ZBar扫描二维码自定义扫描界面)