#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>
//扫描条形码
@property (strong, nonatomic) UIView *boxView;
@property (strong, nonatomic) UILabel *lblStatus;
@property (strong, nonatomic) CALayer *scanLayer; //捕捉会话
@property (nonatomic, strong) AVCaptureSession *captureSession;
//展示layer
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;
//扫描二维码
- (void)clickSaoMiaoBtn
{
[self startReading];
}
- (BOOL)startReading {
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.设置输出媒体数据类型为QRCode
[captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObjects:AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code,nil]];
//6.实例化预览图层
_videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
//7.设置预览图层填充方式
[_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
//8.设置图层的frame
[_videoPreviewLayer setFrame:self.view.layer.bounds];
//9.将图层添加到预览view的图层上
[self.view.layer addSublayer:_videoPreviewLayer];
//10.设置扫描范围
captureMetadataOutput.rectOfInterest = CGRectMake(0.2f, 0.2f, 0.6f, 0.6f);
//10.1.扫描框
_boxView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH * 0.2f, HEIGHT * 0.2f, WIDTH - WIDTH * 0.4f, HEIGHT - HEIGHT * 0.4f)];
_boxView.layer.borderColor = [UIColor orangeColor].CGColor;
_boxView.layer.borderWidth = 2.0f;
[self.view addSubview:_boxView];
//10.2.扫描线
_scanLayer = [[CALayer alloc] init];
_scanLayer.frame = CGRectMake(, , _boxView.bounds.size.width, );
_scanLayer.backgroundColor = [UIColor redColor].CGColor;
[_boxView.layer addSublayer:_scanLayer];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1f 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] > ) {
AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:]; dispatch_async(dispatch_get_main_queue(), ^{ self.searchTF.text = [metadataObj stringValue];
[self stopReading];
}); }
} - (void)moveScanLayer:(NSTimer *)timer
{
CGRect frame = _scanLayer.frame;
if (_boxView.frame.size.height < _scanLayer.frame.origin.y +*hl) {
_scanLayer.frame = CGRectMake(, , _boxView.bounds.size.width, );
}else{
frame.origin.y += *hl;
[UIView animateWithDuration:0.01f animations:^{
_scanLayer.frame = frame;
}];
}
} -(void)stopReading{
[_captureSession stopRunning];
_captureSession = nil;
[_scanLayer removeFromSuperlayer];
[_videoPreviewLayer removeFromSuperlayer];
[_boxView removeFromSuperview];
}

iOS原生的AVFoundation扫描二维码/条形码的更多相关文章

  1. iOS 原生库(AVFoundation)实现二维码扫描,封装的工具类,不依赖第三方库,可高度自定义扫描动画及界面(Swift 4.0)

    Create QRScanner.swift file // // QRScanner.swift // NativeQR // // Created by Harvey on 2017/10/24. ...

  2. iOS- 详解如何使用ZBarSDK集成扫描二维码/条形码,点我!

    1.前言 目前市场主流APP里,二维码/条形码集成主要分两种表现形式来集成: a. 一种是调用手机摄像头并打开系统照相机全屏去拍摄 b. 一种是自定义照相机视图的frame,自己控制并添加相关扫码指南 ...

  3. 制作IOS企业版App网页扫描二维码下载安装

    有时候我们需要在XX网站的主页上去扫描二维码下载,那么ios开发中如何做到这一点呢. 我给大家解答一下,这也是在最近工作中用到的部分,在网上了解了一些. 下面给大家分解一下步骤: 1.Plist 和 ...

  4. iOS - 原生的CIFilter生成二维码和条形码

    使用CIFilter可以不引入任何第三方库,就可以生成想要的二维码和条形码,今天简单的介绍一下使用CIFilter生成二维码和条形码.系统CIFilter生成的二维码和条形码的大小有时并不能满足需求, ...

  5. 在iOS中使用ZBar扫描二维码

    最近在做的项目中需要用到二维码扫描功能,之前在Android中使用过ZXing识别二维码,ZXing也有对应的iOS版本,经过了解,ZBar也是一个常用的二维码识别软件,并分别提供了iOS和Andro ...

  6. 在iOS中使用ZBar扫描二维码和条形码

    最近做了个外包项目,里面用到了二维码扫描和微信支付!之前比较熟悉的是ZXing,但是在Xcode7.1里面发现竟然莫名的不支持,木有办法,从网上查了一下还有一种支持二维码扫描的东西,没错就是接下来我要 ...

  7. ZBar只扫描二维码/条形码

    You can add these codes for ImageScanner scanner.setConfig(0, Config.ENABLE, 0); //Disable all the S ...

  8. Android之条码扫描二维码扫描

    Android之条码扫描二维码扫描 二维码条形码扫描,参考技术网址: 1.Apache License 2.0 开源的ZXing项目的简化版 http://xinlanzero.iteye.com/b ...

  9. ionic 扫描二维码 Barcode Scanner、QR Scanner、ZBar

    1.简介 ionic 官方给我们提供了三个扫描二维码条形码插件,分别为: Barcode Scanner 样式好看,类似支付宝的扫描框.速度稍微比最后一个慢几百毫秒,主要问题是 Android 部分手 ...

随机推荐

  1. Tornado,bootstrap文档链接

    http://demo.pythoner.com/itt2zh/ch1.html#ch1-1 http://www.runoob.com/bootstrap/bootstrap-tutorial.ht ...

  2. pip freeze

    总结: 1.输出安装的包信息,并在另一个环境快速安装 Generate output suitable for a requirements file. $ pip freeze docutils== ...

  3. Cache replacement policies 缓存实现算法

    Cache replacement policies - Wikipedia https://en.wikipedia.org/wiki/Cache_replacement_policies Cach ...

  4. 图论最短路——spfa

    今天开始图论的最短路的最后复习,今天自己手打spfa虽然看了一眼书,但是也算是自己打出来的,毕竟很久没打了,而且还是一遍a代码下来15min左右就搞完了,成就感++.所以呢来篇博客记录一下. 香甜的黄 ...

  5. Python 字典 setdefault() 方法

    描述 Python 字典 setdefault() 方法和 get() 方法类似,返回指定键的值,如果键不在字典中,将会添加键并将值设置为一个指定值,默认为None. get() 和 setdefau ...

  6. GET、POST编码问题

    GET请求.POST经常会出现中文乱码的问题,最好约定前后端的编码,一般为UTF-8.但是这里面也是有坑的. 后端设置编码为UTF-8的推荐方式: SpringMVC配置过滤器: <filter ...

  7. php 中date显示时间不对与Linux文件乱码问题

    php 中date显示时间不对解决办法如下1.修改/etc/php.ini文件 在里头中找到data.timezone =去掉它前面的分号';' 然后设置data.timezone = “Asia/S ...

  8. gulp-webserver

    gulp-webserver是开启服务器,通常和gulp-livereload结合使用.而这两个结合使用效果,几乎类似browser-Sync.下面是gulp-webserver和gulp-liver ...

  9. Core Data with Mantle

    Mantle makes it easy to write a simple model layer for your Cocoa or Cocoa Touch application.  Mantl ...

  10. android activity and fragment活动周期

    1.状态 /* 每个活动一共有四种状态 *:1.运行状态,就是栈顶的那个 * 2.暂停状态:就是不处于栈顶,但是依然可见,比如对话框下面的界面 * 3.停止状态:不处于栈顶,并且不可见 * 4.销毁状 ...