一、二维码的生成
二维码生成利用libqrencode库,此库有很强的中文字符串转二维码能力。
如果libqrencode库中没有QRCodeGenerator类。需要首先导入QRCodeGenerator类。
使用的时候。只需导入QRCodeGenerator类即可。
可能遇到的错误:C99等
解决方法:在QRCodeGenerator类中导入头文件
#import
二、二维码扫描
二维码扫描利用ZBar库实现。
引用库本身扫描效果此处略。
记录ZBar自定义扫描,上源码:
- (void)initScanView
{
_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 clearColor];
_readview.torchMode = NO;//关闭闪关灯
_readview.tracksSymbols = NO;
_readview.showsFPS = NO;// 显示帧率 YES 显示 NO 不显示
UIImage *hbImage=[UIImage imageNamed:@"sao@2x"];
_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];//将被扫描的图像的区域
#pragma warning 此扫描区域的父视图不是readView.
[_readview addSubview:_scanZomeBack];
[_readview addSubview:_readLineView];
[self.view addSubview:_readview];
[_readview start];
//仿微信扫描区域中间动态线
[self scanLineView];
}
- (void)scanLineView
{
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"]];
[UIView animateWithDuration:3.0
delay: 0.0
options: UIViewAnimationOptionRepeat
animations:^{
//修改fream的代码写在这里
_readLineView.frame =CGRectMake(_scanZomeBack.frame.origin.x, _scanZomeBack.frame.origin.y+_scanZomeBack.frame.size.height, _scanZomeBack.frame.size.width, 2);
}
completion:^(BOOL finished){
}];
[_readview addSubview:_readLineView];
}
#pragma mark 获取扫描区域
-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds
{//rect的y和height决定了扫描区域的宽;rect的x和width决定了扫描区域的高。且点(x,y)和(宽高)成1:2关系.
CGFloat x,y,width,height;
x = (rect.origin.x+20) / readerViewBounds.size.width;
y = (rect.origin.y-40) / readerViewBounds.size.height;
width = (rect.size.width-40) / readerViewBounds.size.width;
height = (rect.size.height+80) / readerViewBounds.size.height;
return CGRectMake(x, y, width, height);
}
#pragma ZBarReaderViewDelegate
- (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) {
self.pShowEncode.text = symbol.data;
NSLog(@"data====%@",symbol.data);
break;
}
[readerView stop];
[readerView removeFromSuperview];
}
注意点:
1. zbar使用过程中,如果多次打开摄像头扫描Qrcode,会出现memory leak problem, 导致其内存指数型增长,大概8次调用后即耗费了85M 左右的内存, 最后导致app崩溃
解决方法:
2.64位中出现的扫描区域的蓝色边框
解决方法: 自己找到源码 然后找到下面图片里面的代码 改成这个:cropLayer.borderColor = [UIColor clearColor].CGColor;
,然后再打个.a就好了。
3.蓝色边框不要过早删除,我们可以先参考蓝色边框确定扫描区域。之后再删除。
4.修改扫描区域的时候,注意rect的y和height决定了扫描区域的宽;rect的x和width决定了扫描区域的高。且点(x,y)和(宽高)成1:2关系
5.
三、添加扫描区域透明其他黑色半透明背景视图
类似QQ获取头像背景
利用
CAShapeLayer
和贝塞尔曲线实现此种背景。参考来源:http://blog.csdn.net/zz_mm/article/details/42804671