一:生成二维码
1.根据一个字符串生成一个二维码 根据
#import <CoreImage/CoreImage.h>这个框架写的
在按钮的点击事件写
@interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *imv; @end
// 1. 实例化二维码滤镜 CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; // 2. 恢复滤镜的默认属性 [filter setDefaults]; // 3. 将字符串转换成NSData NSData *data = [@"http://www.baidu.com" dataUsingEncoding:NSUTF8StringEncoding]; // 4. 通过KVO设置滤镜inputMessage数据
[filter setValue:data forKey:@"inputMessage"];
// 5. 获得滤镜输出的图像
CIImage *outputImage = [filter outputImage]; UIImage *i = [UIImage imageWithCIImage:outputImage]; // 6. 将CIImage转换成UIImage,并放大显示 self.imv.image = i;
2.此时 就根据你写的字符串 会 生成一个对应的二维码了 但是有点模糊有没有 ??
解决的方法来啦
//7.把图片变的更清晰 CGRect extent = CGRectIntegral(outputImage.extent); CGFloat size = 100; CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent)); // 创建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:outputImage fromRect:extent]; CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone); CGContextScaleCTM(bitmapRef, scale, scale); CGContextDrawImage(bitmapRef, extent, bitmapImage); // 保存bitmap到图片 CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef); CGContextRelease(bitmapRef); CGImageRelease(bitmapImage); UIImage *i = [UIImage imageWithCGImage:scaledImage];
清晰的有没有,^O^
3.微信的二维码名片是不是有自己的logo哇,怎么弄捏
UIImage *logoImage = [UIImage imageNamed:@"1111.png"]; UIView *QRCodeView = [[UIView alloc] init]; QRCodeView.frame = CGRectMake(0, 0, 100 , 100); if (logoImage != nil) { // 增加logo i = [self addImageToSuperImage:i withSubImage:logoImage andSubImagePosition:CGRectMake(20, 20, 20, 20)]; } QRCodeView.layer.contents = (__bridge id)i.CGImage;
这样就可以加上logo了
二:扫描二维码
用
#import <AVFoundation/AVFoundation.h>这个框架写
@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate> @property(nonatomic,strong)AVCaptureSession *session; @property(nonatomic,strong)AVCaptureVideoPreviewLayer *previewLayer; @end
- (void)viewDidLoad { [super viewDidLoad]; // 1. 实例化拍摄设备 AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; // 2. 设置输入设备 AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; // 3. 设置元数据输出 // 3.1 实例化拍摄元数据输出 AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init]; // 3.3 设置输出数据代理 [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; // 4. 添加拍摄会话 // 4.1 实例化拍摄会话 AVCaptureSession *session = [[AVCaptureSession alloc] init]; // 4.2 添加会话输入 [session addInput:input]; // 4.3 添加会话输出 [session addOutput:output]; // 4.3 设置输出数据类型,需要将元数据输出添加到会话后,才能指定元数据类型,否则会报错 [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]]; self.session = session; // 5. 视频预览图层 // 5.1 实例化预览图层, 传递_session是为了告诉图层将来显示什么内容 AVCaptureVideoPreviewLayer *preview = [AVCaptureVideoPreviewLayer layerWithSession:_session]; preview.videoGravity = AVLayerVideoGravityResizeAspectFill; preview.frame = self.view.bounds; // 5.2 将图层插入当前视图 [self.view.layer insertSublayer:preview atIndex:100]; self.previewLayer = preview; // 6. 启动会话 [_session startRunning]; }
代理方法
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { // 会频繁的扫描,调用代理方法 // 1. 如果扫描完成,停止会话 [self.session stopRunning]; // 2. 删除预览图层 [self.previewLayer removeFromSuperlayer]; NSLog(@"%@", metadataObjects); // 3. 设置界面显示扫描结果 if (metadataObjects.count > 0) { AVMetadataMachineReadableCodeObject *obj = metadataObjects[0]; // 提示:如果需要对url或者名片等信息进行扫描,可以在此进行扩展! // _label.text = obj.stringValue; NSLog(@"%@", obj.stringValue); } }
三:最后封装了一下生成二维码的代码 代价可以直接用
-(UIImage *)superCIImageWith:(NSString*)urlString andSuperSize:(CGFloat)puserSize andlogoImage:(UIImage *)logoImage andLogoImageLocation:(CGRect)rect { // 1. 实例化二维码滤镜 CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; // 2. 恢复滤镜的默认属性 [filter setDefaults]; // 3. 将字符串转换成NSData NSData *data = [urlString dataUsingEncoding:NSUTF8StringEncoding]; // 4. 通过KVO设置滤镜inputMessage数据 [filter setValue:data forKey:@"inputMessage"]; // 5. 获得滤镜输出的图像 CIImage *outputImage = [filter outputImage]; // UIImage *i = [UIImage imageWithCIImage:outputImage]; // 6. 将CIImage转换成UIImage,并放大显示 //7.把图片变的更清晰 CGRect extent = CGRectIntegral(outputImage.extent); CGFloat scale = MIN(puserSize/CGRectGetWidth(extent), puserSize/CGRectGetHeight(extent)); // 创建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:outputImage fromRect:extent]; CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone); CGContextScaleCTM(bitmapRef, scale, scale); CGContextDrawImage(bitmapRef, extent, bitmapImage); // 保存bitmap到图片 CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef); CGContextRelease(bitmapRef); CGImageRelease(bitmapImage); UIImage *i = [UIImage imageWithCGImage:scaledImage]; UIView *QRCodeView = [[UIView alloc] init]; QRCodeView.frame = CGRectMake(0, 0, 400 , 400); if (logoImage != nil) { // 增加logo i = [self addImageToSuperImage:i withSubImage:logoImage andSubImagePosition:rect]; } QRCodeView.layer.contents = (__bridge id)i.CGImage; return i; }