iOS中 扫描二维码/生成二维码具体解释 韩俊强的博客
近期大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家!
每日更新关注:http://weibo.com/hanjunqiang
新浪微博
指示根视图:
self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[SecondViewController new]];
每日更新关注:http://weibo.com/hanjunqiang
新浪微博
生成二维码:
// Created by 韩俊强 on 15/11/27.
// Copyright (c) 2015年 韩俊强. All rights reserved.
// #import "SecondViewController.h" @interface SecondViewController () @property (nonatomic, strong) UITextField *tfCode;
@property (nonatomic, strong) UIButton *btnGenerate;
@property (nonatomic, strong) UIImageView *imageView;
@end @implementation SecondViewController - (void)viewDidLoad {
[super viewDidLoad];
CGSize windowSize = [UIScreen mainScreen].bounds.size; self.tfCode = [[UITextField alloc] initWithFrame:CGRectMake(10, 64, windowSize.width-100, 40)];
[self.view addSubview:self.tfCode];
self.tfCode.borderStyle = UITextBorderStyleRoundedRect; self.btnGenerate = [[UIButton alloc] initWithFrame:CGRectMake(windowSize.width-100, 64, 90, 40)];
[self.view addSubview:self.btnGenerate];
[self.btnGenerate addTarget:self action:@selector(actionGenerate) forControlEvents:UIControlEventTouchUpInside];
self.btnGenerate.backgroundColor = [UIColor lightGrayColor];
[self.btnGenerate setTitle:@"生成" forState:UIControlStateNormal];
[self.btnGenerate setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
[self.view addSubview:self.imageView];
self.imageView.center = CGPointMake(windowSize.width/2, windowSize.height/2); self.tfCode.text = @"http://www.baidu.com";
}
- (void)actionGenerate
{
NSString *text = self.tfCode.text; NSData *stringData = [text dataUsingEncoding: NSUTF8StringEncoding]; //生成
CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[qrFilter setValue:stringData forKey:@"inputMessage"];
[qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"]; UIColor *onColor = [UIColor blackColor];
UIColor *offColor = [UIColor whiteColor]; //上色
CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor" keysAndValues:@"inputImage",qrFilter.outputImage,@"inputColor0",[CIColor colorWithCGColor:onColor.CGColor],@"inputColor1",[CIColor colorWithCGColor:offColor.CGColor],nil]; CIImage *qrImage = colorFilter.outputImage; //绘制
CGSize size = CGSizeMake(300, 300);
CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:qrImage fromRect:qrImage.extent];
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationNone);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); CGImageRelease(cgImage); self.imageView.image = codeImage;
}
扫描二维码:
// Created by 韩俊强 on 15/11/27.
// Copyright (c) 2015年 韩俊强. All rights reserved.
// #import "RootViewController.h"
#import <AVFoundation/AVFoundation.h> @interface RootViewController ()<AVCaptureMetadataOutputObjectsDelegate,UIAlertViewDelegate> @property (nonatomic, strong) UIView *scanRectView;
// 硬件设备
@property (strong, nonatomic) AVCaptureDevice *device;
//输入设备
@property (strong, nonatomic) AVCaptureDeviceInput *input;
//输出设备
@property (strong, nonatomic) AVCaptureMetadataOutput *output;
//桥梁.连接输入和输出设备,
@property (strong, nonatomic) AVCaptureSession *session;
@property (strong, nonatomic) AVCaptureVideoPreviewLayer *preview; @end @implementation RootViewController - (void)viewDidLoad {
[super viewDidLoad];
CGSize windowSize = [UIScreen mainScreen].bounds.size; CGSize scanSize = CGSizeMake(windowSize.width*3/4, windowSize.width*3/4);
CGRect scanRect = CGRectMake((windowSize.width-scanSize.width)/2, (windowSize.height-scanSize.height)/2, scanSize.width, scanSize.height); scanRect = CGRectMake(scanRect.origin.y/windowSize.height, scanRect.origin.x/windowSize.width, scanRect.size.height/windowSize.height,scanRect.size.width/windowSize.width); self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil]; self.output = [[AVCaptureMetadataOutput alloc]init];
[self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; self.session = [[AVCaptureSession alloc]init];
[self.session setSessionPreset:([UIScreen mainScreen].bounds.size.height<500)? AVCaptureSessionPreset640x480:AVCaptureSessionPresetHigh];
[self.session addInput:self.input];
[self.session addOutput:self.output];
self.output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode];
self.output.rectOfInterest = scanRect; self.preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.preview.frame = [UIScreen mainScreen].bounds;
[self.view.layer insertSublayer:self.preview atIndex:0]; self.scanRectView = [UIView new];
[self.view addSubview:self.scanRectView];
self.scanRectView.frame = CGRectMake(0, 0, scanSize.width, scanSize.height);
self.scanRectView.center = CGPointMake(CGRectGetMidX([UIScreen mainScreen].bounds), CGRectGetMidY([UIScreen mainScreen].bounds));
self.scanRectView.layer.borderColor = [UIColor redColor].CGColor;
self.scanRectView.layer.borderWidth = 1; //開始捕获
[self.session startRunning]; } - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
if ( (metadataObjects.count==0) )
{
return;
} if (metadataObjects.count>0) { [self.session stopRunning]; AVMetadataMachineReadableCodeObject *metadataObject = metadataObjects.firstObject;
//输出扫描字符串 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:metadataObject.stringValue message:@"" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil]; [alert show];
}
} - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
[self.session startRunning];
}
每日更新关注:http://weibo.com/hanjunqiang
新浪微博
终于效果:(因为扫描二维码无法展示效果,所以自己动手真机測试吧!)
每日更新关注:http://weibo.com/hanjunqiang
新浪微博
IOS7之前,开发人员进行扫码编程时,通常会借助第三方库。经常使用的是ZBarSDK。IOS7之后,系统的AVMetadataObject类中,为我们提供了解析二维码的接口。经过測试。使用原生API扫描和处理的效率很高。远远高于第三方库。
一、用法演示样例
官方提供的接口很easy,代码例如以下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate> //用于处理採集信息的代理 { AVCaptureSession * session; //输入输出的中间桥梁 } @end @implementation ViewController - ( void )viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //获取摄像设备 AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; //创建输入流 AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; //创建输出流 AVCaptureMetadataOutput * output = [[AVCaptureMetadataOutput alloc]init]; //设置代理 在主线程里刷新 [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; //初始化链接对象 session = [[AVCaptureSession alloc]init]; //高质量採集率 [session setSessionPreset:AVCaptureSessionPresetHigh]; [session addInput:input]; [session addOutput:output]; //设置扫码支持的编码格式(例如以下设置条形码和二维码兼容) output.metadataObjectTypes=@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code]; AVCaptureVideoPreviewLayer * layer = [AVCaptureVideoPreviewLayer layerWithSession:session]; layer.videoGravity=AVLayerVideoGravityResizeAspectFill; layer.frame=self.view.layer.bounds; [self.view.layer insertSublayer:layer atIndex:0]; //開始捕获 [session startRunning]; } |
之后我们的UI上已经能够看到摄像头捕获的内容,仅仅要实现代理中的方法,就能够完毕二维码条形码的扫描:
1
2
3
4
5
6
7
8
|
-( void )captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{ if (metadataObjects.count>0) { //[session stopRunning]; AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ]; //输出扫描字符串 NSLog(@ "%@" ,metadataObject.stringValue); } } |
二、一些优化
通过上面的代码測试,我们能够发现系统的解析处理效率是相当的高。IOS官方提供的API也确实很强大,然而。我们能够做进一步的优化,将效率更加提高:
首先。AVCaptureMetadataOutput类中有一个这种属性(在IOS7.0之后可用):
@property(nonatomic) CGRect rectOfInterest;
这个属性大致意思就是告诉系统它须要注意的区域,大部分APP的扫码UI中都会有一个框。提醒你将条形码放入那个区域,这个属性的作用就在这里。它能够设置一个范围。仅仅处理在这个范围内捕获到的图像的信息。
如此一来。可想而知,我们代码的效率又会得到非常大的提高。在使用这个属性的时候。须要几点注意:
1、这个CGRect參数和普通的Rect范围不太一样。它的四个值的范围都是0-1,表示比例。
2、经过測试发现,这个參数里面的x相应的恰恰是距离左上角的垂直距离。y相应的是距离左上角的水平距离。
3、宽度和高度设置的情况也是类似。
3、举个样例假设我们想让扫描的处理区域是屏幕的下半部分。我们这样设置
1
|
output.rectOfInterest=CGRectMake(0.5,0,0.5, 1); |
详细apple为什么要设计成这样,或者是这个參数我的使用方法那里不正确,还须要了解的朋友给个指导。
每日更新关注:http://weibo.com/hanjunqiang
新浪微博
iOS中 扫描二维码/生成二维码具体解释 韩俊强的博客的更多相关文章
- iOS中 HTTP/Socket/TCP/IP通信协议具体解释 韩俊强的博客
简介: // OSI(开放式系统互联), 由ISO(国际化标准组织)制定 // 1. 应用层 // 2. 表示层 // 3. 会话层 // 4. 传输层 // 5. 网络层 // 6. 数据链接层 / ...
- iOS中 最新微信支付/最全的微信支付教程具体解释 韩俊强的博客
亲们, 首先让我们来看一下微信支付的流程吧. 1. 注冊微信开放平台,创建应用获取appid,appSecret,申请支付功能,申请成功之后会返回一些參数. 2. 下载微信支付sdk 3. clien ...
- iOS中 语音识别功能/语音转文字教程具体解释 韩俊强的博客
原文地址:http://blog.csdn.net/qq_31810357/article/details/51111702 前言:近期研究了一下语音识别,从百度语音识别到讯飞语音识别:首先说一下个人 ...
- iOS中 扫描二维码/生成二维码详解 韩俊强的博客
最近大家总是问我有没有关于二维码的demo,为了满足大家的需求,特此研究了一番,希望能帮到大家! 每日更新关注:http://weibo.com/hanjunqiang 新浪微博 指示根视图: se ...
- iOS中崩溃调试的使用和技巧总结 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 在iOS开发调试过程中以及上线之后,程序经常会出现崩溃的问题.简单的崩溃还好说,复杂的崩溃就需要我们通过解析Cras ...
- iOS中 HTTP/Socket/TCP/IP通信协议详解 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 简单介绍: // OSI(开放式系统互联), 由ISO(国际化标准组织)制定 // 1. 应用层 // 2. 表示层 ...
- iOS中 基于LBXScan库二维码扫描 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 首先声明这个二维码扫描是借助于zxing. 功能模块都完全封装好了,不过界面合你口味,直接使用就好,如果不合口味,后 ...
- iOS中 Realm的学习与使用 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博! 有问题或技术交流可以咨询!欢迎加入! 这篇直接搬了一份官方文档过来看的 由于之前没用markdown搞的乱七八糟的 ...
- iOS中 本地通知/本地通知详解 韩俊强的博客
布局如下:(重点讲本地通知) iOS开发者交流QQ群: 446310206 每日更新关注:http://weibo.com/hanjunqiang 新浪微博 Notification是智能手机应用编 ...
随机推荐
- [USACO15JAN]草鉴定Grass Cownoisseur (分层图,最长路,$Tarjan$)
题目链接 Solution 水水的套路题. 可以考虑到一个环内的点是可以都到达的,所以 \(tajan\) 求出一个 \(DAG\) . 然后 \(DAG\) 上的点权值就是 \(scc\) 的大小. ...
- 【06】react 之 PropsType
React组件创建的时候,需要传入属性,我们可以使用使用PropTypes进行类型检查,您可以使用React.PropTypes在组件的道具上运行. React.PropTypes.array Rea ...
- 修车(bzoj 1070)
Description 同一时刻有N位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的.现在需要安排这M位技术人员所维修的车及顺序,使 ...
- RMQ 算法 学习整理
1. 概述 RMQ(Range Minimum/Maximum Query),即区间最值查询,是指这样一个问题:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A ...
- 不用框架使用ajax 纯js使用ajax post,get范例及其区别
原文发布时间为:2009-11-15 -- 来源于本人的百度文章 [由搬家工具导入] 不用框架使用ajax 纯js使用ajax post,get范例及其区别 ===================== ...
- js7:表单的学习,Forms对象
原文发布时间为:2008-11-09 -- 来源于本人的百度文章 [由搬家工具导入] dreamveawer中,选择插入——表单——然后后面的几个选项进行学习: 大体上这么些类型: <html& ...
- MySQL数据库”mysql SQL Error:1146,SQLState:42S02 “解决方法
项目在开发的时候在Mac平台下开发的,开发完了之后在LINUX环境上部署好之后,运行时MySQL数据库报错,提示为某个表不存在之类的错误信息,后来修改了MySQL的配置文件将大小写敏感去掉,问题解决. ...
- linux 源代码目录结构
Linux源代码目录树结构 (2008-04-21 09:14) 分类: Linux/Unix Linux用来支持各种体系结构的源代码包含大约4500个C语言程序,存放在270个左右的子目录下,总共大 ...
- Java使用apache的开源数据处理框架commons-dbutils完成查询结果集的各种处理输出(8种方式)
package demo; /* * QueryRunner数据查询操作: * 调用QueryRunner类方法query(Connection con,String sql,ResultSetHan ...
- 项目中使用libsodium扩展
前段时间研究了微信小微商户,地址:https://pay.weixin.qq.com/wiki/doc/api/xiaowei.php?chapter=19_11 其接口操作中需要下载证书针对返回的密 ...