• iOS系统方法扫描二维码


    二维码扫描




    #import "RootViewController.h"
    #import      AVFoundation/AVFoundation.h   //两边尖括号  显示不出来

    @interface RootViewController ()   AVCaptureMetadataOutputObjectsDelegate   //协议   道理同上 

    @property (nonatomic, strong) UILabel *showLabel;
    @property (nonatomic, strong) UIButton *button;
    @property (nonatomic, strong) AVCaptureSession *session;
    @property (nonatomic, strong) AVCaptureVideoPreviewLayer *preview;
    @end

    @implementation RootViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.showLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, self.view.bounds.size.width-40, 30)];
        self.showLabel.backgroundColor = [UIColor yellowColor];
        [self.view addSubview:self.showLabel];
        
        self.button = [UIButton buttonWithType:UIButtonTypeSystem];
        self.button.frame = CGRectMake(20, 300, self.view.bounds.size.width-40, 300);
        [self.button setTitle:@"开始" forState:UIControlStateNormal];
        [self.button setTitle:@"停止" forState:UIControlStateSelected];
        [self.button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:self.button];
    }

    - (void)buttonClick:(UIButton *)button {
        button.selected = !button.selected;
        if (button.selected) {
            [self startReadingMachineReadableCodeObjects:@[AVMetadataObjectTypeQRCode] inView:self.view];
        } else {
            [self stopReading];
        }
    }

    // 开始扫描
    - (void)startReadingMachineReadableCodeObjects:(NSArray *)codeObjects inView:(UIView *)preview {
        // 摄像头设备
        AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        NSError *error = nil;
        
        // 设置输入口
        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
        if (error || !input) {
            NSLog(@"error: %@", [error description]);
            return;
        }
        
        // 会话session, 把输入口加入会话
        self.session = [[AVCaptureSession alloc] init];
        [self.session addInput:input];   // 将输入添加到session
        
        // 设置输出口,加入session, 设置输出口参数
        AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
        [self.session addOutput:output];
        
        [output setMetadataObjectTypes:codeObjects];
        [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; // 使用主线程队列,相应比较同步,使用其他队列,相应不同步,容易让用户产生不好的体验
        
        // 设置展示层(预览层)
        self.preview       = [AVCaptureVideoPreviewLayer layerWithSession:self.session];  // 设置预览层信息
        self.preview.frame = preview.bounds;
        [preview.layer insertSublayer:self.preview atIndex:0]; //  添加至视图
        
        [self beginReading];
    }

    // 启动session
    - (void)beginReading
    {
        [self.session startRunning];
    }

    // 关闭session
    - (void)stopReading
    {
        self.button.selected = NO;
        [self.session stopRunning];
        [self.preview removeFromSuperlayer];
    }

    // 识别到二维码 并解析转换为字符串
    #pragma mark -
    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
    {
        [self stopReading];
        
        AVMetadataObject *metadata = [metadataObjects objectAtIndex:0];
        NSString *codeStr  = nil;
        if ([metadata respondsToSelector:@selector(stringValue)]) {
            codeStr = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
        }
        
        self.showLabel.text = codeStr;
    }

    @end

  • 相关阅读:
    OpenLayers的定制
    基于Emgu CV的人脸检测代码
    C#中跨库事务处理解决方案
    SqlHelper简单实现(通过Expression和反射)5.Lambda表达式解析类
    SqlHelper简单实现(通过Expression和反射)8.Sql Server数据处理类
    SqlHelper简单实现(通过Expression和反射)6.Providor模式(工厂+策略)可配置数据库选择
    SqlHelper简单实现(通过Expression和反射)1.引言
    SqlHelper简单实现(通过Expression和反射)3.实体,数据传输对象(DTO)Helper类设计
    SqlHelper简单实现(通过Expression和反射)7.MySql数据处理类
    SqlHelper简单实现(通过Expression和反射)10.使用方式
  • 原文地址:https://www.cnblogs.com/yu0806/p/4302080.html
Copyright © 2020-2023  润新知