您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍了iOS中如何使用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扫描二维码实现自定义扫描界面功能”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。