读取二维码 - (Obj-C)

整个过程需要四个部分:
1.输入设备 (摄像头)
2.输出设备 (二维码算法计算的数据结果 元数据)
3.会话 (管理输入&输出设备)
4.预览Layer

演示代码:

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <SafariServices/SafariServices.h>

@interface ViewController () <AVCaptureMetadataOutputObjectsDelegate>

// 输入设备 摄像头)
@property (nonatomic,strong) AVCaptureDeviceInput *input;
// 输出设备 二维码算法计算的数据结果 元数据
@property (nonatomic,strong) AVCaptureMetadataOutput *output;
// 会话 (管理输入&输出设备)
@property (nonatomic,strong) AVCaptureSession *session;
// 预览layer
@property (nonatomic,strong) AVCaptureVideoPreviewLayer *previewLayer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // 1. 输入设备  摄像头
    /*          设置输入设备类型
         NSString *const AVMediaTypeVideo;
         NSString *const AVMediaTypeAudio;
         NSString *const AVMediaTypeText;
         NSString *const AVMediaTypeClosedCaption;
         NSString *const AVMediaTypeSubtitle;
         NSString *const AVMediaTypeTimecode;
         NSString *const AVMediaTypeTimedMetadata;
         NSString *const AVMediaTypeMetadata;
         NSString *const AVMediaTypeMuxed;
     */
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    self.input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    
    
    // 2. 输出设备 二维码算法计算的数据结果 元数据  (输出设备的类型必须在输出设备添加到会话中以后设置)
    self.output = [[AVCaptureMetadataOutput alloc]init];
    // 获取数据(设置代理)
    [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    // 3. 会话 管理输入&输出设备
    self.session = [[AVCaptureSession alloc]init];
    // 添加输入设备&输出设备到会话中
    if([self.session canAddInput:self.input]){
        [self.session addInput:self.input];
    }
    if ([self.session canAddOutput:self.output]) {
        [self.session addOutput:self.output];
    }
    // 设置输出设备类型 二维码算法:AVMetadataObjectTypeQRCode
    self.output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
    
    // 4. 预览Layer
    self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
    // 添加到当前视图的Layer中
    [self.view.layer addSublayer:self.previewLayer];
    // 设置layer的尺寸
    self.previewLayer.frame = self.view.bounds;
    // 开启会话
    [self.session startRunning];
    
}

#pragma mark - AVCaptureMetadataOutputObjectsDelegate

/**
 *  已经输出元数据后调用
 *
 *  @param captureOutput   输出设备
 *  @param metadataObjects 元数据
 *  @param connection      连接信息
 */
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
    
    for (AVMetadataMachineReadableCodeObject *obj in metadataObjects) {
        
        //通过stringValue属性就可以拿到二维码内的数据
        if ([obj.stringValue hasPrefix:@"http"]) {//如果开头是http说明是一个网页
            
            // 创建控制器
            SFSafariViewController *safariViewController = [[SFSafariViewController alloc]initWithURL:[NSURL URLWithString:obj.stringValue]];
            
            // modal展示
            [self presentViewController:safariViewController animated:YES completion:nil];
            
            // 扫描完成后停止持续扫描
            [self.session stopRunning];
            return;
            
        } else {
            
            NSLog(@"%@",[obj stringValue]);
        }
    }
    
}

@end
授权.PNG
modal展示.PNG

这里并不是真的调到了Safari浏览器,而是在应用内部以Safari样式显示,点击右下角的Safari浏览器按钮才会真正跳转到Safari浏览器

获取二维码内的文字信息:

2016-07-15 19:04:33.536 09-二维码[731:441146] 只要锄头舞的好,哪有墙角挖不倒!
2016-07-15 19:04:33.566 09-二维码[731:441146] 只要锄头舞的好,哪有墙角挖不倒!
2016-07-15 19:04:33.600 09-二维码[731:441146] 只要锄头舞的好,哪有墙角挖不倒!
2016-07-15 19:04:33.633 09-二维码[731:441146] 只要锄头舞的好,哪有墙角挖不倒!
2016-07-15 19:04:33.664 09-二维码[731:441146] 只要锄头舞的好,哪有墙角挖不倒!
2016-07-15 19:04:33.700 09-二维码[731:441146] 只要锄头舞的好,哪有墙角挖不倒!
2016-07-15 19:04:33.734 09-二维码[731:441146] 只要锄头舞的好,哪有墙角挖不倒!
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容