iOS中使用ZBar扫描二维码自定义扫描界面
2015-11-14 21:32
483 查看
之前在Android中使用过ZXing识别二维码,ZXing也有对应的iOS版本,经过了解,ZBar也是一个常用的二维码识别软件,并分别提供了iOS和Android的SDK可供使用,最终我选择了ZBar进行二维码识别,它的注释清晰,便于使用。
ZBar为我们提供了两种使用方式,一种是直接调用ZBar提供的ZBarReaderViewController打开一个扫描界面,另一种方式是使用ZBar提供的可以嵌在其他视图中的ZBarReaderView,实际项目中我们更可能会使用第二种方式,这可以让我们对界面做更多的定制。
ZBar使用起来也非常简单,将ZBarSDK导入项目,在需要使用ZBar的文件中导入ZBarSDK.h头文件即可
[objc] view
plaincopy
#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];
}
[objc] view
plaincopy
#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);
}
[objc] view
plaincopy
#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];
}
ZBar为我们提供了两种使用方式,一种是直接调用ZBar提供的ZBarReaderViewController打开一个扫描界面,另一种方式是使用ZBar提供的可以嵌在其他视图中的ZBarReaderView,实际项目中我们更可能会使用第二种方式,这可以让我们对界面做更多的定制。
ZBar使用起来也非常简单,将ZBarSDK导入项目,在需要使用ZBar的文件中导入ZBarSDK.h头文件即可
[objc] view
plaincopy
#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];
}
[objc] view
plaincopy
#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);
}
[objc] view
plaincopy
#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扫描界面
- 苹果开发——设置iTunes Connect中的Contracts, Tax, and Banking
- [证书]1、iOS申请证书,Certificates, Identifiers &Profiles 简介
- Phonegap 之 iOS银联在线支付(js调用ios端银联支付控件)
- #在蓝懿学习iOS的日子#第六个练习日
- IOS开发简单登录LoginViewController、注册RegisterViewController、UcenterViewController功能实现方法
- IOS iPhone 开发中的文件读写及数据存储(一)
- IOS iPhone 开发中的文件读写及数据存储(一)
- iOS性能优化:Instruments使用实战
- iOS9的新特性以及适配方案
- ios开发-单击和双击手势的区分处理
- ios layout机制相关方法
- 在公司中怎样使用SVN、Git
- TSMessages,非HUD风格的iOS提示框(附官方demo BUG修复方案)
- TSMessages,非HUD风格的iOS提示框(附官方demo BUG修复方案)
- 关于状态栏 上颜色配置 ios7.x 之后
- 总结iOS 多线程学习过程四
- iOS9开放的新API--Spotlight使用指南
- iOS学习之NSBundle介绍和使用
- iOS打包IPA 文件