二维码介绍:

 


二维码(QR(Quick Response)code),又称二维条码,最早起源于日本。


 


它是用特定的几何图形按一定规律在平面(二维方向)上分布的黑白相间的图形,是所有信息数据的一把钥匙。


 


二维码是一种比一维码更高级的条码格式。一维码只能在一个方向(一般是水平方向)上表达信息,


 


而二维码在水平和垂直方向都可以存储信息。一维码只能由数字和字母组成,而二维码能存储汉字、数字和图片等信息,


 


因此二维码的应用领域要广得多。


二维码需求:


开发一款二维码扫描插件,具有扫描大众二维码的能力,能够识别二维码当中包含的网页链接以及文本信息。对网页链接跳转safari浏览器(但是对自己公司的连接要求在app内部内置浏览器)。对扫描的内容弹出提示框。并且让别的app扫描我们自己的二维码的时候要跳转到appstore 下载。

需求确认:


 


二维码算法自己写?不现实。开发周期太长,那就使用第三方那个库QR(Quick Response)code  ZBarSDK,读取信息的时候首先应该判断是不是网页连接,考虑用正则表达式对扫描的结果进行过滤。对于内置 浏览器使用webview控件。弹出提示框使用UIAlertView太麻烦,而且不好用,所以采用开源第三方库BlockAlertActionSheet,BlockAlertActionSheet使用block做的一款具有提示功能类似UIAlertView  UIActionSheet等控件。很强大,很实用。

 


二维码开发:


 


首先在github上下载ZBar SDK


 


地址https://github.com/bmorton/ZBarSDK



下载BlockAlertActionSheet,


 


新建一个test工程。在Main》storyboard上拖放一个button点击button开始扫描。


 


为button连接插座事件。

- (IBAction)btnClicked:(id)sender{ 


  }

 


将ZBarSDK包含在项目工程当中。添加库:QuartzCore.framework ,CoreVideo.framework ,CoreMedia.framework,libiconv.dylib,CoreGraphics.framework。


将BlockAlertActionSheet包含在项目工程当中


 

在 WSQViewController.h中引入头文件。


#import "ZBarSDK.h" 

 

    

 

  #import "BlockAlertView.h"

 


遵循协议 ZBarReaderDelegate,


 

#pragma mark - ZBarReaderDelegate<UIImagePickerControllerDelegate> 

 

    

 

  - (void) readerControllerDidFailToRead: (ZBarReaderController*) reader 

 

    

 

      withRetry: (BOOL) retry 

 

    

 

      { 

 

    

 

      } 

 

    

 

       

 

    

 

      //二维码 

 

    

 

  - (void) imagePickerController: (UIImagePickerController*) reader 

 

    

 

      didFinishPickingMediaWithInfo: (NSDictionary*) info 

 

    

 

      { 

 

    

 

     

 

    

 

  }

 


 


 


定义实例变量:


 

ZBarReaderViewController *reader; 

 

    

 

  UIView* line; //二维码扫描线。 

 

    

 

  BOOL isBottom; 

 

    

 

  NSTimer* lineTimer;//二维码扫描线计时器。

 


自定义二维码扫描界面,(想法是这样的,先把reader原来的界面全部清空,然后自定义界面,因为ZBarSDK是分装好的静态库,)


 


 

-(void)setOverlayStyle:(ZBarReaderViewController *)reader_{ 

 

    

 

  for (UIView *temp in [reader_.view subviews]){ 

 

    

 

  for (UIButton* btn in [temp subviews]) { 

 

    

 

  if ([btn isKindOfClass:[UIButton class]]) { 

 

    

 

  [btn removeFromSuperview]; 

 

    

 

  } 

 

    

 

  } 

 

    

 

  //去掉toolbar 

 

    

 

  for (UIToolbar* tool in [temp subviews]) { 

 

    

 

  if ([tool isKindOfClass:[UIToolbar class]]) { 

 

    

 

  [tool setHidden:YES]; 

 

    

 

  [tool removeFromSuperview]; 

 

    

 

  } 

 

    

 

  } 

 

    

 

  isBottom = NO; 

 

    

 

  //扫描线 

 

    

 

    

 

    

 

  line = [[UIView alloc] initWithFrame:CGRectMake(40, 105, 240, 2)]; 

 

    

 

  line.backgroundColor = [UIColor greenColor]; 

 

    

 

  [reader_.view addSubview:line]; 

 

    

 

    

 

    

 

  lineTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(moveLine) userInfo:nil repeats:YES]; 

 

    

 

  [lineTimer fire]; 

 

    

 

    

 

    

 

  UIImage *scanningBg = [UIImage imageNamed:@"scanning-568h.png"]; 

 

    

 

    

 

    

 

  CGSize size  = [UIScreen mainScreen].bounds.size; 

 

    

 

  UIImageView *scanningView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)]; 

 

    

 

  scanningView.image = scanningBg; 

 

    

 

  [reader_.view addSubview:scanningView]; 

 

    

 

    

 

    

 

    

 

    

 

  //用于取消操作的button 

 

    

 

  UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

 

    

 

  UIImage *bimage = [UIImage imageNamed:@"yellowButton.png"]; 

 

    

 

  //[cancelButton setBackgroundImage:bimage forState:UIControlStateDisabled]; 

 

    

 

  [cancelButton setBackgroundColor:[UIColor whiteColor]]; 

 

    

 

  [cancelButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; 

 

    

 

  [cancelButton setFrame:CGRectMake(20, size.height - 84, 280, 40)]; 

 

    

 

  [cancelButton setTitle:@"取消" forState:UIControlStateNormal]; 

 

    

 

  [cancelButton.titleLabel setFont:[UIFont boldSystemFontOfSize:20]]; 

 

    

 

  [cancelButton addTarget:self action:@selector(dismissOverlayView:)forControlEvents:UIControlEventTouchUpInside]; 

 

    

 

    

 

    

 

  [reader_.view addSubview:cancelButton]; 

 

    

 

    

 

    

 

  } 

 

    

 

  } 

 

    

 

    

 

    

 

  //屏幕移动扫描线。 

 

    

 

  -(void)moveLine{ 

 

    

 

  CGRect lineFrame = line.frame; 

 

    

 

  CGFloat y = lineFrame.origin.y; 

 

    

 

  if (!isBottom) { 

 

    

 

  isBottom = YES; 

 

    

 

  y=y+245.0; 

 

    

 

  lineFrame.origin.y = y; 

 

    

 

  [UIView animateWithDuration:1.5 animations:^{ 

 

    

 

  line.frame = lineFrame; 

 

    

 

  }]; 

 

    

 

  }else if(isBottom){ 

 

    

 

  isBottom = NO; 

 

    

 

  y = y -245; 

 

    

 

  lineFrame.origin.y = y; 

 

    

 

  [UIView animateWithDuration:1.5 animations:^{ 

 

    

 

  line.frame = lineFrame; 

 

    

 

  }]; 

 

    

 

  } 

 

    

 

  } 

 

    

 

    

 

    

 

  // 点击cancel  button事件 

 

    

 

  - (void)dismissOverlayView:(id)sender{ 

 

    

 

  [lineTimer invalidate]; 

 

    

 

  [reader dismissModalViewControllerAnimated:YES]; 

 

    

 

  }