首先来看看借助苹果原生CoreImage框架如何将文字信息生成二维码图片,毫无疑问肯定要导入#import <CoreImage/CoreImage.h>这个头文件。
实现以下代码既可以生成二维码图片,但是图片比较模糊。
// 1.创建滤镜对象
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 2.恢复默认设置
[filter setDefaults];
// 3.设置数据
NSString *info = @"http://www.baidu.com";
NSData *infoData = [info dataUsingEncoding:NSUTF8StringEncoding];
[filter setValue:infoData forKeyPath:@"inputMessage"];
// 4.生成二维码
CIImage *outputImage = [filter outputImage];
self.imageView.image = [UIImage imageWithCIImage:outputImage];
解决二维码图片模糊问题,需要我们自己绘图。新建一个CIImage的分类,然后实现如下代码。直接调用这个分类的方法将CIImage转为UIImage即可解决问题。
- (UIImage *)createNonInterpolatedWithSize:(CGFloat)size
{
CGRect extent = CGRectIntegral(self.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:self 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];
}
接下来看看如何扫面二维码。扫秒二维码是借助AVFoundation框架,相关注释在代码中。
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>
//注意这里用weak
@property (nonatomic, weak) AVCaptureSession *session;
@property (nonatomic, weak) AVCaptureVideoPreviewLayer *layer;
@end
@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//1.获取捕捉会话
AVCaptureSession *session = [[AVCaptureSession alloc]init];
self.session = session;
//2.设置输入(摄像头)
//AVMediaTypeVideo摄像头, AVMediaTypeAudio话筒, or AVMediaTypeMuxed弹幕
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
[_session addInput:input];
//3.设置输出数据
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc]init];
//设置代理 <AVCaptureMetadataOutputObjectsDelegate>
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[_session addOutput:output];
//设置输出类型 AVMetadataObjectTypeQRCode表示输出类型为二维码
//注意:必须在设置代理完毕后设置
[output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
//4.添加阅览图层
AVCaptureVideoPreviewLayer *layer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:self.session];
self.layer = layer;
_layer.frame = self.view.bounds;
[self.view.layer addSublayer:_layer];
//5.开始扫描
[_session startRunning];
}
#pragma mark - <AVCaptureMetadataOutputObjectsDelegate>
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
if (metadataObjects.count > 0) {
// 1.获取扫描到的内容
AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject];
NSLog(@"%@", object.stringValue);
// 2.停止会话
[self.session stopRunning];
// 3.移除预览图层
[self.layer removeFromSuperlayer];
}
}