iOS 原生二维码扫描,带扫描框和扫描过程动画

在代码中使用了相对布局框架Masonry

准备两张图片,一张是扫描边框,一张是扫描时的细线分别命名

scanFrame.png和scanLine.png并提前放入工程

导入相对布局头文件

#define MAS_SHORTHAND

#define MAS_SHORTHAND_GLOBALS

#import "Masonry.h"

导入依赖头文件

#import <AVFoundation/AVFoundation.h>

具体代码如下:

static const char *KEYScanQRCodeQueueName = "ScanQRCodeQueue";

@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>

@property (nonatomic,strong)UIImageView *line;;

@property (nonatomic,strong) AVCaptureSession *scanSession;

@property (nonatomic,strong) AVCaptureVideoPreviewLayer *scanLayer;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler: ^(BOOL granted) {

if (granted) {

[self startReading];

} else {

NSLog(@"请授权访问相机");        }

}];

}

//开始扫描

- (BOOL)startReading

{

// 获取 AVCaptureDevice 实例

NSError * error;

AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

// 初始化输入流

AVCaptureDeviceInput *inputStream = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];

if (!inputStream) {

NSLog(@"%@", [error localizedDescription]);

return NO;

}

// 创建会话

_scanSession = [[AVCaptureSession alloc] init];

// 添加输入流

[_scanSession addInput:inputStream];

// 初始化输出流

AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];

// 添加输出流

[_scanSession addOutput:captureMetadataOutput];

// 创建dispatch queue.

dispatch_queue_t dispatchQueue;

dispatchQueue = dispatch_queue_create(KEYScanQRCodeQueueName, NULL);

[captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];

// 设置元数据类型 AVMetadataObjectTypeQRCode

[captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];

// 创建输出对象

_scanLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_scanSession];

[_scanLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

[_scanLayer setFrame:self.view.bounds];

[self.view.layer addSublayer:_scanLayer];

[self setOverlayPickerView];

// 开始会话

[_scanSession startRunning];

return YES;

}

//结束扫描

- (void)stopReading

{

[_scanSession stopRunning];

_scanSession = nil;

}

//扫描结果

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects

fromConnection:(AVCaptureConnection *)connection

{

if (metadataObjects != nil && [metadataObjects count] > 0) {

AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];

NSString *result;

if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {

result = metadataObj.stringValue;

} else {

NSLog(@"不是二维码");

}

[self performSelectorOnMainThread:@selector(reportScanResult:) withObject:result waitUntilDone:NO];

}

}

//处理结果

- (void)reportScanResult:(NSString *)result

{

[self stopReading];

NSLog(@"%@",result);

}

//添加识别途中动画

- (void)setOverlayPickerView

{

//左侧的view

UIView *leftView = [UIView new];

leftView.alpha = 0.5;

leftView.backgroundColor = [UIColor blackColor];

[self.view addSubview:leftView];

[leftView makeConstraints:^(MASConstraintMaker *make) {

make.top.equalTo(self.view.top);

make.bottom.equalTo(self.view.bottom);

make.left.equalTo(self.view.left);

make.width.equalTo(30);

}];

//右侧的view

UIView *rightView = [UIView new];

rightView.alpha = 0.5;

rightView.backgroundColor = [UIColor blackColor];

[self.view addSubview:rightView];

[rightView makeConstraints:^(MASConstraintMaker *make) {

make.top.equalTo(self.view.top);

make.bottom.equalTo(self.view.bottom);

make.right.equalTo(self.view.right);

make.width.equalTo(30);

}];

//最上部view

UIView* upView = [UIView new];

upView.alpha = 0.5;

upView.backgroundColor = [UIColor blackColor];

[self.view addSubview:upView];

[upView makeConstraints:^(MASConstraintMaker *make) {

make.top.equalTo(self.view.top);

make.left.equalTo(leftView.right);

make.right.equalTo(rightView.left);

make.height.equalTo(30);

}];

//扫描框

UIImageView *centerView = [UIImageView new];

centerView.center = self.view.center;

centerView.image = [UIImage imageNamed:@"scanFrame.png"];

centerView.contentMode = UIViewContentModeScaleAspectFit;

centerView.backgroundColor = [UIColor clearColor];

[self.view addSubview:centerView];

[centerView makeConstraints:^(MASConstraintMaker *make) {

make.top.equalTo(upView.bottom);

make.left.equalTo(leftView.right);

make.right.equalTo(rightView.left);

make.height.equalTo(upView.width);

}];

//底部view

UIView * downView = [UIView new];

downView.alpha = 0.5;

downView.backgroundColor = [UIColor blackColor];

[self.view addSubview:downView];

[downView makeConstraints:^(MASConstraintMaker *make) {

make.top.equalTo(centerView.bottom);

make.left.equalTo(leftView.right);

make.right.equalTo(rightView.left);

make.bottom.equalTo(self.view.bottom);

}];

_line = [UIImageView new];

_line.image = [UIImage imageNamed:@"scanLine.png"];

_line.contentMode = UIViewContentModeScaleAspectFill;

_line.backgroundColor = [UIColor clearColor];

[self addAnimation];

[self.view addSubview:_line];

[_line makeConstraints:^(MASConstraintMaker *make) {

make.top.equalTo(centerView.top);

make.left.equalTo(centerView.left);

make.right.equalTo(centerView.right);

make.height.equalTo(2);

}];

//提示信息

UILabel *msg = [UILabel new];

msg.backgroundColor = [UIColor clearColor];

msg.textColor = [UIColor whiteColor];

msg.textAlignment = NSTextAlignmentCenter;

msg.font = [UIFont systemFontOfSize:16];

msg.text = @"将二维码放入框内,可识别车票";

[self.view addSubview:msg];

[msg makeConstraints:^(MASConstraintMaker *make) {

make.top.equalTo(centerView.bottom).offset(20);

make.left.equalTo(self.view.left);

make.right.equalTo(self.view.right);

make.height.equalTo(30);

}];

}

- (void)addAnimation{

CABasicAnimation *animation = [self moveYTime:2 fromY:[NSNumber numberWithFloat:0] toY:[NSNumber numberWithFloat:screenWidth-60-2] rep:OPEN_MAX];

[line.layer addAnimation:animation forKey:@"animation"];

}

-(CABasicAnimation *)moveYTime:(float)time fromY:(NSNumber *)fromY toY:(NSNumber *)toY rep:(int)rep

{

CABasicAnimation *animationMove = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];

[animationMove setFromValue:fromY];

[animationMove setToValue:toY];

animationMove.duration = time;

animationMove.delegate = self;

animationMove.repeatCount  = rep;

animationMove.fillMode = kCAFillModeForwards;

animationMove.removedOnCompletion = NO;

animationMove.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

return animationMove;

}

@end

说明scanFrame.PNG是扫描时候的边框需要自己提前放到自己工程里面。scanLine.png是扫描时候从上到下的细线提前放入工程

上一篇:Android BaseAdapter ListView (明星简介列表)


下一篇:centos 安装python3与Python2并存,并解决"smtplib" object has no attribute 'SMTP_SSL'的错误