来源:Yi'mouleng(@丶伊眸冷)

链接:http://t.cn/R40WxcM

前言

有关二维码的介绍,我这里不做过多说明, 可以直接去基维百科查看,附上链接QR code(https://en.wikipedia.org/wiki/QR_code).

IOS7之前,开发者进行扫码编程时,一般会借助第三方库。常用的是ZBarSDKa和ZXingObjC,IOS7之后,系统的AVMetadataObject类中,为我们提供了解析二维码的接口。经过测试,使用原生API扫描和处理的效率非常高,远远高于第三方库。

扫描

官方提供的接口非常简单,直接看代码,主要使用的是AVFoundation。

@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];

if (!input) return;

//创建输出流

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

//设置代理 在主线程里刷新

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

//设置有效扫描区域

CGRect scanCrop=[self getScanCrop:_scanWindow.bounds readerViewBounds:self.view.frame];

output.rectOfInterest = scanCrop;

//初始化链接对象

_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];

}

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

if (metadataObjects.count>0) {

//[session stopRunning];

AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];

//输出扫描字符串

NSLog(@"%@",metadataObject.stringValue);

}

}

一些初始化的代码加上实现代理方法便完成了二维码扫描的工作,这里我们需要注意的是, 在二维码扫描的时候, 我们一般都会在屏幕中间放一个方框,用来显示二维码扫描的大小区间,这里我们在个AVCaptureMetadataOutput类中有一个rectOfInterest属性,它的作用就是设置扫描范围。

这个CGRect参数和普通的Rect范围不太一样,它的四个值的范围都是0-1,表示比例。
rectOfInterest都是按照横屏来计算的 所以当竖屏的情况下 x轴和y轴要交换一下。
宽度和高度设置的情况也是类似。

我们在上面设置有效扫描区域的方法如下

#pragma mark-> 获取扫描区域的比例关系

-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds

{

CGFloat x,y,width,height;

x = (CGRectGetHeight(readerViewBounds)-CGRectGetHeight(rect))/2/CGRectGetHeight(readerViewBounds);

y = (CGRectGetWidth(readerViewBounds)-CGRectGetWidth(rect))/2/CGRectGetWidth(readerViewBounds);

width = CGRectGetHeight(rect)/CGRectGetHeight(readerViewBounds);

height = CGRectGetWidth(rect)/CGRectGetWidth(readerViewBounds);

return CGRectMake(x, y, width, height);

}

读取

读取主要用到CoreImage 不过要强调的是读取二维码的功能只有在iOS8之后才支持,我们需要在相册中调用一个二维码,将其读取,代码如下

#pragma mark-> 我的相册

-(void)myAlbum{

NSLog(@"我的相册");

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){

//1.初始化相册拾取器

UIImagePickerController *controller = [[UIImagePickerController alloc] init];

//2.设置代理

controller.delegate = self;

//3.设置资源:

/**

UIImagePickerControllerSourceTypePhotoLibrary,相册

UIImagePickerControllerSourceTypeCamera,相机

UIImagePickerControllerSourceTypeSavedPhotosAlbum,照片库

*/

controller.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

//4.随便给他一个转场动画

controller.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;

[self presentViewController:controller animated:YES completion:NULL];

}else{

UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"设备不支持访问相册,请在设置->隐私->照片中进行设置!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

[alert show];

}

}

完成相册代理, 我们在代理中添加读取二维码方法

#pragma mark-> imagePickerController delegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

//1.获取选择的图片

UIImage *image = info[UIImagePickerControllerOriginalImage];

//2.初始化一个监测器

CIDetector*detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{ CIDetectorAccuracy : CIDetectorAccuracyHigh }];

[picker dismissViewControllerAnimated:YES completion:^{

//监测到的结果数组

NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]];

if (features.count >=1) {

/**结果对象 */

CIQRCodeFeature *feature = [features objectAtIndex:0];

NSString *scannedResult = feature.messageString;

UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"扫描结果" message:scannedResult delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

[alertView show];

}

else{

UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"该图片没有包含一个二维码!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

[alertView show];

}

}];

}

因为没用真机,所以这里没有给出太多的截图, 用模拟器读取自带图片,结果如下

生成

生成二维码,其实也是用到CoreImage,但是步骤繁琐一些,代码如下

#pragma mark-> 二维码生成

-(void)create{

UIImage *image=[UIImage imageNamed:@"6824500_006_thumb.jpg"];

NSString*tempStr;

if(self.textField.text.length==0){

tempStr=@"ddddddddd";

}else{

tempStr=self.textField.text;

}

UIImage*tempImage=[QRCodeGenerator qrImageForString:tempStr imageSize:360 Topimg:image withColor:RandomColor];

_outImageView.image=tempImage;

}

+(UIImage*)qrImageForString:(NSString *)string imageSize:(CGFloat)size Topimg:(UIImage *)topimg withColor:(UIColor*)color{

if (![string length]) {

return nil;

}

QRcode *code = QRcode_encodeString([string UTF8String], 0, QR_ECLEVEL_L, QR_MODE_8, 1);

if (!code) {

return nil;

}

// create context

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef ctx = CGBitmapContextCreate(0, size, size, 8, size * 4, colorSpace, kCGImageAlphaPremultipliedLast);

CGAffineTransform translateTransform = CGAffineTransformMakeTranslation(0, -size);

CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1, -1);

CGContextConcatCTM(ctx, CGAffineTransformConcat(translateTransform, scaleTransform));

// draw QR on this context

[QRCodeGenerator drawQRCode:code context:ctx size:size withPointType:0 withPositionType:0 withColor:color];

// get image

CGImageRef qrCGImage = CGBitmapContextCreateImage(ctx);

UIImage * qrImage = [UIImage imageWithCGImage:qrCGImage];

if(topimg)

{

UIGraphicsBeginImageContext(qrImage.size);

//Draw image2

[qrImage drawInRect:CGRectMake(0, 0, qrImage.size.width, qrImage.size.height)];

//Draw image1

float r=qrImage.size.width*35/240;

[topimg drawInRect:CGRectMake((qrImage.size.width-r)/2, (qrImage.size.height-r)/2 ,r, r)];

qrImage=UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

}

// some releases

CGContextRelease(ctx);

CGImageRelease(qrCGImage);

CGColorSpaceRelease(colorSpace);

QRcode_free(code);

return qrImage;

}

+ (void)drawQRCode:(QRcode *)code context:(CGContextRef)ctx size:(CGFloat)size withPointType:(QRPointType)pointType withPositionType:(QRPositionType)positionType withColor:(UIColor *)color {

unsigned char *data = 0;

int width;

data = code->data;

width = code->width;

float zoom = (double)size / (code->width + 2.0 * qr_margin);

CGRect rectDraw = CGRectMake(0, 0, zoom, zoom);

// draw

const CGFloat *components;

if (color) {

components = CGColorGetComponents(color.CGColor);

}else {

components = CGColorGetComponents([UIColor blackColor].CGColor);

}

CGContextSetRGBFillColor(ctx, components[0], components[1], components[2], 1.0);

NSLog(@"aad :%f  bbd :%f   ccd:%f",components[0],components[1],components[2]);

for(int i = 0; i

for(int j = 0; j

if(*data & 1) {

rectDraw.origin = CGPointMake((j + qr_margin) * zoom,(i + qr_margin) * zoom);

if (positionType == QRPositionNormal) {

switch (pointType) {

case QRPointRect:

CGContextAddRect(ctx, rectDraw);

break;

case QRPointRound:

CGContextAddEllipseInRect(ctx, rectDraw);

break;

default:

break;

}

}else if(positionType == QRPositionRound) {

switch (pointType) {

case QRPointRect:

CGContextAddRect(ctx, rectDraw);

break;

case QRPointRound:

if ((i>=0 && i6 && j>=0 && j6) || (i>=0 && i6 && j>=width-7-1 && j-1) || (i>=width-7-1 && i-1 && j>=0 && j6)) {

CGContextAddRect(ctx, rectDraw);

}else {

CGContextAddEllipseInRect(ctx, rectDraw);

}

break;

default:

break;

}

}

}

++data;

}

}

CGContextFillPath(ctx);

}

在textField输入,生成下图

长按二维码识别

这个功能有很多的地方在用, 最让人熟知的我想便是微信了,其实实现方法还是很简单的。

我们用刚才生成的二维码进行长按识别,效果如下

结语

本文demo:https://github.com/yimouleng/GBAliScan

转自mokey1422所写的仿支付宝二维码。

系统原生的二维码扫描扫描识别速度,要比第三方好用得多,在没有特殊原因的情况下,比如7.0系统以下,我希望大家都能用系统原生的方法。

iOS-原生二维码

http://www.jianshu.com/p/e15ca2799796

QRCode - 二维码识别与生成的更多相关文章

  1. WEB H5 JS QRCode二维码快速自动生成

    万能的GITHUB: https://github.com/davidshimjs/qrcodejs HTML: <div class="col-xs-10 col-xs-offset ...

  2. python qrcode二维码生成与识别

    二维码 二维码生成 1.用法 https://github.com/lincolnloop/python-qrcode 2.使用 简单实用 import qrcode # 二维码内容 data = & ...

  3. QRCode二维码生成方案及其在带LOGO型二维码中的应用(1)

    原文:QRCode二维码生成方案及其在带LOGO型二维码中的应用(1) 提要:很多公司为商业宣传之需,常将企业LOGO加入二维码中,但如果LOGO遮挡区域足够地大,二维码就变得无法识别.那么,有没有一 ...

  4. jQuery生成QRcode二维码

    jQuery生成QRcode二维码示例 <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...

  5. PHP生成QRCode二维码

    php生成QRCode二维码示例 <?php //引入 phpqrcode 类库 //phpqrcode下载地址:https://github.com/t0k4rt/phpqrcode //或从 ...

  6. 用CIFilter生成QRCode二维码图片

    用CIFilter生成QRCode二维码图片 CIFilter不仅仅可以用来做滤镜,它还可以用来生成二维码. CIFilterEffect.h + CIFilterEffect.m // // CIF ...

  7. QRCode二维码生成方案及其在带LOGO型二维码中的应用(2)

    原文:QRCode二维码生成方案及其在带LOGO型二维码中的应用(2) 续前:QRCode二维码生成方案及其在带LOGO型二维码中的应用(1)  http://blog.csdn.net/johnsu ...

  8. Atitit java 二维码识别 图片识别

    Atitit java 二维码识别 图片识别 1.1. 解码11.2. 首先,我们先说一下二维码一共有40个尺寸.官方叫版本Version.11.3. 二维码的样例:21.4. 定位图案21.5. 数 ...

  9. 有关python下二维码识别用法及识别率对比分析

    最近项目中用到二维码图片识别,在python下二维码识别,目前主要有三个模块:zbar .zbarlight.zxing. 1.三个模块的用法: #-*-coding=utf-8-*- import ...

随机推荐

  1. 2、COCOS2D-X内存管理机制

    在C++中.动态内存分配是一把双刃剑,一方面,直接訪问内存地址提高了应用程序的性能,与使用内存的灵活性.还有一方面.因为程序没有正确地分配与释放造成的比如野指针,反复释放,内存泄漏等问题又严重影响着应 ...

  2. 3D屏保:魔方2.0版本

    一个三维魔方的屏保软件,可支持2级到72级的魔方.启动后魔方会自动旋转,并最终回到初始状态.有很多人问我这是怎么做到的,用的什么解魔方的算法,其实我自己根本就不会玩魔方,别人用技巧解魔方,我这程序中用 ...

  3. HTML5 Canvas,WebGL,CSS Shaders,GLSL的暧昧关系 【转】

    HTML5 Canvas,WebGL,CSS Shaders,GLSL的暧昧关系 这篇文章发布于 2011年10月10日,星期一,17:14,归类于 canvas相关. 阅读 58013 次, 今日 ...

  4. javascript+JQuery实现返回顶部功能

    很多网站上都有返回顶部的效果,本文阐述如何使用jquery实现返回顶部按钮. 首先需要在顶部添加如下html元素: <p id="back-to-top"><a ...

  5. Maven Dependencies没有了的解决办法

    头疼的问题,maven Dependencies突然没有了,别的项目都有,个别的却怎么也出不来.  以下是某大神的解决方法,特此转发,以防丢失: 网上搜索了一番,大多都是下面这种做法: 右击 Mave ...

  6. 2.Dynamic Programming on Stolen Values【dp】

    Problem: There are  n houses built in a line, each of which contains some value in it. A thief is go ...

  7. windows10(64位)Anaconda3+Python3.6搭建Tensorflow(cpu版本)及keras

    转自:windows10(64位)Anaconda3+Python3.6搭建Tensorflow(cpu版本)及keras 1.本来电脑安装的是anaconda3 5.3.1,但安装的python版本 ...

  8. linux /dev 常见特殊设备介绍与应用[loop,null,zero,full,random]

    linux是文件型系统,所有硬件如软件都会在对于的目录下面有相应的文件表示.对于dev这个目录,我们知道它下面的文件,表示的是linux的设备.在windows系统中,设备大家很好理解,象硬盘,磁盘指 ...

  9. office2007word文档设置多级目录

    office本来不是很难,关键就是经验吧,直入主题. 文档结构图设置了四级,但是目录始终只显示三级,郁闷了好久,网上看的也不靠谱,方法如下: 引用-目录-插入目录 弹出插入目录设置后,修改级别为最大, ...

  10. [Node.js] Level 3 new. Steam

    File Read Stream Lets use the fs module to read a file and log its contents to the console. Use the  ...