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 }
  • 相关阅读:
    SQL表连接查询inner join left join right join full join ...
    希尔伯特空间回顾(简直太好了)
    pytorch的matmul怎么广播
    pytorch怎么抽取中间的特征或者梯度
    winedt102安装
    数据挖掘 关联规则
    git 命令
    caffe: c++11支持
    匈牙利算法
    ubuntu两个conda安装和切换
  • 原文地址:https://www.cnblogs.com/yyh123/p/3439501.html
Copyright © 2011-2022 走看看