iOS的二维码(QRCode)的学习

QRCode 二维码的学习:

需要倒入一个框架   AVFoundation

1.需要有输入设备, 

2.输出设备 (解析输入设备的内容)

3.有一个会话类(输入输出完事了需要哪一个是输入那一个是输出)

 AVCaptureSession  

4。一个展示扫描的内容 layer

二 : 然后创建

三:设置代理 --获取数据

会话类不知道哪一个是时输入设备 哪一个是 输出设备的 需要衔接她们

去绑定输入和输出设备 (做一个是否的判断)、

四:设置扫描类型

五:开始扫描

[self.session startRunning]

六:完成代理方法去处理二维码 

扫描到的是一个数据数组

还需要去找到一个类(是一个类型的)  (细节头文件是没有提示的) 跳到方法里去

获取到了数据 里面有两个属性  obj.stringvalue

七:在iOS9里面又一个特殊服务  可以不用加载webview  可以方便展示网页

细节:(自带一类似导航栏的东西   完成  刷新 还带有分享  也就是系统真正的浏览器)

好处:网页过长的时候 在滚动的过程中是没有那个tabbar和导航栏的

只有在最上面的时候才会显示那个的tabbar和导航栏

 

如果你的SFSafariViewController   

//

//  GASScanViewController.m

//  GasManger

//

//  Created by Ninesday on 16/6/1.

//  Copyright © 2016年 Ninesday. All rights reserved.

//

#import "GASScanViewController.h"

#import <AVFoundation/AVFoundation.h>

#import <SafariServices/SafariServices.h>

@interface GASScanViewController ()<AVCaptureMetadataOutputObjectsDelegate>

//1.输入设备

@property (nonatomic,strong)AVCaptureDeviceInput *input;

//2.输出设备

@property (nonatomic,strong)AVCaptureMetadataOutput *output;

//3.会话类

@property (nonatomic,strong) AVCaptureSession *session;

//4.扫描到内容的layer

@property (nonatomic,strong) AVCaptureVideoPreviewLayer *previewlayer;

@end

@implementation GASScanViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    //输入设备

    //得先创建设备

    AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    self.input = [[AVCaptureDeviceInput alloc]initWithDevice:device error:nil];

    

    //输出设备

    self.output = [AVCaptureMetadataOutput new];

    

    //会话类

    self.session = [AVCaptureSession new];

    //绑定设备

    if ([self.session canAddInput:self.input]) {

        [self.session addInput:self.input];

    }

    if ([self.session canAddOutput:self.output]) {

        [self.session addOutput:self.output];

    }

    //设置代理获取数据

    [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

    //设置扫描的类型

    [self.output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];

    

    //扫描到内容layer--需要设置会话

    

    

    self.previewlayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:self.session];

    self.previewlayer.frame = self.view.bounds;

    [self.view.layer addSublayer:self.previewlayer];

    

    //开始扫描

    [self.session startRunning];

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {

    if (metadataObjects.count >0) {

        //1.停止扫描

        [self.session stopRunning];

        //删除layer

        [self.previewlayer removeFromSuperlayer];

        //获取数据 AVMetadataMachineReadableCodeObject 头文件没提示

        AVMetadataMachineReadableCodeObject *obj =  metadataObjects[0];

        NSLog(@"%@", obj.stringValue);

        

        if ([obj.stringValue hasPrefix:@"http"]) {

            //iOS9新增加的

            SFSafariViewController *sf = [[SFSafariViewController alloc]initWithURL:[NSURL URLWithString:obj.stringValue]];

            [self presentViewController:sf animated:sf completion:nil];

        }

    }

}

@end

八:二维码的优化

导入一个封装的类  

重置类型   

二维码的扫描 

iOS7 平台之后才有             流行的框架    ZBar ZXing

总结:

1.输入设备

2.输出

3.会话

4.代理

5.扫描类型

6.展示扫描到的内容

7.开始扫描

8.代理方法中进行解析

1.停止扫描

2.删除layer

3.获取数据   AVMetadataMachReadableCodeObject  头文件没提示

原文地址:https://www.cnblogs.com/Ninesday/p/5555016.html