二维码

来源:互联网 发布:淘宝大拿韩代怎么样 编辑:程序博客网 时间:2024/04/24 02:05

苹果公司升级到IOS7后自己的PassBook自带二维码扫描功能,所以现在使用二维码功能不需要在借助第三方库了

使用前请先导入AVFoundation.frameWork

//

//  YHQViewController.m

//  ReadQRCode

//

//  Created by apple on 13-12-22.

//  Copyright (c) 2013 apple. All rights reserved.

//

 

#import "YHQViewController.h"

#import <AVFoundation/AVFoundation.h>

 

@interfaceYHQViewController ()<AVCaptureMetadataOutputObjectsDelegate>

 

// IBOutletUILabel *captureLabe是自己建立的storyBoard中的Label用于显示获取到的二维码的信息的连线

@property (weaknonatomicIBOutletUILabel *captureLabel;

@property(strong,nonatomicAVCaptureSession *session; // 二维码生成的绘画

@property(strong,nonatomic)  AVCaptureVideoPreviewLayer *previewLayer;  // 二维码生成的屠城 

 

@end

 

@implementation YHQViewController

 

- (void)viewDidLoad

{

    [superviewDidLoad];

}

 

#pragma mark - 读取二维码

- (void)readQRcode

{

    // 1. 摄像头设备

    AVCaptureDevice *device = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];

    

    // 2. 设置输入

    // 因为模拟器是没有摄像头的,因此在此最好做一个判断

    NSError *error = nil;

    AVCaptureDeviceInput *input = [AVCaptureDeviceInputdeviceInputWithDevice:deviceerror:&error];

       if (error) {

        NSLog(@"没有摄像头-%@", error.localizedDescription);

        return;

    }

    

    // 3. 设置输出(Metadata元数据)

    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutputallocinit];

    // 3.1 设置输出的代理

    // 说明:使用主线程队列,相应比较同步,使用其他队列,相应不同步,容易让用户产生不好的体验

    [output setMetadataObjectsDelegate:selfqueue:dispatch_get_main_queue()];

    //    [output setMetadataObjectsDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];

    

    // 4. 拍摄会话

    AVCaptureSession *session = [[AVCaptureSessionallocinit];

    // 添加session的输入和输出

    [session addInput:input];

    [session addOutput:output];

    // 4.1 设置输出的格式

    // 提示:一定要先设置会话的输出为output之后,再指定输出的元数据类型!

    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];

    

    // 5. 设置预览图层(用来让用户能够看到扫描情况)

    AVCaptureVideoPreviewLayer *preview = [AVCaptureVideoPreviewLayerlayerWithSession:session];

    // 5.1 设置preview图层的属性

    [preview setVideoGravity:AVLayerVideoGravityResizeAspectFill];

    // 5.2 设置preview图层的大小

    [preview setFrame:self.view.bounds];

    // 5.3 将图层添加到视图的图层

    [self.view.layerinsertSublayer:preview atIndex:0];

    self.previewLayer = preview;

    

    // 6. 启动会话

    [session startRunning];

    

    self.session = session;

}

 

#pragma mark - 输出代理方法

// 此方法是在识别到QRCode,并且完成转换

// 如果QRCode的内容越大,转换需要的时间就越长

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection

{

    // 会频繁的扫描,调用代理方法

    // 1. 如果扫描完成,停止会话

    [self.sessionstopRunning];

    // 2. 删除预览图层

    [self.previewLayerremoveFromSuperlayer];

    

    NSLog(@"%@", metadataObjects);

    // 3. 设置界面显示扫描结果

    

    if (metadataObjects.count > 0) {

        AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];

        // 提示:如果需要对url或者名片等信息进行扫描,可以在此进行扩展!

        _captureLabel.text = obj.stringValue;

    }

}

// 在storyBoard中添加的按钮的连线的点击事件,一点击按钮就提示用户打开摄像头并扫描

- (IBAction)capture {

    //扫描二维码

    [selfreadQRcode];

}

 

@end


0 0
原创粉丝点击