//
// ViewController.m
// 二维码-全是干货
//
// Created by WSJ on 15/7/17.
// Copyright © 2015年 WSJ. All rights reserved.
//
/**
二维码实现思路
1. 输入设备(用来获取外界信息) 摄像头, 麦克风, 键盘
2. 输出设备 (将收集到的信息, 做解析, 来获取收到的内容)
3. 会话session (用来连接输入和输出设备)
4. 特殊的layer (展示输入设备所采集的信息)
*/
//主控制器代码实现
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "HMPreView.h"
@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>
//1. 输入设备(用来获取外界信息) 摄像头, 麦克风, 键盘
@property (nonatomic, strong) AVCaptureDeviceInput *input;
//2. 输出设备 (将收集到的信息, 做解析, 来获取收到的内容)
@property (nonatomic, strong) AVCaptureMetadataOutput *output;
//3. 会话session (用来连接输入和输出设备)
@property (nonatomic, strong) AVCaptureSession *session;
//4. 特殊的layer (展示输入设备所采集的信息)
//@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;
@property (nonatomic, strong) HMPreView *preview;
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark 点击屏幕开始扫描
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//1.输入设备(用来获取外界信息) 摄像头, 麦克风, 键盘
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
self.input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
//2.输出设备 (将收集到的信息, 做解析, 来获取收到的内容)
self.output = [AVCaptureMetadataOutput new];
//3.会话session (用来连接输入和输出设备)
self.session = [AVCaptureSession new];
// 会话扫描展示的大小
[self.session setSessionPreset:AVCaptureSessionPresetHigh];
// 会话跟输入和输出设备关联
if ([self.session canAddInput:self.input]) {
[self.session addInput:self.input];
}
if ([self.session canAddOutput:self.output]) {
[self.session addOutput:self.output];
}
//下面两句代码应该写在此处
//制定输出设备的代理, 用来接受返回的数据
[self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//设置元数据类型 二维码QRCode
[self.output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
//4.特殊的layer (展示输入设备所采集的信息)
//self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
// 大小layer的大小
//self.previewLayer.frame = self.view.bounds;
//[self.view.layer addSublayer:self.previewLayer];
self.preview = [[HMPreView alloc] initWithFrame:self.view.bounds];
self.preview.session = self.session;
[self.view addSubview:self.preview];
//5. 启动会话
[self.session startRunning];
}
/**
captureOutput : 输出设备
metadataObjects : 元数据对象的数组
connection : 连接
*/
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
//1. 停止会话
[self.session stopRunning];
//2. 删除layer
//[self.previewLayer removeFromSuperlayer];
[self.preview removeFromSuperview];
//3. 遍历数据获取内容
for (AVMetadataMachineReadableCodeObject *obj in metadataObjects) {
//NSLog(@"obj: %@",obj.stringValue);
self.label.text = obj.stringValue;
}
}
@end
//layer层代码实现
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface HMPreView : UIView
@property (nonatomic,strong)AVCaptureSession *session;
@end
// HMPreView.m
// 二维码扫描
//
// Created by WSJ on 15/7/17.
// Copyright (c) 2015年 WSJ. All rights reserved.
//
#import "HMPreView.h"
@interface HMPreView ()
@property (nonatomic,strong)UIImageView *imageView;
@property (nonatomic,strong)UIImageView *lineImageView;
@property (nonatomic,strong)NSTimer *timer;
@end
@implementation HMPreView
/**
* layer的类型
*
* @return AVCaptureVideoPreviewLayer 特殊的layer 可以展示输入设备采集到得信息
*/
+ (Class)layerClass
{
return [AVCaptureVideoPreviewLayer class];
}
- (void)setSession:(AVCaptureSession *)session
{
_session = session;
AVCaptureVideoPreviewLayer *layer = (AVCaptureVideoPreviewLayer *) self.layer;
layer.session = session;
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self initUiConfig];
}
return self;
}
- (void)initUiConfig
{
//设置背景图片
_imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pick_bg.png"]];
//设置位置到界面的中间
_imageView.frame = CGRectMake(self.bounds.size.width * 0.5 - 140, self.bounds.size.height * 0.5 - 140, 280, 280);
//添加到视图上
[self addSubview:_imageView];
//初始化二维码的扫描线的位置
_lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 10, 220, 2)];
_lineImageView.image = [UIImage imageNamed:@"line.png"];
[_imageView addSubview:_lineImageView];
//开启定时器
_timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(animation) userInfo:nil repeats:YES];
}
- (void)animation
{
[UIView animateWithDuration:2.8 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
_lineImageView.frame = CGRectMake(30, 260, 220, 2);
} completion:^(BOOL finished) {
_lineImageView.frame = CGRectMake(30, 10, 220, 2);
}];
}
@end
这里就能根据原生代码实现二维码扫描信息了,快来试试吧,当然网络上有很多第三方框架.