iOS原生二维码扫描、生成

1. 生成二维码

-(void)生成二维码
{
UIImageView *_imgView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, [UIScreen mainScreen].bounds.size.width/2.0, [UIScreen mainScreen].bounds.size.width/2.0)];
[self.view addSubview:_imgView];


//二维码滤镜
CIFilter *filter=[CIFilter filterWithName:@"CIQRCodeGenerator"];

//恢复滤镜的默认属性
[filter setDefaults];

//将字符串转换成NSData
NSData *data=[@"www.baidu.com" dataUsingEncoding:NSUTF8StringEncoding];

//通过KVO设置滤镜inputmessage数据
[filter setValue:data forKey:@"inputMessage"];

//获得滤镜输出的图像
CIImage *outputImage=[filter outputImage];


//将CIImage转换成UIImage,并放大显示
_imgView.image=[self createNonInterpolatedUIImageFormCIImage:outputImage withSize:100.0];



//如果还想加上阴影,就在ImageView的Layer上使用下面代码添加阴影

_imgView.layer.shadowOffset=CGSizeMake(0, 0.5);//设置阴影的偏移量

_imgView.layer.shadowRadius=1;//设置阴影的半径

_imgView.layer.shadowColor=[UIColor blackColor].CGColor;//设置阴影的颜色为黑色

_imgView.layer.shadowOpacity=0.3;
}

//改变二维码大小

- (UIImage *)createNonInterpolatedUIImageFormCIImage:(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, (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);

// 保存bitmap到图片

CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);

CGContextRelease(bitmapRef);

CGImageRelease(bitmapImage);

return [UIImage imageWithCGImage:scaledImage];

}

2. 扫描二维码

-(void)扫描二维码
// 1. 实例化拍摄设备
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    // 2. 设置输入设备
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
    
    // 3. 设置元数据输出
    // 3.1 实例化拍摄元数据输出
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    //设置扫描区域
//        output.rectOfInterest = CGRectMake(CGRectGetMinX(BOX_FRAME)/CGRectGetWidth(frame), CGRectGetMinY(BOX_FRAME)/CGRectGetHeight(frame), CGRectGetMaxX(BOX_FRAME)/CGRectGetWidth(frame),CGRectGetMaxY(BOX_FRAME)/CGRectGetHeight(frame));
    // 3.3 设置输出数据代理
    [output setMetadataObjectsDelegate:delegate queue:dispatch_get_main_queue()];
    
    // 4. 添加拍摄会话
    // 4.1 实例化拍摄会话
    _session = [[AVCaptureSession alloc] init];
    
    // 4.2 添加会话输入
    [_session addInput:input];
    
    // 4.3 添加会话输出
    [_session addOutput:output];
    
    // 4.3 设置输出数据类型,需要将元数据输出添加到会话后,才能指定元数据类型,否则会报错
    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
    
    // 5. 视频预览图层
    // 5.1 实例化预览图层, 传递_session是为了告诉图层将来显示什么内容
    AVCaptureVideoPreviewLayer *preview = [AVCaptureVideoPreviewLayer layerWithSession:_session];
    preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
    preview.frame = self.bounds;
    
    // 5.2 将图层插入当前视图
    [self.layer addSublayer:preview];
    // 6. 启动会话
    [_session startRunning];
}

#pragma mark delegateMethod
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {

// 1. 如果扫描完成,停止会话
[_session stopRunning];

if (metadataObjects.count > 0)
{
    AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];
    NSLog(@"%@",obj.stringValue);
    
}

}

iOS原生二维码扫描、生成_第1张图片
效果图

demo地址

你可能感兴趣的:(iOS原生二维码扫描、生成)