码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-01-17 13:57:12      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

之前在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];
    
    
}


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

标签:

原文地址:http://blog.csdn.net/zz_mm/article/details/42804671

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!