iOS 自带AVFoundation库实现二维码扫描

.h里

#import <AVFoundation/AVFoundation.h>

@interface ErWeiViewController : UIViewController<AVCaptureMetadataOutputObjectsDelegate>

-(BOOL)startReading;

-(void)stopReading;

//捕捉会话

@property (nonatomic, strong) AVCaptureSession *captureSession;

//展示layer

@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;

@property(nonatomic, assign)BOOL isReading;

@property (strong, nonatomic) UIView *boxView;

@property (strong, nonatomic) CALayer *scanLayer;

@property (strong, nonatomic)  UIView *viewPreview;

@property (strong, nonatomic)  UILabel *lblStatus;

@property (strong, nonatomic)  UIButton *startBtn;

- (void)startStopReading:(id)sender;

.m里

- (void)viewDidLoad {

[super viewDidLoad];

_captureSession = nil;

_isReading = NO;

[self startReading];

}

-(BOOL)startReading {

_viewPreview = [[UIView alloc]initWithFrame:self.view.bounds];

[self.view addSubview:_viewPreview];

NSError *error;

//1.初始化捕捉设备(AVCaptureDevice),类型为AVMediaTypeVideo

AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

//2.用captureDevice创建输入流

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

if (!input) {

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

return NO;

}

//3.创建媒体数据输出流

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

//4.实例化捕捉会话

_captureSession = [[AVCaptureSession alloc] init];

//4.1.将输入流添加到会话

[_captureSession addInput:input];

//4.2.将媒体输出流添加到会话中

[_captureSession addOutput:captureMetadataOutput];

//5.创建串行队列,并加媒体输出流添加到队列当中

dispatch_queue_t dispatchQueue;

dispatchQueue = dispatch_queue_create("myQueue", NULL);

//5.1.设置代理

[captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];

//5.2.设置输出媒体数据类型为所有类型

captureMetadataOutput.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];

//6.实例化预览图层

_videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];

//7.设置预览图层填充方式

[_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

//8.设置图层的frame

[_videoPreviewLayer setFrame:_viewPreview.layer.bounds];

//9.将图层添加到预览view的图层上

[_viewPreview.layer addSublayer:_videoPreviewLayer];

//10.设置扫描范围

captureMetadataOutput.rectOfInterest = CGRectMake(0.2f, 0.2f, 0.8f, 0.8f);

//10.1.扫描框

_boxView = [[UIView alloc] initWithFrame:CGRectMake(_viewPreview.bounds.size.width * 0.2f, _viewPreview.bounds.size.height * 0.2f, _viewPreview.bounds.size.width - _viewPreview.bounds.size.width * 0.4f, _viewPreview.bounds.size.height - _viewPreview.bounds.size.height * 0.4f)];

_boxView.layer.borderColor = [UIColor greenColor].CGColor;

_boxView.layer.borderWidth = 1.0f;

[_viewPreview addSubview:_boxView];

//10.2.扫描线

_scanLayer = [[CALayer alloc] init];

_scanLayer.frame = CGRectMake(0, 0, _boxView.bounds.size.width, 1);

_scanLayer.backgroundColor = [UIColor brownColor].CGColor;

[_boxView.layer addSublayer:_scanLayer];

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.2f target:self selector:@selector(moveScanLayer:) userInfo:nil repeats:YES];

[timer fire];

//10.开始扫描

[_captureSession startRunning];

return YES;

}

#pragma mark - AVCaptureMetadataOutputObjectsDelegate协议方法

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

{

//判断是否有数据

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

AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];

//判断回传的数据类型  captureMetadataOutput.metadataObjectTypes  数组里的类型进行遍历对比 添加if判断类型,

//        下面只以AVMetadataObjectTypeQRCode 为例子

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

//            [_lblStatus performSelectorOnMainThread:@selector(setText:) withObject:[metadataObj stringValue] waitUntilDone:NO];

//            NSLog(@"11111 = %@", _lblStatus.text);

//            [self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];

//            _isReading = NO;

//        }

}

[self dismissViewControllerAnimated:YES completion:^{

}];

}

//实现计时器方法moveScanLayer:(NSTimer *)timer

- (void)moveScanLayer:(NSTimer *)timer

{

CGRect frame = _scanLayer.frame;

if (_boxView.frame.size.height < _scanLayer.frame.origin.y) {

frame.origin.y = 0;

_scanLayer.frame = frame;

}else{

frame.origin.y += 5;

[UIView animateWithDuration:0.1 animations:^{

_scanLayer.frame = frame;

}];

}

}

- (void)startStopReading:(id)sender {

if (!_isReading) {

if ([self startReading]) {

[_startBtn setTitle:@"Stop" forState:UIControlStateNormal];

[_lblStatus setText:@"Scanning for QR Code"];

}

}

else{

[self stopReading];

[_startBtn setTitle:@"Start!" forState:UIControlStateNormal];

}

_isReading = !_isReading;

}

-(void)stopReading{

[_captureSession stopRunning];

_captureSession = nil;

[_scanLayer removeFromSuperlayer];

[_videoPreviewLayer removeFromSuperlayer];

}

时间: 2024-07-30 20:06:22

iOS 自带AVFoundation库实现二维码扫描的相关文章

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

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

AVFoundation二维码扫描限制区域

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

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

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

苹果原生二维码扫描功能——可限制扫描区域

使用原生的好处就是扫描特别快效率特别高,使用  AVFoundation 来进行二维码扫描,更主要的是限制扫描二维码的范围.(默认的是全屏扫描) 首先是要用到的几个类 @property ( strong , nonatomic ) AVCaptureDevice * device; @property ( strong , nonatomic ) AVCaptureDeviceInput * input; @property ( strong , nonatomic ) AVCaptureMe

【转】 iOS使用AVFoundation实现二维码扫描

原文:http://strivingboy.github.io/blog/2014/11/08/scan-qrcode/ 关于二维码扫描有不少优秀第三方库如: ZBar SDK 里面有详细的文档,相应介绍也非常多,如:http://rdcworld-iphone.blogspot.in/2013/03/how-to-use-barcode-scanner-br-and-qr-in.html ZXing google推出的开源项目,相应介绍如:http://blog.devtang.com/blo

Ios二维码扫描(系统自带的二维码扫描)

Ios二维码扫描 这里给大家介绍的时如何使用系统自带的二维码扫描方法和一些简单的动画! 操作步骤: 1).首先你需要搭建UI界面如图:下图我用了俩个imageview和一个label 2).你需要在你当前的控制器中导入 #import <AVFoundation/AVFoundation.h> <AVCaptureMetadataOutputObjectsDelegate>代理 3).在@interface中定义 @property (nonatomic,strong)AVCapt

iOS使用AVFoundation实现二维码扫描(ios7以上)——转载

关于二维码扫描有不少优秀第三方库: ZBar SDK 里面有详细的文档,相应介绍也非常多,如:http://rdcworld-iphone.blogspot.in/2013/03/how-to-use-barcode-scanner-br-and-qr-in.html ZXing google推出的开源项目,相应介绍如:http://blog.devtang.com/blog/2012/12/23/use-zxing-library/ 关于AVFoundation AVFoundation 是一

iOS使用AVFoundation实现二维码扫描

实现过程如下: Step1:需要导入:AVFoundation Framework 包含头文件: #import <AVFoundation/AVFoundation.h> Step2:设置捕获会话 设置 AVCaptureSession 和 AVCaptureVideoPreviewLayer 成员 1 2 3 4 5 6 7 8 9 10 #import <AVFoundation/AVFoundation.h> static const char *kScanQRCodeQu

iOS中 基于LBXScan库二维码扫描 韩俊强的博客

每日更新关注:http://weibo.com/hanjunqiang  新浪微博 首先声明这个二维码扫描是借助于zxing. 功能模块都完全封装好了,不过界面合你口味,直接使用就好,如果不合口味,后面告诉你怎么修改. - 1.cocoaPods导入pod 'LBXScan', '~> 1.1.1' 导入方法看这: - 2.将JQScan文件夹拖到你的工程中(这个是写好的). <code class="language-Objective-C hljs objectivec has-