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是扫描时候从上到下的细线提前放入工程
iOS 原生二维码扫描,带扫描框和扫描过程动画的更多相关文章
- iOS 原生二维码扫描和生成
代码地址如下:http://www.demodashi.com/demo/12551.html 一.效果预览: 功能描述:WSLNativeScanTool是在利用原生API的条件下封装的二维码扫描工 ...
- iOS 原生二维码扫描(可限制扫描区域)
篇文章的主要原因不是展示如何使用 AVFoundation 来进行二维码扫描,更主要的是限制扫描二维码的范围.(因为默认的是全屏扫描) 项目遇到扫描二维码的功能需求,这里我放弃了使用三方库,而采用了 ...
- 【转】 iOS 原生二维码扫描(可限制扫描区域)
在用 AVFoundation 完成扫码后,遇到2个问题: 1,如何限制扫描范围? 2.条形码如何扫描? 一位朋友的文章帮助了我,特地转来,可以帮到有需要的朋友. 原文:http://blog.csd ...
- iOS:原生二维码扫描
做iOS的二维码扫描,有两个第三方库可以选择,ZBar和ZXing.今天要介绍的是iOS7.0后AVFoundation框架提供的原生二维码扫描. 首先需要添加AVFoundation.framewo ...
- iOS开发-二维码扫描和应用跳转
iOS开发-二维码扫描和应用跳转 序言 前面我们已经调到过怎么制作二维码,在我们能够生成二维码之后,如何对二维码进行扫描呢? 在iOS7之前,大部分应用中使用的二维码扫描是第三方的扫描框架,例如Z ...
- Firemonkey 原生二维码扫描优化
之前用了ZXing的Delphi版本,运行自带的例子,速度非常慢,与安卓版本的相比查了很多,因此打算使用集成jar的方法,但是总觉得美中不足. 经过一番研究,基本上解决了问题. 主要有两方面的优化: ...
- iOS - QRCode 二维码
1.QRCode 在 iOS7 以前,在 iOS 中实现二维码和条形码扫描,我们所知的有,两大开源组件 ZBar 与 ZXing. 这两大组件我们都有用过,这里总结下各自的缺点: 1.ZBar 在扫描 ...
- IOS开发 二维码功能的实现
原帖地址:http://yul100887.blog.163.com/blog/static/20033613520121020611299/ 如今二维码随处可见,无论是实物商品还是各种礼券都少不了二 ...
- iOS系统原生 二维码的生成、扫描和读取(高清、彩色)
由于近期工作中遇到了个需求:需要将一些固定的字段 在多个移动端进行相互传输,所以就想到了 二维码 这个神奇的东东! 现在的大街上.连个摊煎饼的大妈 都有自己的二维码来让大家进行扫码支付.可见现在的二维 ...
随机推荐
- dialog中的button动态设置为disable[转]
我们再写dialog的时候,会时常有这样一种需求,希望通过某些条件将dialog的button设置为disable的. 基本的命令就是将“确定”这个button设置为disable(false). 如 ...
- ios NSString 去除空格和回车
去除两端空格 NSString *temp = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCh ...
- DefaultResouceLoader的设计
它是什么 DefaultResourceLoader是Spring中的ResourceLoader的默认实现类,也是AbstractApplicationContext的父类,它也可以单独使用,用来从 ...
- HID Boot device.
整理这篇文章的目的: 客户会有用到遥控器部分(遥控器操作flow:当按下某个键时,MCU会透过UR送command给TP,TP吃到后再透过微软标准的keyboard上报) 要求:在BIOS设定阶段,遥 ...
- linux服务器内存占用太高-释放内存
修改/proc/sys/vm/drop_caches,释放Slab占用的cache内存空间(参考drop_caches的官方文档): Writing to this will cause the ke ...
- jsp DAO设计模式
DAO(Data Access Objects)设计模式是属于J2EE体系架构中的数据层的操作. 一.为什么要用DAO? 比较在JSP页面中使用JDBC来连接数据库,这样导致了JSP页面中包含了大量的 ...
- JS 函数参数
1.简单的无参函数调用 function Test1(Func) { Func(); } function Test2() { alert("我要被作为函数参数啦!"); } // ...
- Swift缩水版MJExtension - Reflect的基本使用
github:https://github.com/CharlinFeng/Reflect 直接拖拽Reflect文件夹到您的项目中即可,无任何第三方依赖!文件夹结构说明:.Coding 归档相关.R ...
- jQuery操作元素
通常,我们在创建元素时,会使用以下代码: var p = document.createElement("p"); p.innerText = "this is para ...
- debian系(Ubuntu)安装jenkins(持续集成)
wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add - sudo sh -c 'ec ...