您的位置:首页 > 移动开发 > IOS开发

iOS二维码的读取

2014-01-08 18:08 316 查看
既然已经生成二维码了,那么需要读取把,所以做了一个二维码读取的,话不多说,看代码,每句代码我都写了注释了,相信大家看了都会明白的,代码都是经过真机测试。

//
//  ZJViewController.m
//  ReadQRCode
//
//  Created by apple on 13-12-22.
//  Copyright (c) 2013年 zhengjing. All rights reserved.
//

#import "ZJViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ZJViewController ()<AVCaptureMetadataOutputObjectsDelegate>

@property (weak, nonatomic) IBOutlet UILabel *captureLabel;
@property(strong,nonatomic) AVCaptureSession *session;
@property(strong,nonatomic)  AVCaptureVideoPreviewLayer *previewLayer;
@end

@implementation ZJViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - 读取二维码
- (void)readQRcode
{
// 1. 摄像头设备
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

// 2. 设置输入
// 因为模拟器是没有摄像头的,因此在此最好做一个判断
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];

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

return;
}

// 3. 设置输出(Metadata元数据)
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
// 3.1 设置输出的代理
// 说明:使用主线程队列,相应比较同步,使用其他队列,相应不同步,容易让用户产生不好的体验
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//    [output setMetadataObjectsDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];

// 4. 拍摄会话
AVCaptureSession *session = [[AVCaptureSession alloc] init];
// 添加session的输入和输出
[session addInput:input];
[session addOutput:output];
// 4.1 设置输出的格式
// 提示:一定要先设置会话的输出为output之后,再指定输出的元数据类型!
[output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];

// 5. 设置预览图层(用来让用户能够看到扫描情况)
AVCaptureVideoPreviewLayer *preview = [AVCaptureVideoPreviewLayer layerWithSession:session];
// 5.1 设置preview图层的属性
[preview setVideoGravity:AVLayerVideoGravityResizeAspectFill];
// 5.2 设置preview图层的大小
[preview setFrame:self.view.bounds];
// 5.3 将图层添加到视图的图层
[self.view.layer insertSublayer: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.session stopRunning];
// 2. 删除预览图层
[self.previewLayer removeFromSuperlayer];

NSLog(@"%@", metadataObjects);
// 3. 设置界面显示扫描结果

if (metadataObjects.count > 0) {
AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];
// 提示:如果需要对url或者名片等信息进行扫描,可以在此进行扩展!
_captureLabel.text = obj.stringValue;
}
}
- (IBAction)capture {
//扫描二维码
[self readQRcode];
}

@end

下载地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息