之前一直在用 第三方的二维码扫描和生成功能,后来因为项目导入太多库,整的混乱不说,还会时不时的出现问题,所以去学习了一下系统自带的二维码功能,现将学习内容粘贴如下,有需要的小伙伴,可以参考一下。
-
导入系统库、遵循代理
- 先定义几个属性
//扫描展示的view
@property (strong, nonatomic) UIView *boxView;
//扫描框view
@property (strong, nonatomic) UIView *boxView;
//扫描线layer
@property (strong, nonatomic) CALayer *scanLayer;
//捕捉会话
@property (nonatomic, strong) AVCaptureSession *captureSession;
//展示layer(预览图层)
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;
- 方法
//是否开始扫描
-(BOOL)startReading;
// 停止扫描之后的方法
-(void)stopReading;
我这边实现的时候是通过button的点击事件来实现的,你也可以通过各种方法来实现,比如触摸屏幕的时候开启扫描,这个就看你喜好了。话不多说,我们赶紧用代码来实现吧。
- 首先我先创建我所需要的二维码展示view和启动二维码扫描的button。
_viewPreview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/2)];
_viewPreview.backgroundColor=[UIColor cyanColor];
[self.view addSubview:_viewPreview];
_startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_startBtn.frame=CGRectMake(100, self.view.frame.size.height/2+100, self.view.frame.size.width, 50);
[_startBtn setTitle:@"二维码" forState:UIControlStateNormal];
_startBtn.backgroundColor=[UIColor redColor];
[_startBtn addTarget:self action:@selector(startStopReading:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_startBtn];
_captureSession = nil;
_isReading = NO;
-
其次就是启动二维码扫描(先判断是否开启,如果开启了就关闭)
启动之后,重点的代码就来了。
- (BOOL)startReading {
NSError *error;
//1.初始化捕捉设备(AVCaptureDevice),类型为AVMediaTypeVideo
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//2.用captureDevice创建输入流
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
if (!input) {
NSLog(@"%@", [error localizedDescription]);
return NO;
}
//3.创建媒体数据输出流
AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
//4.实例化捕捉会话
_captureSession = [[AVCaptureSession alloc] init];
//4.1.将输入流添加到会话
[_captureSession addInput:input];
//4.2.将媒体输出流添加到会话中
[_captureSession addOutput:captureMetadataOutput];
//5.创建串行队列,并加媒体输出流添加到队列当中
dispatch_queue_t dispatchQueue;
dispatchQueue = dispatch_queue_create("myQueue", NULL);
//5.1.设置代理
[captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
//5.2.设置输出媒体数据类型为QRCode
[captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];
//6.实例化预览图层
_videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
//7.设置预览图层填充方式
[_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
//8.设置图层的frame
[_videoPreviewLayer setFrame:_viewPreview.layer.bounds];
//9.将图层添加到预览view的图层上
[_viewPreview.layer addSublayer:_videoPreviewLayer];
//10.设置扫描范围
captureMetadataOutput.rectOfInterest = CGRectMake(0.2f, 0.2f, 0.8f, 0.8f);
//10.1.扫描框
_boxView = [[UIView alloc] initWithFrame:CGRectMake(_viewPreview.bounds.size.width * 0.2f, _viewPreview.bounds.size.height * 0.2f, _viewPreview.bounds.size.width - _viewPreview.bounds.size.width * 0.4f, _viewPreview.bounds.size.height - _viewPreview.bounds.size.height * 0.4f)];
_boxView.layer.borderColor = [UIColor greenColor].CGColor;
_boxView.layer.borderWidth = 1.0f;
[_viewPreview addSubview:_boxView];
//10.2.扫描线
_scanLayer = [[CALayer alloc] init];
_scanLayer.frame = CGRectMake(0, 0, _boxView.bounds.size.width, 1);
_scanLayer.backgroundColor = [UIColor brownColor].CGColor;
[_boxView.layer addSublayer:_scanLayer];
//11.开启定时器,帮助扫描线动画效果的实现
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.2f target:self selector:@selector(moveScanLayer:) userInfo:nil repeats:YES];
[timer fire];
//11.开始扫描
[_captureSession startRunning];
return YES;
}```
4.扫描时,执行扫描线的动画效果
![](http://upload-images.jianshu.io/upload_images/1615658-80c4674745e21aac.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
5.扫描到之后执行代理方法
![](http://upload-images.jianshu.io/upload_images/1615658-fc0a7b10436df451.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
6.关闭的时候,先把所有东西都释放掉
```-(void)stopReading{
[_captureSession stopRunning];
_captureSession = nil;
[_scanLayer removeFromSuperlayer];
[_videoPreviewLayer removeFromSuperlayer];
}
OK,以上就是全部代码了,如有不足之处,还请多多指导。第一次发文,还是很紧张的。哈哈