时间:2021-05-20
之前在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扫描二维码自定义扫描界面,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
前言:对于二维码扫描我们使用的是开源框架Zxing或者Zbar,这里使用基于zxing的二维码扫描,类似支付宝网格扫描。二维码原理介绍: 二维码是用某种特定的
前面我们已经调到过怎么制作二维码,在我们能够生成二维码之后,如何对二维码进行扫描呢?在ios7之前,大部分应用中使用的二维码扫描是第三方的扫描框架,例如zxin
iOS11中可以直接使用相机扫描二维码了,该怎么扫描二维码呢?下面我们就来看看iOS11扫描二维码的方法。1、首先,打开屏幕上的【相机】2、将二维码放到镜头中3
怎么使用易信的扫一扫功能易信二维码扫描怎么使用易信的扫一扫功能易信二维码扫描怎么使用易信的扫一扫功能易信二维码扫描怎么使用易信的扫一扫功能易信二维码扫描怎么使用
微信怎么扫描二维码手机微信扫一扫全知道微信怎么扫描二维码手机微信扫一扫全知道微信怎么扫描二维码手机微信扫一扫全知道 4、扫描成功,界面会跳转到这个二维码的详细