本文主要是介绍iOS-AVFoundation二维码扫描,二维码扫描,图片的无损放大,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
二维码扫描
扫描实现思路
二维码实现思路
- 输入设备(用来获取外界信息) 摄像头, 麦克风, 键盘
- 输出设备 (将收集到的信息, 做解析, 来获取收到的内容)
- 会话session (用来连接输入和输出设备)
- 特殊的layer (展示输入设备所采集的信息)
下面上代码:
//
// ViewController.m
// 二维码
//
// Created by VolitationXiaoXing on 15/12/17.
// Copyright © 2016年 dream. All rights reserved.
//
#import "ViewController.h"#import <AVFoundation/AVFoundation.h>#import "HMPreView.h"@interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate>//1. 输入设备(用来获取外界信息) 摄像头, 麦克风, 键盘@property (nonatomic, strong) AVCaptureDeviceInput *input;//2. 输出设备 (将收集到的信息, 做解析, 来获取收到的内容)
@property (nonatomic, strong) AVCaptureMetadataOutput *output;//3. 会话session (用来连接输入和输出设备)
@property (nonatomic, strong) AVCaptureSession *session;//4. 特殊的layer (展示输入设备所采集的信息)//@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;@property (nonatomic, strong) HMPreView *preview;@property (weak, nonatomic) IBOutlet UILabel *label;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.}#pragma mark 点击屏幕开始扫描
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{//1.输入设备(用来获取外界信息) 摄像头, 麦克风, 键盘AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];self.input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];//2.输出设备 (将收集到的信息, 做解析, 来获取收到的内容)self.output = [AVCaptureMetadataOutput new];//3.会话session (用来连接输入和输出设备)self.session = [AVCaptureSession new];// 会话扫描展示的大小
[self.session setSessionPreset:AVCaptureSessionPresetHigh];// 会话跟输入和输出设备关联
if ([self.session canAddInput:self.input]) {[self.session addInput:self.input];
}
if ([self.session canAddOutput:self.output]) {[self.session addOutput:self.output];
}//下面两句代码应该写在此处//制定输出设备的代理, 用来接受返回的数据[self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];//设置元数据类型 二维码QRCode[self.output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];//4.特殊的layer (展示输入设备所采集的信息)//self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];// 大小layer的大小//self.previewLayer.frame = self.view.bounds;//[self.view.layer addSublayer:self.previewLayer];//这个View是一个自定义的View,就是中间一个正方形,中间有一个线上下扫动self.preview = [[HMPreView alloc] initWithFrame:self.view.bounds];self.preview.session = self.session;[self.view addSubview:self.preview];//5. 启动会话[self.session startRunning];
}/**captureOutput : 输出设备metadataObjects : 元数据对象的数组connection : 连接*/- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{//1. 停止会话[self.session stopRunning];//2. 删除layer//[self.previewLayer removeFromSuperlayer];[self.preview removeFromSuperview];//3. 遍历数据获取内容for (AVMetadataMachineReadableCodeObject *obj in metadataObjects) {//NSLog(@"obj: %@",obj.stringValue);self.label.text = obj.stringValue;}
}@end
生成二维码
下面是生成二维码的代码,包括了无损放大图片
//
// ViewController.m
// QRCodeCreat
//
// Created by VolitationXiaoXing on 16/7/14.
// Copyright © 2016年 itcast. All rights reserved.
//#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UIImageView *imgView;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];[filter setDefaults];NSData *data = [@"这个是二维码中的数据" dataUsingEncoding:NSUTF8StringEncoding];[filter setValue:data forKey:@"inputMessage"];//设置容错率[filter setValue:@"H" forKey:@"inputCorrectionLevel"];CIImage *coreImage = [filter outputImage];// 1.无损放大图片UIImage *QRImage = [self imageWithCIImage:coreImage andSize:self.imgView.bounds.size];// 2.合并头像CGSize headSize = CGSizeMake(self.imgView.bounds.size.width * 0.3, self.imgView.bounds.size.height * 0.3);UIImage *overImage = [self imageWithBackgroundImage:QRImage centerImage:[UIImage imageNamed:@"head"] centerImageSize:headSize];self.imgView.image = overImage;}// 1.无损放大图片这里用到了CIImage与CGImage与UIImage转换- (UIImage *)imageWithCIImage:(CIImage *)coreImage andSize:(CGSize)size{// 1.CIImage --> CGImageCIContext *tempContext = [CIContext contextWithOptions:nil];CGImageRef originCGImage = [tempContext createCGImage:coreImage fromRect:coreImage.extent];// 2.创建和size一样大的图形上下文(Bitmap)CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();CGContextRef bitmapCtx = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, cs, kCGImageAlphaNone);// 3.CGImage 渲染到上下文中CGContextSetInterpolationQuality(bitmapCtx, kCGInterpolationNone);CGContextDrawImage(bitmapCtx, CGRectMake(0, 0, size.width, size.height), originCGImage);// 4.将图形上下文中的图像取出来CGImageRef QRImage = CGBitmapContextCreateImage(bitmapCtx);// 释放// returnreturn [UIImage imageWithCGImage:QRImage];}// 2.合并头像- (UIImage *)imageWithBackgroundImage:(UIImage *)backgroundImage centerImage:(UIImage *)centerImage centerImageSize:(CGSize)centerSize{// 1.开启图形上下文UIGraphicsBeginImageContext(backgroundImage.size);// 2.先画背景[backgroundImage drawAtPoint:CGPointZero];// 3.画头像CGFloat headW = centerSize.width;CGFloat headH = centerSize.height;CGFloat headX = (backgroundImage.size.width - headW) * 0.5;CGFloat headY = (backgroundImage.size.width - headH) * 0.5;[centerImage drawInRect:CGRectMake(headX, headY, headW, headH)];// 4.取图像UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return newImage;}@end
这篇关于iOS-AVFoundation二维码扫描,二维码扫描,图片的无损放大的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!