zoukankan      html  css  js  c++  java
  • 二维码(扫描)

    读取二维码

    • 读取二维码需要导入AVFoundation框架

    • 利用摄像头识别二维码中的内容(模拟器不行)

    • 输入(摄像头)

    • 由会话将摄像头采集到的二维码图像转换成字符串数据

    • 输出(数据)

    • 由预览图层显示扫描场景

    过程
    • 1.创建捕捉会话
    • 我们会想问二维码数据从哪里来???
    • 2.创建输入设备
    • 将输入设备加入session中
    • 3.创建输出方式
    • 4.添加显示一个layer
    • 5.开始扫描
    #import "ViewController.h"
    #import <AVFoundation/AVFoundation.h>
    
    @interface ViewController () <AVCaptureMetadataOutputObjectsDelegate>
    
    @property (nonatomic, weak) AVCaptureSession *session;
    @property (nonatomic, weak) AVCaptureVideoPreviewLayer *layer;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        // 1.创建捕捉会话
        AVCaptureSession *session = [[AVCaptureSession alloc] init];
        self.session = session;
        
        // 2.添加输入设备(数据从摄像头输入)
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
        [session addInput:input];
        
        // 3.添加输出数据(示例对象-->类对象-->元类对象-->根元类对象)
        AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
        [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
        [session addOutput:output];
        
        // 3.1.设置输入元数据的类型(类型是二维码数据)
        [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
        
        // 4.添加扫描图层
        AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:session];
        layer.frame = self.view.bounds;
        [self.view.layer addSublayer:layer];
        self.layer = layer;
        
        // 5.开始扫描
        [session startRunning];
    }
    
    #pragma mark - 实现output的回调方法
    // 当扫描到数据时就会执行该方法
    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
    {
        if (metadataObjects.count > 0) {
            AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject];
            NSLog(@"%@", object.stringValue);
            
            // 停止扫描
            [self.session stopRunning];
            
            // 将预览图层移除
            [self.layer removeFromSuperlayer];
        } else {
            NSLog(@"没有扫描到数据");
        }
    }
    
    @end
    
    
  • 相关阅读:
    Apache 流框架 Flink,Spark Streaming,Storm对比分析(1)
    Apache 流框架 Flink,Spark Streaming,Storm对比分析(2)
    spark日志配置及问题排查方式。
    Structure Streaming和spark streaming原生API访问HDFS文件数据对比
    fstream,sstream的学习记录
    控制位数和填充0
    激活windows10(已更新工具)
    7-13 统计工龄(排序)
    Insertion or Heap Sort
    Insert or Merge
  • 原文地址:https://www.cnblogs.com/yangyongnian/p/5483576.html
Copyright © 2011-2022 走看看