生成二维码
1 导入CoreImage框架
#import
2 通过滤镜CIFilter生成二维码
/**
* 生成二维码
ShareModel*model = [ShareModelmj_objectWithKeyValues:responseObject.dataObject];
CIFilter*filter = [CIFilterfilterWithName:@"CIQRCodeGenerator"];
// 滤镜恢复默认设置
[filtersetDefaults];
// 2. 给滤镜添加数据
NSString*string = model.qrKey;
NSData*data = [stringdataUsingEncoding:NSUTF8StringEncoding];
// 使用KVC的方式给filter赋值
[filtersetValue:dataforKeyPath:@"inputMessage"];
// 3. 生成二维码
CIImage*image = [filteroutputImage];
UIImage* image1 = [weakSelfcreateNonInterpolatedUIImageFormCIImage:imagewithSize:weakSelf.CodeimageV.frame.size.width];
// 4. 显示二维码
weakSelf.CodeimageV.image= image1;
通过上述方法生成的二维码图片比较模糊,第5步显示二维码的时候可以调用以下方法生成比较清晰的二维码
/**
* 根据CIImage生成指定大小的UIImage
*
* @param image CIImage
* @param size 图片宽度
*
* @return 生成高清的UIImage
- (UIImage*)createNonInterpolatedUIImageFormCIImage:(CIImage*)image withSize:(CGFloat) size {
CGRectextent =CGRectIntegral(image.extent);
CGFloatscale =MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// 1.创建bitmap;
size_twidth =CGRectGetWidth(extent) * scale;
size_theight =CGRectGetHeight(extent) * scale;
CGColorSpaceRefcs =CGColorSpaceCreateDeviceGray();
CGContextRefbitmapRef =CGBitmapContextCreate(nil, width, height,8,0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext*context = [CIContextcontextWithOptions:nil];
CGImageRefbitmapImage = [contextcreateCGImage:imagefromRect:extent];
CGContextSetInterpolationQuality(bitmapRef,kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
// 2.保存bitmap到图片
CGImageRefscaledImage =CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return[UIImageimageWithCGImage:scaledImage];
}
扫描二维码
1 导入
2 扫描二维码实现
#import"ViewController.h"// 导入框架#import@interfaceViewController()/** 捕捉会话 */@property(nonatomic,weak)AVCaptureSession*session;/** 预览图层 */@property(nonatomic,weak)AVCaptureVideoPreviewLayer*layer;@end@implementationViewController- (void)viewDidLoad { [superviewDidLoad];// 扫描二维码[selfscanCIQRCode];}/**
* 扫描二维码
*/- (void)scanCIQRCode{
// 1. 创建捕捉会话
AVCaptureSession*session = [[AVCaptureSessionalloc] init];
self.session= session;
// 2. 添加输入设备(数据从摄像头输入)
AVCaptureDevice*device = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput*input = [AVCaptureDeviceInputdeviceInputWithDevice:device error:nil];
[session addInput:input];
// 3. 添加输出数据接口AVCaptureMetadataOutput*output = [[AVCaptureMetadataOutputalloc] init];
// 设置输出接口代理[output setMetadataObjectsDelegate:selfqueue:dispatch_get_main_queue()];
[session addOutput:output];
// 3.1 设置输入元数据的类型(类型是二维码数据)
// 注意,这里必须设置在addOutput后面,否则会报错[output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
// 4.添加扫描图层
AVCaptureVideoPreviewLayer*layer = [AVCaptureVideoPreviewLayerlayerWithSession:session]; layer.frame=self.view.bounds;
[self.view.layeraddSublayer: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.sessionstopRunning];
// 将预览图层移除[self.layerremoveFromSuperlayer];
}else{
NSLog(@"没有扫描到数据");
}}@end
推荐文章:http://www.jianshu.com/p/7a0a8c8703d3
文/袁俊亮技术博客(作者)
原文链接:http://www.jianshu.com/p/b8bf300e9a56