iOS中如何使用ZBar扫描二维码实现自定义扫描界面功能
这篇文章主要介绍了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扫描二维码实现自定义扫描界面功能”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!
分享标题:iOS中如何使用ZBar扫描二维码实现自定义扫描界面功能
文章链接:http://ybzwz.com/article/jphhgg.html