title : IOS中生成二维码、扫描二维码
category : UI
IOS中生成二维码、保存二维码
标签(空格分隔): IOS
[TOC]
简介
- 从IOS7开始集成了二维码的生成和读取功能
生成二维码
1 导入CoreImage框架
#import
2 通过滤镜CIFilter生成二维码
/**
* 生成二维码
*/
- (void)creatCIQRCodeImage
{
// 1.创建过滤器,这里的@"CIQRCodeGenerator"是固定的
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 2.恢复默认设置
[filter setDefaults];
// 3. 给过滤器添加数据
NSString *dataString = @"二维码测试数据";
NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
// 注意,这里的value必须是NSData类型
[filter setValue:data forKeyPath:@"inputMessage"];
// 4. 生成二维码
CIImage *outputImage = [filter outputImage];
// 5. 显示二维码
self.imageView.image = [UIImage imageWithCIImage:outputImage];
}
- 通过上述方法生成的二维码图片比较模糊,第5步显示二维码的时候可以调用以下方法生成比较清晰的二维码
/**
* 根据CIImage生成指定大小的UIImage
*
* @param image CIImage
* @param size 图片宽度
*
* @return 生成高清的UIImage
*/
- (UIImage *)creatNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat)size
{
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// 1. 创建bitmap
size_t width = CGRectGetWidth(extent) * scale;
size_t height = CGRectGetHeight(extent) * scale;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
// 2.保存bitmap图片
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}
- 将上述生成高清二维码的方法封装成UIImage的Category如下
.h文件
#import
@interface UIImage (Category)
/**
* 根据CIImage生成指定大小的UIImage
*
* @param image CIImage
* @param size 图片宽度
*
* @return 生成的高清的UIImage
*/
+ (UIImage *)creatNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat)size;
@end
.m文件
#import "UIImage+Category.h"
@implementation UIImage (Category)
/**
* 根据CIImage生成指定大小的UIImage
*
* @param image CIImage
* @param size 图片宽度
*
* @return 生成的高清的UIImage
*/
+ (UIImage *)creatNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat)size
{
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// 1. 创建bitmap
size_t width = CGRectGetWidth(extent) * scale;
size_t height = CGRectGetHeight(extent) * scale;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
// 2.保存bitmap图片
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}
@end
扫描二维码
- 1 导入
- 2 扫描二维码实现
#import "ViewController.h"
// 导入框架
#import
@interface ViewController ()
/** 捕捉会话 */
@property (nonatomic, weak) AVCaptureSession *session;
/** 预览图层 */
@property (nonatomic, weak) AVCaptureVideoPreviewLayer *layer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 扫描二维码
[self scanCIQRCode];
}
/**
* 扫描二维码
*/
- (void)scanCIQRCode
{
// 1. 创建捕捉会话
AVCaptureSession *session = [[AVCaptureSession alloc] init];
self.session = session;
// 2. 添加输入设备(数据从摄像头输入)
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
[session addInput:input];
// 3. 添加输出数据接口
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
// 设置输出接口代理
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[session addOutput:output];
// 3.1 设置输入元数据的类型(类型是二维码数据)
// 注意,这里必须设置在addOutput后面,否则会报错
[output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
// 4.添加扫描图层
AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:session];
layer.frame = self.view.bounds;
[self.view.layer addSublayer:layer];
self.layer = layer;
// 5. 开始扫描
[session startRunning];
}
#pragma mark -
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
if (metadataObjects.count) {// 扫描到了数据
AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject];
// 停止扫描
[self.session stopRunning];
// 将预览图层移除
[self.layer removeFromSuperlayer];
}else{
NSLog(@"没有扫描到数据");
}
}
@end
推荐文章:http://www.jianshu.com/p/7a0a8c8703d3
推荐文章:http://zhangbuhuai.com/2015/02/11/iOS%E5%8E%9F%E7%94%9F%E4%BA%8C%E7%BB%B4%E7%A0%81%E6%89%AB%E6%8F%8F/
Swift二维码扫描:https://github.com/ConanMTHu/QRCodeByAVFoundation