zoukankan      html  css  js  c++  java
  • iOS使用AVFoundation实现二维码扫描

    实现过程如下:

    Step1:需要导入:AVFoundation Framework 包含头文件:

    #import <AVFoundation/AVFoundation.h>
    

    Step2:设置捕获会话

    设置 AVCaptureSession 和 AVCaptureVideoPreviewLayer 成员

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
        #import <AVFoundation/AVFoundation.h>
    
        static const char *kScanQRCodeQueueName = "ScanQRCodeQueue";
    
        @interface ViewController () <AVCaptureMetadataOutputObjectsDelegate>
        .....
        @property (nonatomic) AVCaptureSession *captureSession;
        @property (nonatomic) AVCaptureVideoPreviewLayer *videoPreviewLayer;
        @property (nonatomic) BOOL lastResult;
        @end
    

    Step3:创建会话,读取输入流

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    
        - (BOOL)startReading
        {
            // 获取 AVCaptureDevice 实例
            NSError * error;
            AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
            // 初始化输入流
            AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
            if (!input) {
                NSLog(@"%@", [error localizedDescription]);
                return NO;
            }
            // 创建会话
            _captureSession = [[AVCaptureSession alloc] init];
            // 添加输入流
            [_captureSession addInput:input];
            // 初始化输出流
            AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
            // 添加输出流
            [_captureSession addOutput:captureMetadataOutput];
    
            // 创建dispatch queue.
            dispatch_queue_t dispatchQueue;
            dispatchQueue = dispatch_queue_create(kScanQRCodeQueueName, NULL);
            [captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
            // 设置元数据类型 AVMetadataObjectTypeQRCode
            [captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];
    
            // 创建输出对象
            _videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
            [_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
            [_videoPreviewLayer setFrame:_sanFrameView.layer.bounds];
            [_sanFrameView.layer addSublayer:_videoPreviewLayer];
            // 开始会话
            [_captureSession startRunning];
    
            return YES;
        }
    

    Step4:停止读取

    1
    2
    3
    4
    5
    6
    7
    
        - (void)stopReading
        {
            // 停止会话
            [_captureSession stopRunning];
            _captureSession = nil;
        }
    

    Step5:获取捕获数据

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects
      fromConnection:(AVCaptureConnection *)connection
    {
        if (metadataObjects != nil && [metadataObjects count] > 0) {
            AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
            NSString *result;
            if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
                result = metadataObj.stringValue;
            } else {
                NSLog(@"不是二维码");
            }
            [self performSelectorOnMainThread:@selector(reportScanResult:) withObject:result waitUntilDone:NO];
        }
    }
    

    Step6:处理结果

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
      - (void)reportScanResult:(NSString *)result
      {
          [self stopReading];
          if (!_lastResult) {
              return;
          }
          _lastResut = NO;
          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"二维码扫描"
                                                          message:result
                                                         delegate:nil
                                                cancelButtonTitle:@"取消"
                                                otherButtonTitles: nil];
          [alert show];
          // 以下处理了结果,继续下次扫描
          _lastResult = YES;
      }
    

    以上基本就是二维码的获取流程

  • 相关阅读:
    Shiro学习(一)总体介绍
    Spring-MVC理解之一:应用上下文webApplicationContext
    现代软件工程 第十四章 【质量保障】 练习与讨论
    rtsp 流媒体服务器,播放器
    ios app 打包
    xcrun: error: unable to find utility "PackageApplication", not a developer tool or in PATH ​
    Bootstrap日期和时间表单组件
    微信小程序组件action-sheet
    微信小程序组件radio
    微信小程序组件slider
  • 原文地址:https://www.cnblogs.com/luoxiaofu/p/5274072.html
Copyright © 2011-2022 走看看