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是扫描时候从上到下的细线提前放入工程

时间: 2024-12-09 06:50:34

iOS 原生二维码扫描,带扫描框和扫描过程动画的相关文章

二维码扫描(iOS原生二维码扫描)

一.关于二维码扫描的第三方库有很多:例如比较常用的两个 1.ZBar SDK ZBar为我们提供了两种使用方式,一种是直接调用ZBar提供的ZBarReaderViewController打开一个扫描界面,另一种方式是使用ZBar提供的可以嵌在其他视图中的ZBarReaderView,实际项目中我们更可能会使用第二种方式,这可以让我们对界面做更多的定制,详细的百度查找相关文档来看. 2.ZXing(Github镜像地址)是一个开源的条码生成和扫描库(开源协议为Apache2.0).它不但支持众多

iOS 原生二维码扫描(可限制扫描区域)

写这篇文章的主要原因不是展示如何使用 AVFoundation   来进行二维码扫描,更主要的是限制扫描二维码的范围.(因为默认的是全屏扫描) 项目遇到扫描二维码的功能需求,这里我放弃了使用三方库,而采用了苹果原生的扫描. 原生的好处就是扫描特别快效率特别高,但是遇到一个问题就是不知道怎么去限制扫描范围. 还是先简单说一下怎么使用来进行二维码扫描吧. 首先是要用到的几个类 @property (strong,nonatomic)AVCaptureDevice * device; @propert

【转】 iOS 原生二维码扫描(可限制扫描区域)

在用 AVFoundation 完成扫码后,遇到2个问题: 1,如何限制扫描范围? 2.条形码如何扫描? 一位朋友的文章帮助了我,特地转来,可以帮到有需要的朋友. 原文:http://www.2cto.com/kf/201411/356046.html 写这篇文章的主要原因不是展示如何使用 AVFoundation 来进行二维码扫描,更主要的是限制扫描二维码的范围.(因为默认的是全屏扫描) 项目遇到扫描二维码的功能需求,这里我放弃了使用三方库,而采用了苹果原生的扫描. 原生的好处就是扫描特别快效

iOS 原生二维码扫描

哎 关于限制扫码范围的控制,弄了老夫好久,还是不尽如意 .h 1 // 2 // LIUScanTwoDimensionalCode.h 3 // YouYouShoppingCenter 4 // 5 // Created by liujun on 15/7/28. 6 // Copyright (c) 2015年 刘俊. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 #import <AVFoundation/AVF

iOS 花式二维码生成和二维码识别

iOS 原生的二维码识别非常之棒,反正比 ZXing 和 ZBar 效果都好些,所以以后打算尽量用原生的二维码识别,然后最近把原生的二维码生成也顺便做了一遍,并且在原有基础上加了一些样式参数,封了一个小库方便以后使用. 项目地址:https://github.com/EyreFree/EFQRCode EFQRCode 是一个用 Swift 编写的用来生成和识别二维码的库,它基于系统二维码生成与识别进行开发. 生成:利用输入的水印图/图标等资源生成各种艺术二维码: 识别:识别率比 iOS 原生二

iOS之原生二维码扫描

做iOS的二维码扫描,有两个第三方库可以选择,ZBar和ZXing.今天要介绍的是iOS7.0后AVFoundation框架提供的原生二维码扫描. 首先需要添加AVFoundation.framework框架到你工程中build phase的"Link Binary With Libraries"之下,然后就可以开始了. 一.做好准备工作,搭建UI IBOutlet.IBAction如下: @property (weak, nonatomic) IBOutlet UIView *vie

iOS:原生二维码扫描

做iOS的二维码扫描,有两个第三方库可以选择,ZBar和ZXing.今天要介绍的是iOS7.0后AVFoundation框架提供的原生二维码扫描. 首先需要添加AVFoundation.framework框架到你工程中build phase的"Link Binary With Libraries"之下,然后就可以开始了. 一.做好准备工作,搭建UI UI效果如图 IBOutlet.IBAction如下: @property (weak, nonatomic) IBOutlet UIVi

iOS开发-二维码扫描和应用跳转

iOS开发-二维码扫描和应用跳转 序言 前面我们已经调到过怎么制作二维码,在我们能够生成二维码之后,如何对二维码进行扫描呢? 在iOS7之前,大部分应用中使用的二维码扫描是第三方的扫描框架,例如ZXing或者ZBar.使用时集成麻烦,出错也不方便调试.在iOS7之后,苹果自身提供了二维码的扫描功能,从效率上来说,原生的二维码远高于这些第三方框架.本文讲解如何使用原生框架实现二维码扫描功能,并且进行扫描后的项目跳转.ps:本期的源代码会在文章结尾给出链接 扫描相关类 二维码扫描需要获取摄像头并读取

iOS之二维码的制作与扫描

IOS之二维码的制作与扫描     二维码SDK的Dome“QRCode”中制作libqrencode库文件,扫描ZBarSDK库文件1.制作二维码  /*字符转二维码导入 libqrencode文件添加  #import "QRCodeGenerator.h"@property (strong , nonatomic) UIImageView* qRImageView;- (void)viewDidLoad{    [super viewDidLoad];    self.qRIma