二维码相关

二维码生成
生成高清二维码
扫描二维码

二维码相关_第1张图片
二维码.png
  • 二维码生成
  • 需要导入CoreImage/CoreImage.h
  • 中间的图片就是一个image
#pragma mark - 二维码生成
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // 创建过滤器
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    // 恢复默认设置
    [filter setDefaults];
    
    // 给过滤器添加数据
    NSString *dataString = @"宝宝";
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    [filter setValue:data forKeyPath:@"inputMessage"];
    
    // 获取二维码
    CIImage *outputImage = [filter outputImage];
    
    // 显示
    self.filterImage.image = [UIImage imageWithCIImage:outputImage];
}
二维码相关_第2张图片
高清二维码.png
  • 高清二维码
    • 抽取一个分类

#import "UIImage+Extension.h"

@implementation UIImage (Extension)

+ (UIImage *)createHighDefinitionImageFormCIImage:(CIImage *)image withSize:(CGFloat)size {
    
    CGRect extent = CGRectIntegral(image.extent);
    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, (CGImageAlphaInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 保存bitmap到图片
    CGImageRef scaleImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    
    return [UIImage imageWithCGImage:scaleImage];
}

@end

  • 扫描二维码
    • 导入AVFoundation/AVFoundation.h
    • 需要真机调试,因为输入设备是摄像头
    • 扫描到的数据会调用代理方法
    • 判断,若是网页地址,就创建webView显示出来
#pragma mark - 扫描二维码
- (IBAction)scanCIQRCode:(id)sender {
    
    // 创建捕捉会话
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    
    // 添加输入设备
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    [session addInput:input];
    
    // 添加输出数据
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [session addOutput:output];
    
    // 告诉它扫描的数据是二维码
    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
    
    // 添加扫描图层
    AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:session];
    layer.frame = self.view.bounds;
    [self.view.layer addSublayer:layer];
    
    // 开始扫描
    [session startRunning];
}

#pragma mark - output协议
// 扫描到数据时调用
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
    
    if (metadataObjects.count > 0) {
        NSLog(@"扫描到数据");
    } else {
        NSLog(@"扫描失败");
    }
    
}

你可能感兴趣的:(二维码相关)