欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-21 08:22:46
之前在android中使用过zxing识别二维码,zxing也有对应的ios版本,经过了解,zbar也是一个常用的二维码识别软件,并分别提供了ios和android的sdk...

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

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

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

#pragma mark 初始化扫描
- (void)initscan
{
  readview = [zbarreaderview new];
  readview.backgroundcolor = [uicolor clearcolor];
  readview.frame = cgrectmake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
  readview.readerdelegate = self;
  readview.allowspinchzoom = yes;//使用手势变焦
  readview.trackingcolor = [uicolor redcolor];
  readview.showsfps = no;// 显示帧率 yes 显示 no 不显示
  //readview.scancrop = cgrectmake(0, 0, 1, 1);//将被扫描的图像的区域
  uiimage *hbimage=[uiimage imagenamed:@"pick_bg.png"];
  scanzomeback=[[uiimageview alloc] initwithimage:hbimage];
  //添加一个背景图片
  cgrect mimagerect=cgrectmake((readview.frame.size.width-200)/2.0, (readview.frame.size.height-200)/2.0, 200, 200);
  [scanzomeback setframe:mimagerect];
  readview.scancrop = [self getscancrop:mimagerect readerviewbounds:readview.bounds];//将被扫描的图像的区域
  [readview addsubview:scanzomeback];
  [readview addsubview:readlineview];
  [self.view addsubview:readview];
  [readview start];
}
#pragma mark 获取扫描区域
-(cgrect)getscancrop:(cgrect)rect readerviewbounds:(cgrect)readerviewbounds
{
  cgfloat x,y,width,height;
  x = rect.origin.x / readerviewbounds.size.width;
  y = rect.origin.y / readerviewbounds.size.height;
  width = rect.size.width / readerviewbounds.size.width;
  height = rect.size.height / readerviewbounds.size.height;
  return cgrectmake(x, y, width, height);
}
#pragma mark 扫描动画
-(void)loopdrawline
{
  cgrect rect = cgrectmake(scanzomeback.frame.origin.x, scanzomeback.frame.origin.y, scanzomeback.frame.size.width, 2);
  if (readlineview) {
    [readlineview removefromsuperview];
  }
  readlineview = [[uiimageview alloc] initwithframe:rect];
  [readlineview setimage:[uiimage imagenamed:@"line.png"]];
  [uiview animatewithduration:3.0
             delay: 0.0
            options: uiviewanimationoptioncurveeasein
           animations:^{
             //修改fream的代码写在这里
             readlineview.frame =cgrectmake(scanzomeback.frame.origin.x, scanzomeback.frame.origin.y+scanzomeback.frame.size.height, scanzomeback.frame.size.width, 2);
             [readlineview setanimationrepeatcount:0];
           }
           completion:^(bool finished){
             if (!is_anmotion) {
               [self loopdrawline];
             }
           }];
  [readview addsubview:readlineview];
}
#pragma mark 获取扫描结果
- (void)readerview:(zbarreaderview *)readerview didreadsymbols:(zbarsymbolset *)symbols fromimage:(uiimage *)image
{
  // 得到扫描的条码内容
  const zbar_symbol_t *symbol = zbar_symbol_set_first_symbol(symbols.zbarsymbolset);
  nsstring *symbolstr = [nsstring stringwithutf8string: zbar_symbol_get_data(symbol)];
  if (zbar_symbol_get_type(symbol) == zbar_qrcode) {
    // 是否qr二维码
  }
  for (zbarsymbol *symbol in symbols) {
    [stxtfield settext:symbol.data];
    break;
  }
  [readerview stop];
  [readerview removefromsuperview];
}

github地址:https://github.com/zbar/zbar

以上所述是小编给大家介绍的ios中使用zbar扫描二维码自定义扫描界面,希望对大家有所帮助

上一篇:

下一篇: