iOS 二维码生成和扫描
一、二维码生成
//创建二维码视图
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)];
[self.view addSubview:imageView];
//二维码中间图片 (如果没有中间图片可屏蔽)
UIImageView *sjhImageView = [[UIImageView alloc]initWithFrame:CGRectMake((imageView.frame.size.width- 50)/2, (imageView.frame.size.height - 50)/2, 50, 50)];
sjhImageView.image = [UIImage imageNamed:@"sunjunhua"];
[imageView addSubview:sjhImageView];
// 1.创建过滤器
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 2.恢复默认
[filter setDefaults];
// 3.给过滤器添加数据
NSString *dataString = @"我爱你,么么哒!";
NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
// 4.通过KVO设置滤镜inputMessage数据
[filter setValue:data forKeyPath:@"inputMessage"];
// 4.获取输出的二维码
CIImage *outputImage = [filter outputImage];
// 5.将CIImage转换成UIImage,并放大显示
imageView.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:300];
因为正常系统生成的二位吗很模糊不清晰,下面这个方法是让二维码处于高清状态。//高清二维码
- (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.1.这里要签协议AVCaptureMetadataOutputObjectsDelegate
// 获取 AVCaptureDevice 实例
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
// 初始化输入流
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:nil];
// 创建会话
_session = [[AVCaptureSession alloc] init];
//高质量采集率
[_session setSessionPreset:AVCaptureSessionPresetHigh];
// 初始化输出流
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
//代理
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
CGFloat x = ((kScreenHeight-Width-64)/2.0)/kScreenHeight;
CGFloat y = ((kScreenWidth-Width)/2.0)/kScreenWidth;
CGFloat width = Width/kScreenHeight;
CGFloat height = Width/kScreenWidth;
output.rectOfInterest = CGRectMake(x, y, width, height);
if ([_session canAddInput:input]) {
// 添加输入流
[_session addInput:input];
}
if ([_session canAddOutput:output]) {
// 添加输出流
[_session addOutput:output];
}
//设置二维码类型 (下面几个类型是 二维码、条形码都兼容,如果需要其他类型还需添加)
output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];
//添加扫描画面
_previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_session];
[_previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[_previewLayer setFrame:self.view.layer.bounds];
[self.view.layer addSublayer:_previewLayer];
// 开始会话
[_session startRunning];
2.2实现代理方法
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
NSString *stringValue;
if ([metadataObjects count] >0){
//停止扫描
[_session stopRunning];
//删除预览图层
// [self.previewLayer removeFromSuperlayer];
AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
//扫描结果
stringValue = metadataObject.stringValue;
//根据需求处理结果
//初始化提示框;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"扫描结果" message:stringValue preferredStyle: UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
//弹出提示框;
[self presentViewController:alert animated:true completion:nil];
三、选取相册中二维码扫描
3.1 需要在info.plist中添加相册权限 NSPhotoLibraryUsageDescription
3.2 签协议UINavigationControllerDelegate,UIImagePickerControllerDelegate
3.3 在需要相册功能按钮中实现
UIImagePickerController *imagrPicker = [[UIImagePickerController alloc]init];
imagrPicker.delegate = self;
imagrPicker.allowsEditing = YES;
//将来源设置为相册
imagrPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:imagrPicker animated:YES completion:nil];
3.4 实现代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//获取选中的照片
UIImage *image = info[UIImagePickerControllerEditedImage];
if (!image) {
image = info[UIImagePickerControllerOriginalImage];
}
//初始化 将类型设置为二维码
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:nil];
[picker dismissViewControllerAnimated:YES completion:^{
//设置数组,放置识别完之后的数据
NSArray *features = [detector featuresInImage:[CIImage imageWithData:UIImagePNGRepresentation(image)]];
//判断是否有数据(即是否是二维码)
if (features.count >= 1) {
//取第一个元素就是二维码所存放的文本信息
CIQRCodeFeature *feature = features[0];
NSString *scannedResult = feature.messageString;
//通过对话框的形式呈现
[self alertControllerMessage:scannedResult];
}else{
//通过对话框的形式呈现
[self alertControllerMessage:@"这不是一个二维码"];
}
}];
}
四、二维码扫描动画(你懂得)
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenWidth-Width)/2.0, (kScreenHeight-Width-64)/2.0, Width, Width)];
imageView.image = [UIImage imageNamed:@"scanscanBg"];
[self.view addSubview:imageView];
UIImageView *animationView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"scanLine"]];
animationView.frame = CGRectMake(0,0, Width, 10);
CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath =@"transform.translation.y";
animation.byValue = @(Width-5);
animation.removedOnCompletion = NO;
animation.duration = 2.0;
animation.repeatCount = MAXFLOAT;
[animationView.layer addAnimation:animation forKey:nil];
[imageView addSubview:animationView];
五、扫描区域外部视图(朦朦胧胧的感觉)
//设置统一的视图颜色和视图的透明度
UIColor *color = [UIColor blackColor];
float alpha = 0.6;
//设置扫描区域外部上部的视图
UIView *topView = [[UIView alloc]init];
topView.frame = CGRectMake(0, 64, kScreenWidth, (kScreenHeight-64-Width)/2.0-64);
topView.backgroundColor = color;
topView.alpha = alpha;
//设置扫描区域外部左边的视图
UIView *leftView = [[UIView alloc]init];
leftView.frame = CGRectMake(0, 64+topView.frame.size.height, (kScreenWidth-Width)/2.0,Width);
leftView.backgroundColor = color;
leftView.alpha = alpha;
//设置扫描区域外部右边的视图
UIView *rightView = [[UIView alloc]init];
rightView.frame = CGRectMake((kScreenWidth-Width)/2.0+Width,64+topView.frame.size.height, (kScreenWidth-Width)/2.0,Width);
rightView.backgroundColor = color;
rightView.alpha = alpha;
//设置扫描区域外部底部的视图
UIView *botView = [[UIView alloc]init];
botView.frame = CGRectMake(0, 64+Width+topView.frame.size.height,kScreenWidth,kScreenHeight-64-Width-topView.frame.size.height);
botView.backgroundColor = color;
botView.alpha = alpha;
//将设置好的扫描二维码区域之外的视图添加到视图图层上
[self.view addSubview:topView];
[self.view addSubview:leftView];
[self.view addSubview:rightView];
[self.view addSubview:botView];
总结:都是查资料和自己的总结,第一次写,比较Low,忘见谅,只是参考。
iOS 二维码生成和扫描相关推荐
- iOS - 二维码生成、扫描及页面跳转
主要内容的介绍 普通二维码生成 彩色二维码生成 带有小图标二维码生成 扫描二维码的自定义 是否开启闪光灯 从相册中获取二维码 扫描成功之后提示音 扫描成功之后的界面之间的跳转 扫描二维码界面采取了微信 ...
- 微信公众平台----带参数二维码生成和扫描事件
原文:微信公众平台----带参数二维码生成和扫描事件 摘要: 账号管理----生成带参数的二维码 消息管理----接收消息----接收事件推送 为了满足用户渠道推广分析和用户帐号绑定等场景的需要,公众 ...
- 苹果原生二维码生成与扫描及生成的二维码不清楚的解决方案
苹果原生二维码生成与扫描及生成的二维码不清楚的解决方案 参考文章: (1)苹果原生二维码生成与扫描及生成的二维码不清楚的解决方案 (2)https://www.cnblogs.com/CoderEYL ...
- 二维码生成、扫描、图片识别(Zxing)
这样的例子虽然已经很多了,不过我在网上浏览了一圈,也没找到几个图库二维码图片识别例子,好的算法识别率才高.这里有一个好点的算法,算法不是我写的,只是作为整理记录,给众多安卓开发者一个方便.demo的U ...
- Android开发——Android中的二维码生成与扫描
0. 前言 今天这篇文章主要描述二维码的生成与扫描,使用目前流行的Zxing,为什么要讲二维码,因为二维码太普遍了,随便一个Android APP都会有二维码扫描.本篇旨在帮助有需求的同学快速完成二维 ...
- iOS二维码生成中间带图片Logo
iOS二维码生成中间带图片效果图: ViewController.h 1 #import <UIKit/UIKit.h> 2 @interface ViewController : UIV ...
- iOS二维码生成(带logo)
实在不好意思,昨天忘记写的<<二维码生成>>忘记写最常见的黑白二维码嵌入一张图片,一般都是公司的logo.今天补上 // // ViewController.m // 内置图片 ...
- iOS开发二维码生成和扫描
准备工作 导入<CoreImage/CoreImage.h>,生成二维码用 导入<AVFoundation/AVFoundation.h>,读取二维码用 设置代理协议AVCap ...
- Android之二维码生成与扫描
转载请标明出处: http://blog.csdn.net/hai_qing_xu_kong/article/details/51260428 本文出自:[顾林海的博客] ##前言 月底离开公司,准备 ...
最新文章
- fliqlo windows_Windows小众软件工具推荐
- The J2EE Architect's Handbook讀書筆記(二)
- Java高级工程师必备数据结构算法高效查找算法原理分析与实现
- 救基友记2_JAVA
- C#使用HTTP头检测网络资源是否有效
- redis实战_Redis实战(7)-SortedSet实战之认识有序集合(命令行与代码实战)
- python清除输出内容_jupyter notebook清除输出方式
- java微信支付异步通知_Java中微信支付退款异步通知解码
- Tech.ED 2009特别奉献:Windows 7解读
- 纯新手DSP编程--5.31--DSP/BIOS中的数据交换
- 关于Adobe AIR 获取屏幕信息及任务栏高度.
- .NET高性能编程 - C#如何安全、高效地玩转任何种类的内存之Span的本质(一)。
- 从eoeandroid换到CSDN-[回顾]
- 云时代的“双态IT”运维思路
- 阿里云的ACP认证与ACE认证含金量高吗?
- Centos 6版本Device eth0 does not seem to be present,delaying initialization.故障处理
- 中国联通、华为联合举办国内首例5G异地合奏音乐会
- Android面试知识总结
- 定积分(黎曼和)的编程实现(java和python实现)
- 魔高一丈道高一尺,开放接口安全性设计
热门文章
- ASP 页的执行造成响应缓冲区超过其配置限制
- 【PowerBI/PBI】PBI DAX之CALCULATE,IF 和 Excel VBA之COUNTIFS
- python删除异常值代码_利用Python进行异常值分析实例代码
- python iocp_Windows之IOCP
- 检查计算机更新在哪里,有效解决升级Win10系统后电脑卡在正在检查更新的情况...
- 开科唯识冲刺创业板:年营收3.7亿 红杉奕信是二股东
- VGG16网络结构修改全连接层可以实现输入图像尺寸的限制
- 使用google authenticator(谷歌身份验证器)打造用户登录动态口令
- C#中SqlCommand的使用小结
- python将想要打印的数据输出到txt文件中,打印省略号里面的内容