生成二维码过程:
- 实例化二维码滤镜
恢复滤镜的默认属性
将字符串转化为NSData
通过KVC设置滤镜inputMessage数据
获得滤镜输出的图像
将CIImage转化成为UIImage,并放大显示
通过位图创建高清图片
实现:
主要通过iOS提供的CIFilter滤镜技术(内部支持二维码)
以下方法作为NSString的分类的一个方法实现(返回值就是二维码图片)
-(UIImage *)createQRCodeErrorCorrectionLevel:(ErrorCorrectionLevel)errorCorrectionLevel{
//实例化二维码滤镜 CIQRCodeGenerator 二维码生成器滤镜
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setDefaults]; //设置滤镜默认属性
//将NSString转换成NSData
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
//KVC设置滤镜inputMessage数据
[filter setValue:data forKey:@"inputMessage"];
[filter setValue:[self convertErrorCorrectionLevelToString:errorCorrectionLevel] forKey:@"inputCorrectionLevel"];
// 滤镜输出的图片CIImage
CIImage *outputImage = [filter outputImage];
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *image = [UIImage imageWithCGImage:cgImage
scale:1
orientation:UIImageOrientationUp];
//放大处理
UIImage *resizedImage = [self resizeImage:image
withQuality:kCGInterpolationNone
rate:10.0];
return resizedImage;
}
自定义图片放大缩小方法
- (UIImage *)resizeImage:(UIImage *)image
withQuality:(CGInterpolationQuality)quality
rate:(CGFloat)rate {
UIImage *resizedImg = nil;
CGFloat width = image.size.width * rate;
CGFloat height = image.size.height * rate;
UIGraphicsBeginImageContext(CGSizeMake(width, height));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, quality);
[image drawInRect:CGRectMake(0, 0, width, height)];
resizedImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resizedImg;
}
生成自定义的二维码:(二维码是有上下方位的,三角定位)
- 纠错率:有些二维码中间有个图片还能被识别,那是因为二维码的纠错率,可以根据设置纠正不同比例的错误
通过KVC设置滤镜的纠错率, [filter setValue:@“M” forKey:@“inputCorrectionLevel” ];
L:表示7%的字码被修正 M:15% Q: 25% H:30%
- 中间加个图片什么的,另外设置纠错率就可以了
识别二维码:二维码探测器
1、实例化二维码探测器
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];
UIImage *img = [UIImage imageNamed:@""];
2、识别二维码, 可识别一张图中多个二维码的情况
NSArray *feartureArr = [detector featuresInImage:img.CIImage];
for (CIQRCodeFeature *feature in feartureArr) {
NSLog(@"二维码内容---%@",feature.messageString);
NSLog(@"二维码bounds---%@",NSStringFromCGRect(feature.bounds));
}
扫描二维码
- 实例化拍摄设备
设置输入设备
设置元数据输出处理对象
添加拍摄会话
视频预览图层
启动会话
监听元数据处理后的结果
//获取设备摄像头
_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if([_device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]
{
_device.focusMode = AVCaptureFocusModeContinuousAutoFocus;
}
if([_device isFocusModeSupported:AVCaptureFocusModeLocked]) {
[_device unlockForConfiguration];
}
//设置输入设备
_input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:nil];
//设置元数据输出处理对象
_output = [[AVCaptureMetadataOutput alloc]init];
[_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
//添加拍摄会话
_session = [[AVCaptureSession alloc]init];
[_session setSessionPreset:AVCaptureSessionPresetHigh];
//设置会话输入输出对象
if ([_session canAddInput:_input]) {
[_session addInput:_input];
}
if ([_session canAddOutput:_output]) {
[_session addOutput:_output];
}
// 条码类型 AVMetadataObjectTypeQRCode
if ([_output.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeQRCode]) {//做一些空值处理,容易导致崩溃
_output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];
}
else {
_alerView = [[UIAlertView alloc]initWithTitle:@"请在iPhone的\"设置-隐私-相机\"选项中,允许微信访问你的相机."
message:nil
delegate:self
cancelButtonTitle:@"残忍拒绝"
otherButtonTitles:@"去设置", nil];
[_alerView show];
}
//视频预览图层
_preview =[AVCaptureVideoPreviewLayer layerWithSession:_session];
_preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
_preview.frame =CGRectMake(0,0,kScreenWidth,kScreenHeight);
[self.view.layer insertSublayer:_preview atIndex:0];
//启动会话
[_session startRunning];
//放大相机
[self setVideoZoomFactor:1.5];
放大相机
- (void)setVideoZoomFactor:(CGFloat)zoomFactor{
if (!_device) {
return;
}
_initialPinchZoom = _device.videoZoomFactor;
NSError* error = nil;
[_device lockForConfiguration:&error];
if (!error) {
if (_device.activeFormat.videoMaxZoomFactor > 1.5) {
_device.videoZoomFactor = zoomFactor;
}
[_device unlockForConfiguration];
}
}
设备扫描输出代理
AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects
fromConnection:(AVCaptureConnection *)connection {
NSString *qrcodeResult;
if (!metadataObjects) {
return;
}
if ([metadataObjects count] > 0 ) {
AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
qrcodeResult = [NSString stringWithFormat:@"%@",metadataObject.stringValue];
}
if (!qrcodeResult){
return;
}
}