zoukankan      html  css  js  c++  java
  • iOS开发之二维码扫描

    二维码扫描

    01-导入系统库

     

     

    02 新建继承自UIView的

    LHQPreView

    2.1导入系统库头文件

    #import <AVFoundation/AVFoundation.h>

    2.2声明所需要的属性

    @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

    - (void)setSession:(AVCaptureSession *)session{

        _session = session;

        AVCaptureVideoPreviewLayer *layer = (AVCaptureVideoPreviewLayer *)self.layer;

        layer.session = session;

        

    }

    2.5 初始化调用方法实现layer上展示扫描横线的动画

    - (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导入系统库头文件

    #import <AVFoundation/AVFoundation.h>

    3.2导入特殊layer的View

    LHQPreView.h

    3.3定义所需要的属性

    //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

     //1.摄像头 输入设备(从外界采集信息)

        //创建具体的设备

        //AVMediaTypeAudio 麦克风

        //AVMediaTypeVideo 摄像头

        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

        _input = [AVCaptureDeviceInput deviceInputWithDevice:device error:NULL];

    3.4.2

    //2.输出设备(解析采集来的内容 然后获取数据)

        _output = [[AVCaptureMetadataOutput alloc]init];

     

    3.4.3

     //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

     //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

     //5 开启会话

        [self.session startRunning];

     

     

    3.4.7实现代理方法,打印出扫描的结果

    /*

     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);

        }

        

    }

     

     

     

     

  • 相关阅读:
    模板 无源汇上下界可行流 loj115
    ICPC2018JiaozuoE Resistors in Parallel 高精度 数论
    hdu 2255 奔小康赚大钱 最佳匹配 KM算法
    ICPC2018Beijing 现场赛D Frog and Portal 构造
    codeforce 1175E Minimal Segment Cover ST表 倍增思想
    ICPC2018Jiaozuo 现场赛H Can You Solve the Harder Problem? 后缀数组 树上差分 ST表 口胡题解
    luogu P1966 火柴排队 树状数组 逆序对 离散化
    luogu P1970 花匠 贪心
    luogu P1967 货车运输 最大生成树 倍增LCA
    luogu P1315 观光公交 贪心
  • 原文地址:https://www.cnblogs.com/LiLihongqiang/p/7119954.html
Copyright © 2011-2022 走看看