写在前面
最近项目要实现相机扫描二维码功能,具体要求:1、扫描框 2、扫描动画 3、相册识别二维码 4、声音反馈。
记得之前用过三方库做过类似功能,但是也是知其然不知其所以然,然后今天自己用原生api简单封装了一个二维码扫描控件。
项目结构介绍
控件封装后主要结构如图:
如图中代码目录,vender里面放的是UIView+Frame分类,Resource里面放的是图片声音资源,TZImagePickerController是第三方相册,用来获取相册中的二维码识别的。主要的就是以QR开头的文件,我们具体说一说。
QRCode.h
这个文件主要放的是各个文件的头文件,方便在各处调用
#import "QRCodeScanManager.h" #import #import "QRLightManager.h" #import "QRCodeScanView.h" #import "QRCodeHelper.h"
QRLightManager
这个类是用来开启关闭闪光灯的
/** 打开手电筒 */ + (void)openFlashLight; /** 关闭手电筒 */ + (void)closeFlashLight;
#pragma mark 打开手电筒 + (void)openFlashLight { AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; NSError *error = nil; if ([captureDevice hasTorch]) { BOOL locked = [captureDevice lockForConfiguration:&error]; if (locked) { captureDevice.torchMode = AVCaptureTorchModeOn; [captureDevice unlockForConfiguration]; } } } #pragma mark 关闭手电筒 + (void)closeFlashLight { AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; if ([device hasTorch]) { [device lockForConfiguration:nil]; [device setTorchMode:AVCaptureTorchModeOff]; [device unlockForConfiguration]; } }
QRCodeScanView
这个类是将这个界面单独封装出来,便于自定义
在.h文件中有个枚举用来标识二维码扫描四周角标的位置:
typedef enum : NSUInteger { CornerLoactionDefault,//默认与边框同中心点 CornerLoactionInside,//在边框线内部 CornerLoactionOutside,//在边框线外部 } CornerLoaction;
自定义view各个属性:
@property (nonatomic, strong) UIColor *borderColor;/** 边框颜色*/ @property (nonatomic, assign) CornerLoaction cornerLocation;/** 边角位置 默认default*/ @property (nonatomic, strong) UIColor *cornerColor;/** 边角颜色 默认正保蓝#32d2dc*/ @property (nonatomic, assign) CGFloat cornerWidth;/** 边角宽度 默认2.f*/ @property (nonatomic, assign) CGFloat backgroundAlpha;/** 扫描区周边颜色的alpha 默认0.5*/ @property (nonatomic, assign) CGFloat timeInterval;/** 扫描间隔 默认0.02*/ @property (nonatomic, strong) UIButton *lightBtn;/** 闪光灯*/
暴露外部调用的方法:
/** 添加定时器 */ - (void)addTimer; /** 移除定时器 */ - (void)removeTimer; /** 根据灯光判断 */ - (void)lightBtnChangeWithBrightnessValue:(CGFloat)brightnessValue;
初始化默认值:
- (void)initilize { self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5]; self.borderColor = [UIColor whiteColor]; _cornerLocation = CornerLoactionDefault; _cornerColor = [UIColor colorWithRed:50/255.0f green:210/255.0f blue:220/255.0f alpha:1.0]; _cornerWidth = 2.0; self.timeInterval = 0.02; _backgroundAlpha = 0.5; [self addSubview:self.lightBtn]; }
重写view的drawRect方法,在这个方法里面绘制scanView的边框和边角
//空白区域设置 [[[UIColor blackColor] colorWithAlphaComponent:self.backgroundAlpha] setFill]; UIRectFill(rect); //获取上下文,并设置混合模式 -> kCGBlendModeDestinationOut CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetBlendMode(context, kCGBlendModeDestinationOut); //设置空白区 UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(borderX + 0.5 * borderLineW, borderY+ 0.5 * borderLineW, borderW - borderLineW, borderH - borderLineW)]; [bezierPath fill]; //执行混合模式 CGContextSetBlendMode(context, kCGBlendModeNormal); //边框设置 UIBezierPath *borderPath = [UIBezierPath bezierPathWithRect:CGRectMake(borderX, borderY, borderW, borderH)]; borderPath.lineCapStyle = kCGLineCapButt; borderPath.lineWidth = borderLineW; [self.borderColor set]; [borderPath stroke]; CGFloat cornerLength = 20; //左上角小图标 UIBezierPath *leftTopPath = [UIBezierPath bezierPath]; leftTopPath.lineWidth = self.cornerWidth; [self.cornerColor set]; CGFloat insideExcess = fabs(0.5 * (self.cornerWidth - borderLineW)); CGFloat outsideExcess = 0.5 * (borderLineW + self.cornerWidth); if (self.cornerLocation == CornerLoactionInside) { [leftTopPath moveToPoint:CGPointMake(borderX + insideExcess, borderY + cornerLength + insideExcess)]; [leftTopPath addLineToPoint:CGPointMake(borderX + insideExcess, borderY + insideExcess)]; [leftTopPath addLineToPoint:CGPointMake(borderX + cornerLength + insideExcess, borderY + insideExcess)]; } else if (self.cornerLocation == CornerLoactionOutside) { [leftTopPath moveToPoint:CGPointMake(borderX - outsideExcess, borderY + cornerLength - outsideExcess)]; [leftTopPath addLineToPoint:CGPointMake(borderX - outsideExcess, borderY - outsideExcess)]; [leftTopPath addLineToPoint:CGPointMake(borderX + cornerLength - outsideExcess, borderY - outsideExcess)]; } else { [leftTopPath moveToPoint:CGPointMake(borderX, borderY + cornerLength)]; [leftTopPath addLineToPoint:CGPointMake(borderX, borderY)]; [leftTopPath addLineToPoint:CGPointMake(borderX + cornerLength, borderY)]; } [leftTopPath stroke];
增加定时器以及开始动画:
- (void)addTimer { [self addSubview:self.scanningLine]; self.timer = [NSTimer timerWithTimeInterval:self.timeInterval target:self selector:@selector(beginAnimaiton) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes]; } #pragma mark 动画 - (void)beginAnimaiton { static BOOL isOrignPostion = YES; if (isOrignPostion) { _scanningLine.y = 0; isOrignPostion = NO; [UIView animateWithDuration:self.timeInterval animations:^{ self->_scanningLine.y += 2; } completion:nil]; } else { if (_scanningLine.frame.origin.y >= 0) { CGFloat scanContent_MaxY = self.frame.size.width; if (_scanningLine.y >= scanContent_MaxY - 10) { _scanningLine.y = 0; isOrignPostion = YES; } else { [UIView animateWithDuration:0.02 animations:^{ self->_scanningLine.y += 2; } completion:nil]; } } else { isOrignPostion = !isOrignPostion; } } }
闪光灯按钮点击事件,开启关闭闪光灯:
- (void)lightBtnClick:(UIButton *)btn { btn.selected = !btn.selected; if (btn.selected) { [QRLightManager openFlashLight]; } else { [QRLightManager closeFlashLight]; } }
QRCodeScanManager
这个单例用来控制所有的关于二维码扫描的事件
初始开启session 会话
/** 打开手电筒 */ + (void)openFlashLight; /** 关闭手电筒 */ + (void)closeFlashLight;
0
block:
/** 打开手电筒 */ + (void)openFlashLight; /** 关闭手电筒 */ + (void)closeFlashLight;
1
/** 打开手电筒 */ + (void)openFlashLight; /** 关闭手电筒 */ + (void)closeFlashLight;
2
/** 打开手电筒 */ + (void)openFlashLight; /** 关闭手电筒 */ + (void)closeFlashLight;
3
/** 打开手电筒 */ + (void)openFlashLight; /** 关闭手电筒 */ + (void)closeFlashLight;
4
注:这里只是截取部分重要代码,具体功能我会将代码放到GitHub上,代码里注释也写的很明白
使用功能
开启、结束定时器
开启、关闭session会话
启用、移除sampleBuffer代理
/** 打开手电筒 */ + (void)openFlashLight; /** 关闭手电筒 */ + (void)closeFlashLight;
5
初始化scanManager
/** 打开手电筒 */ + (void)openFlashLight; /** 关闭手电筒 */ + (void)closeFlashLight;
6
从相册识别二维码:
/** 打开手电筒 */ + (void)openFlashLight; /** 关闭手电筒 */ + (void)closeFlashLight;
7
作者:劉光軍_Shine
链接:https://www.jianshu.com/p/08733f2bfc26
本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/ios-erweimasaomiaoxiangguan.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9月 7, 2018 at 04:52 下午