zoukankan      html  css  js  c++  java
  • 二维码读取

     1 #pragma mark - 读取二维码
     2 - (void)readQRCoder
     3 {
     4     // 1. 摄像头设备
     5     AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
     6 
     7     // 2. 设置输入
     8     // 因为模拟器是没有摄像头的,因此在此最好做一个判断
     9     NSError *error = nil;
    10     AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    11     
    12     if (error) {
    13         NSLog(@"没有摄像头-%@", error.localizedDescription);
    14         
    15         return;
    16     }
    17     
    18     // 3. 设置输出(Metadata元数据)
    19     AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    20     // 3.1 设置输出的代理
    21     // 说明:使用主线程队列,相应比较同步,使用其他队列,相应不同步,容易让用户产生不好的体验
    22     [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    23 //    [output setMetadataObjectsDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
    24     
    25     // 4. 拍摄会话
    26     AVCaptureSession *session = [[AVCaptureSession alloc] init];
    27     // 添加session的输入和输出
    28     [session addInput:input];
    29     [session addOutput:output];
    30     // 4.1 设置输出的格式
    31     // 提示:一定要先设置会话的输出为output之后,再指定输出的元数据类型!
    32     [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
    33     
    34     // 5. 设置预览图层(用来让用户能够看到扫描情况)
    35     AVCaptureVideoPreviewLayer *preview = [AVCaptureVideoPreviewLayer layerWithSession:session];
    36     // 5.1 设置preview图层的属性
    37     [preview setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    38     // 5.2 设置preview图层的大小
    39     [preview setFrame:self.view.bounds];
    40     // 5.3 将图层添加到视图的图层
    41     [self.view.layer insertSublayer:preview atIndex:0];
    42     self.previewLayer = preview;
    43     
    44     // 6. 启动会话
    45     [session startRunning];
    46     
    47     self.session = session;
    48 }
    49 
    50 #pragma mark - 输出代理方法
    51 // 此方法是在识别到QRCode,并且完成转换
    52 // 如果QRCode的内容越大,转换需要的时间就越长
    53 - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
    54 {
    55     // 会频繁的扫描,调用代理方法
    56     // 1. 如果扫描完成,停止会话
    57     [self.session stopRunning];
    58     // 2. 删除预览图层
    59     [self.previewLayer removeFromSuperlayer];
    60     
    61     NSLog(@"%@", metadataObjects);
    62     // 3. 设置界面显示扫描结果
    63     
    64     if (metadataObjects.count > 0) {
    65         AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];
    66         // 提示:如果需要对url或者名片等信息进行扫描,可以在此进行扩展!
    67         _label.text = obj.stringValue;
    68     }
    69 }
  • 相关阅读:
    Windows 操作系统引导过程 BIOS & EFI
    Mac 系统引导过程概述 & BootCamp 的秘密
    Windows 10 安装 Ubuntu 子系统
    nrm 安装及报错处理
    司马懿人物关系
    大江大河
    曹操人物关系
    必要条件探路(导数)
    该题七种想法(一题一课之外接球)
    欧拉-查柏(Euler-Chapple)公式及其推广
  • 原文地址:https://www.cnblogs.com/yyh123/p/3439501.html
Copyright © 2011-2022 走看看