http://i.cnblogs.com/EditPosts.aspx?postid=5288517

//写在最前

/*

AVFoundation原生框架的好处就是扫描特别快效率特别高,但是可能会遇到一个问题就是不知道怎么去限制扫描范围。

二维码扫描有关优秀第三方库:---- ZXing google推出的开源项目-----ZBar SDK 里面也有详细的文档

*/

引入头文件导入流媒体需要的框架

@import AVFoundation;

//遵守协议 AVCaptureMetadataOutputObjectsDelegate

@property (nonatomic,strong)AVCaptureSession *session;

@property (nonatomic,strong)AVCaptureVideoPreviewLayer *layer;

- (void)viewDidLoad {

[super viewDidLoad];

[self startRunningSession];

[self.view bringSubviewToFront:self.v];

self.animV = [[UIView alloc]initWithFrame:CGRectMake(92, 124, 190, 2)];

self.animV.backgroundColor = [UIColor redColor];

[self.view addSubview:self.animV];

if (!_t) {

self.t = [NSTimer scheduledTimerWithTimeInterval:4.2/24 target:self selector:@selector(animateShow) userInfo:nil repeats:YES];

}

}

- (void)animateShow{

CGRect frame = self.animV.frame;

frame.origin.y += 8;

if (frame.origin.y >= 316) {

frame.origin.y = 116;

}

self.animV.frame = frame;

}

- (void)startRunningSession

// 获取 AVCaptureDevice 实例

NSError * error;

AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

[captureDevice setTorchMode:AVCaptureTorchModeOn];   //开启照明

// 初始化输入流

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

if (!input) {

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

}

// 创建会话

_session = [[AVCaptureSession alloc] init];

if ([_session canAddInput:input]) {

// 添加输入流

[_session addInput:input];

}

// 初始化输出流

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

if ([_session canAddOutput:captureMetadataOutput]) {

// 添加输出流

[_session addOutput:captureMetadataOutput];

}

// 创建dispatch queue.

dispatch_queue_t dispatchQueue;

dispatchQueue = dispatch_queue_create(kScanName, NULL);

[captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

[captureMetadataOutput setRectOfInterest:CGRectMake(100/667.0,87/375.0,200/667.0, 200/375.0)];

// 设置元数据类型 AVMetadataObjectTypeQRCode

[captureMetadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];

//value CGRectMake(0, 0, 1, 1). Metadata objects whose bounds do not intersect with the rectOfInterest will not be returned.

// 创建输出对象

_layer = [AVCaptureVideoPreviewLayer layerWithSession:_session];

[_layer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

[_layer setFrame:self.view.layer.frame];

[self.view.layer addSublayer:_layer];

// 开始会话

[_session startRunning];

}

#pragma  -   mark   -     AVCaptureMetadataOutputObjectsDelegate

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

NSLog(@"-------");

if (metadataObjects && metadataObjects.count) {

AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];

NSString *result;

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

result = metadataObj.stringValue;

NSLog(@"result:%@",result);

} else {

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

}

[_session stopRunning];

_session = nil;

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"QRCodeInfo"

message:result

delegate:nil

cancelButtonTitle:@"cancel"

otherButtonTitles: nil];

[alert show];

}

}

#import "cornView.h"

@import CoreGraphics;

//给扫描控制器加一个View进行遮盖处理就是上面的self.v

- (void)drawRect:(CGRect)rect {

UIBezierPath *p = [UIBezierPath bezierPathWithRect:CGRectMake(87, 56, 200, 200)];

//  NSLog(@"0000");

[[UIColor clearColor]setFill];

填充为中间没有颜色

[p fill];

填充之外的不烦

[p fillWithBlendMode:kCGBlendModeClear alpha:1];

}

//写在最后

很小的二维码,边长不到1cm,于是就修改了 sessionPreset 为 1080p 的,当时用的是ZXing, 当把图片质量改清楚时,也造成了性能的下降,基本打开扫描界面就会报memoryWarning,但是也确实解决了小二维码扫描的问题。

AVCaptureSession 可以设置 sessionPreset 属性,这个决定了视频输入每一帧图像质量的大小。

  • AVCaptureSessionPreset320x240
  • AVCaptureSessionPreset352x288
  • AVCaptureSessionPreset640x480
  • AVCaptureSessionPreset960x540
  • AVCaptureSessionPreset1280x720
  • AVCaptureSessionPreset1920x1080

以上列举了部分的属性值,分别代表输入图片质量大小,一般来说AVCaptureSessionPreset640x480就够使用,但是如果要保证较小的二维码图片能快速扫描,最好设置高些,如AVCaptureSessionPreset1920x1080(就是我们常说的1080p).

提升扫描速度和性能的就是设置解析的范围,在zbar和zxing中就是scanCropAVFoundation中设置 AVCaptureMetadataOutput 的 rectOfInterest 属性来配置解析范围。

captureOutput.rectOfInterest = CGRectMake(cropRect.origin.y/size.height,
cropRect.origin.x/size.width,
cropRect.size.height/size.height,
cropRect.size.width/size.width);

rectOfInterest是基于图像的大小裁剪的。 》》》此博文源于各路汇总。

iOS之Scanning的实现的更多相关文章

  1. IOS零碎技术整理(3)-获取wifi列表

    1.   该功能实现基于MobileApple80211框架来进行开发,而目前该框架成为了私有框架,其中的API均为私有API. 如果使用这些API可能导致应用不能上app store或者ios版本升 ...

  2. iOS蓝牙开发(二)蓝牙相关基础知识

    原文链接: http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-1.html iOS蓝牙开发(一)蓝牙相关基础知识: 蓝牙常见名称和缩写 MFI ====== ...

  3. iOS蓝牙开发(一)蓝牙相关基础知识(转)

    转载自:http://www.cocoachina.com/ios/20150915/13454.html 原文作者:刘彦玮 蓝牙常见名称和缩写 MFI ======= make for ipad , ...

  4. ios 可变参数(va_list,va_start,va_end)

    例如:UIAlertView的init方法中的otherButtonTitles:(NSString *)otherButtonTitles, ...等多个可变参数. ios实现传递不定长的多个参数的 ...

  5. iOS CoreBluetooth 教程

    去App Store搜索并下载“LightBlue”这个App,对调试你的app和理解Core Bluetooth会很有帮助. ================================ Cor ...

  6. iOS:原生二维码扫描

    做iOS的二维码扫描,有两个第三方库可以选择,ZBar和ZXing.今天要介绍的是iOS7.0后AVFoundation框架提供的原生二维码扫描. 首先需要添加AVFoundation.framewo ...

  7. iOS蓝牙开发

    蓝牙常见名称和缩写 MFI ======= make for ipad ,iphone, itouch 专们为苹果设备制作的设备 BLE ==== buletouch low energy,蓝牙4.0 ...

  8. iOS - Bluetooth 蓝牙

    1.蓝牙介绍 具体讲解见 蓝牙 技术信息 蓝牙协议栈 2.iBeacon 具体讲解见 Beacon iBeacon 是苹果公司 2013 年 9 月发布的移动设备用 OS(iOS7)上配备的新功能.其 ...

  9. iOS关于蓝牙连接的简单介绍与使用

    下面是两台iPhone6连接同一台蓝牙设备的结果: **成功连接**** peripheral: <CBPeripheral: 0x1700f4500, identifier = 50084F6 ...

随机推荐

  1. Unity-Animator深入系列---StateMachineBehaviour状态机脚本学习

    回到 Animator深入系列总目录 首先这个脚本必须继承自StateMachineBehaviour public class MySMB : StateMachineBehaviour { pub ...

  2. SecureCRT issue "Could not open clipboard: Assess is denied" (无法打开粘贴板:访问被拒绝)

    I got an issue when copying some line/word (actually just select the context ) in the Linux terminal ...

  3. Uva 1220,Hali-Bula 的晚会

    题目链接:https://uva.onlinejudge.org/external/12/1220.pdf 题意: 公司n个人,形成一个数状结构,选出最大独立集,并且看是否是唯一解. 分析: d(i) ...

  4. 关于 android 中 postDelayed方法的讲解

    代码如下: 这是一种可以创建多线程消息的函数 使用方法: 1,首先创建一个Handler对象 Handler handler=new Handler(); 2,然后创建一个Runnable对象 Run ...

  5. ubuntu下安装花生壳

    下载地址:http://hsk.oray.com/download/#type=linux 官方的文档: 花生壳(公网版) for linux的安装以及使用 Linux花生壳(公网版)将大大简化大家的 ...

  6. Android 内存分析工具 - LogCat GC

    一.GC_Reason 触发垃圾回收的回收的集中原因: 类型 描述 GC_CONCURRENT 内存使用将满时,并发的进行垃圾回收. GC_FOR_MALLOC 当内存已满应用尝试分配内存时会出触发垃 ...

  7. Winform容器标签 打印标签 对话框控件

    一.容器标签 布局: Anchor:锁定位置,指定与窗口容器的边缘位置,会随着窗口大小的改变而改变: Dock:填充窗口的位置.一般与容器标签同时使用. 1.Panel:对控件进行分组.可以独立布局, ...

  8. Android编译中m、mm、mmm的区别

    准备工作 在AndroidSource Code中有envsetup.sh档案,当执行过此档案后,可以大幅将build的过程简单化.自动化 此档案在src(android source code 位置 ...

  9. jQuery Validation remote的缓存请求

    不知大家有没有遇到,用jQuery Validation(本文讨论的版本为jQuery Validation Plugin 1.11.1)用remote方式做校验时,如果验证元素的值保持一致,进行多次 ...

  10. Spring.Net 技术简介 IOC and DI

    一 简单介绍            IOC 控制转移,就是将创建放到容器里,从而达到接耦合的目的,DI是 在容器创建对象的时候,DI读取配置文件,然后给对象赋默认值,两者一般结合使用,实现注入.   ...