您的位置:首页 > 其它

二维码使用原理

2016-04-24 12:35 405 查看
#import <UIKit/UIKit.h>

#import <AVFoundation/AVFoundation.h>

@interface HMPreView :
UIView

@property (nonatomic,strong)AVCaptureSession *session;

@end

#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);

}];

}

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

#import "HMPreView.h"

/** 补充的iOS9新特性*/

#import <SafariServices/SafariServices.h>

/**

AV

A : Audio 声音

V : Video 视频

*/

@interface
ViewController ()<AVCaptureMetadataOutputObjectsDelegate>

/** 输入设备*/

@property (nonatomic,
strong) AVCaptureDeviceInput *input;

/** 数据输出*/

@property (nonatomic,
strong) AVCaptureMetadataOutput *output;

/** 会话类*/

@property (nonatomic,
strong) AVCaptureSession *session;

/** 特殊的layer,展示用户扫描到的内容*/

//@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;

@property (nonatomic,
strong) HMPreView *preview;

@end

@implementation ViewController

- (void)viewDidLoad {

[super
viewDidLoad];

}

#pragma mark 点击屏幕扫描二维码

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

//1.
输入设备

AVCaptureDevice *device = [AVCaptureDevice
defaultDeviceWithMediaType:AVMediaTypeVideo];

self.input = [[AVCaptureDeviceInput
alloc] initWithDevice:device
error:nil];

//2.
数据输出

//2.1
创建数据输出对象

self.output = [AVCaptureMetadataOutput
new];

//2.2
设置代理

[self.output
setMetadataObjectsDelegate:self
queue:dispatch_get_main_queue()];

//3.
会话类 --> 协调输入和输出

//3.1
创建会话类

self.session = [AVCaptureSession
new];

//3.2
协调输入和输出

if ([self.session
canAddInput:self.input]) {

[self.session
addInput:self.input];

}

if ([self.session
canAddOutput:self.output]) {

[self.session
addOutput:self.output];

}

//2.3
设置扫描类型 二维码 : QRCode

[self.output
setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];

//4. 特殊的layer,展示用户扫描到的内容 -->
需要一个会话类的对象

// self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];

// 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];

}

#pragma mark 代理方法 -->
获取数据

//metadataObjects: 扫描到的数据

//fromConnection: 连接

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

{

//1.
停止扫描

[self.session
stopRunning];

//2.
删除特殊的layer

//[self.previewLayer removeFromSuperlayer];

[self.preview
removeFromSuperview];

//3.
获取数据

//AVMetadataMachineReadableCodeObject:
这个类如果不知道, 可以打印一下

for (AVMetadataMachineReadableCodeObject *obj
in metadataObjects) {

//4.
将来就在这里进行数据处理

//
判断网页并弹出

if ([obj.stringValue
containsString:@"http"]) {

//4.1
创建SafariVC

SFSafariViewController *sfVC = [[SFSafariViewController
alloc]
initWithURL:[NSURL
URLWithString:obj.stringValue]];

//4.2
弹出SafariVC

[self
presentViewController:sfVC animated:YES
completion:nil];

}

}

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