首页 > 代码库 > iOS开发之二维码扫描
iOS开发之二维码扫描
二维码扫描
01-导入系统库
02 新建继承自UIView的
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } span.s1 { }</style>LHQPreView
2.1导入系统库头文件
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c81b13 } span.s1 { color: #822d0f } span.s2 { }</style>#import <AVFoundation/AVFoundation.h>
2.2声明所需要的属性
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c42275 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } span.s1 { } span.s2 { color: #000000 } span.s3 { color: #6122ae }</style>@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UIImageView *lineImageView;
@property(nonatomic,strong)NSTimer *timer;
@property(nonatomic,strong)AVCaptureSession *session;
2.3返回layer的类型
//可以展示输入设备展示的信息
+(Class)layerClass{
return [AVCaptureVideoPreviewLayer class];
}
2.4 设置特殊的layer
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px } span.s1 { } span.s2 { color: #c42275 } span.s3 { color: #6122ae } span.s4 { color: #539aa4 } span.s5 { color: #000000 } span.s6 { color: #703daa }</style>- (void)setSession:(AVCaptureSession *)session{
_session = session;
AVCaptureVideoPreviewLayer *layer = (AVCaptureVideoPreviewLayer *)self.layer;
layer.session = session;
}
2.5 初始化调用方法实现layer上展示扫描横线的动画
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c42275 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3e1e81 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1e9421 } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px } p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #539aa4 } span.s1 { } span.s2 { color: #c42275 } span.s3 { color: #703daa } span.s4 { color: #3e1e81 } span.s5 { color: #294c50 } span.s6 { color: #000000 } span.s7 { font: 18.0px "PingFang SC" } span.s8 { color: #539aa4 } span.s9 { color: #6122ae } span.s10 { color: #c81b13 } span.s11 { font: 18.0px Menlo; color: #000000 } span.s12 { font: 18.0px Menlo } span.s13 { color: #0435ff }</style>- (instancetype)initWithFrame:(CGRect)frame{
if(self = [super initWithFrame:frame]){
[self initUiConfig];
}
return self;
}
- (void)initUiConfig
{
//设置背景图片
_imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pick_bg.png"]];
//设置位置到界面的中间
_imageView.frame = CGRectMake(self.bounds.size.width * 0.5 - 140, self.bounds.size.height * 0.5 - 140, 280, 280);
//添加到视图上
[self addSubview:_imageView];
//初始化二维码的扫描线的位置
_lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 10, 220, 2)];
_lineImageView.image = [UIImage imageNamed:@"line.png"];
[_imageView addSubview:_lineImageView];
//开启定时器
_timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(animation) userInfo:nil repeats:YES];
}
- (void)animation
{
[UIView animateWithDuration:2.8 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
_lineImageView.frame = CGRectMake(30, 260, 220, 2);
} completion:^(BOOL finished) {
_lineImageView.frame = CGRectMake(30, 10, 220, 2);
}];
}
3 主控制器
3.1导入系统库头文件
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c81b13 } span.s1 { color: #822d0f } span.s2 { }</style>#import <AVFoundation/AVFoundation.h>
3.2导入特殊layer的View
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c81b13 } span.s1 { }</style>LHQPreView.h
3.3定义所需要的属性
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1e9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c42275 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } span.s1 { font: 18.0px Menlo } span.s2 { } span.s3 { color: #000000 } span.s4 { color: #6122ae } span.s5 { font: 18.0px "PingFang SC" } span.s6 { color: #3c828b }</style>//3.3.1. 输入设备(从外界采集信息)
//输入设备有很多种 摄像头 麦克风 键盘
@property(nonatomic,strong)AVCaptureDeviceInput *input;
//3.3.2.输出设备(解析采集来的内容 然后获取数据)Metadata 元数据
@property(nonatomic,strong)AVCaptureMetadataOutput *output;
//3.3.3.会话 session(连接输入和输出进行工作)
@property(nonatomic,strong)AVCaptureSession *session;
//3.3.4 layer 特殊的layer(展示输入设备采集到的信息)
@property(nonatomic,strong)LHQPreView *preView;
3.4 二维码扫描四个步骤
3.4.1
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1e9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3e1e81 } span.s1 { font: 18.0px Menlo; color: #000000 } span.s2 { font: 18.0px Menlo } span.s3 { } span.s4 { color: #000000 } span.s5 { font: 18.0px "PingFang SC" } span.s6 { color: #3e1e81 } span.s7 { color: #703daa } span.s8 { color: #539aa4 } span.s9 { color: #6122ae } span.s10 { color: #c42275 }</style>//1.摄像头 输入设备(从外界采集信息)
//创建具体的设备
//AVMediaTypeAudio 麦克风
//AVMediaTypeVideo 摄像头
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
_input = [AVCaptureDeviceInput deviceInputWithDevice:device error:NULL];
3.4.2
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1e9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae } span.s1 { font: 18.0px Menlo } span.s2 { } span.s3 { color: #000000 } span.s4 { color: #539aa4 } span.s5 { color: #3e1e81 }</style>//2.输出设备(解析采集来的内容 然后获取数据)
_output = [[AVCaptureMetadataOutput alloc]init];
3.4.3
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1e9421 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #539aa4 } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px } p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3e1e81 } span.s1 { color: #000000 } span.s2 { } span.s3 { font: 18.0px "PingFang SC" } span.s4 { color: #539aa4 } span.s5 { color: #3e1e81 } span.s6 { font: 18.0px Menlo } span.s7 { font: 18.0px Menlo; color: #000000 } span.s8 { color: #c42275 } span.s9 { color: #0435ff }</style>//3.会话 session(连接输入和输出进行工作)
_session = [[AVCaptureSession alloc]init];
// 会话展示的大小
[_session setSessionPreset:AVCaptureSessionPresetHigh];
//添加输入设备和输出设备
if([_session canAddInput:_input]){
[_session addInput:_input];
}
if([_session canAddOutput:_output]){
[_session addOutput:_output];
}
//指定输出设备的代理 来返回解析到的数据
// 把网址展示,并且跳转做相关的操作
[_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//设置元数据类型
//AVMetadataObjectTypeQRCode 二维码类型
[_output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
3.4.5
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1e9421 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } span.s1 { color: #000000 } span.s2 { } span.s3 { font: 18.0px "PingFang SC" } span.s4 { font: 18.0px Menlo; color: #000000 } span.s5 { font: 18.0px Menlo } span.s6 { color: #3c828b } span.s7 { color: #3e1e81 } span.s8 { color: #c42275 } span.s9 { color: #703daa } span.s10 { color: #539aa4 }</style>//4 layer 特殊的layer(展示输入设备采集到的信息)
// _previewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:_session];
// //daxiao
// self.previewLayer.frame = self.view.bounds;
// //
// [self.view.layer addSublayer:self.previewLayer];
//创建一个特殊视图,用来展示二维码界面
LHQPreView *preView = [[LHQPreView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:preView];
preView.session = _session;
3.4.6
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3e1e81 } span.s1 { color: #000000 } span.s2 { } span.s3 { font: 18.0px "PingFang SC" } span.s4 { color: #c42275 } span.s5 { color: #539aa4 }</style>//5 开启会话
[self.session startRunning];
3.4.7实现代理方法,打印出扫描的结果
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3e1e81 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae } span.s1 { } span.s2 { font: 18.0px "PingFang SC" } span.s3 { color: #c42275 } span.s4 { color: #6122ae } span.s5 { color: #000000 } span.s6 { color: #539aa4 } span.s7 { color: #3e1e81 } span.s8 { color: #c81b13 } span.s9 { color: #703daa }</style>/*
captureOutput 输出
metadataObjects 元数据数组
connection 连接
*/
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
//停止会话
[_session stopRunning];
//移除labyer
[self.preView removeFromSuperview];
for(AVMetadataMachineReadableCodeObject *objc in metadataObjects){
// NSLog(@"%@",[objc class]);
//AVMetadataMachineReadableCodeObject
NSLog(@"%@",objc.stringValue);
}
}
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c81b13 } span.s1 { color: #822d0f } span.s2 { }</style> <style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px "PingFang SC"; color: #1e9421 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae } span.s1 { } span.s2 { font: 18.0px "PingFang SC" } span.s3 { font: 18.0px Menlo } span.s4 { color: #703daa } span.s5 { color: #000000 } span.s6 { color: #c42275 } span.s7 { color: #3e1e81 }</style>
iOS开发之二维码扫描