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

原文地址:https://www.cnblogs.com/yu0806/p/4302080.html