iOS扫描二维码与生成二维码
来源:互联网 发布:淘宝详情页ps技巧 编辑:程序博客网 时间:2024/05/18 20:34
objective-c二维码的扫码与生成
- 扫描二维码
- 生成二维码
1.扫描二维码
获取输入设备(当前是摄像头)
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
根据输入设备获取输入对象
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
创建输出对象
AVCaptureMetadataOutput *output = [AVCaptureMetadataOutput new];
设置代理监听对象输出的数据,在主线程回调
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
创建会话
AVCaptureSession *session = [AVCaptureSession new];//实现高质量的输出和摄像,默认值为AVCaptureSessionPresetHigh,可以不写[session setSessionPreset:AVCaptureSessionPresetHigh];
添加输入输出到会话中(判断session是否已满)
if ([session canAddInput:input]) { [session addInput:input];}if ([session canAddOutput:output]) { [session addOutput:output];}
设置输出对象的输出什么样的数据(二维码、条码)
output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeCode128Code,AVMetadataObjectTypeCode93Code,AVMetadataObjectTypeCode39Code,AVMetadataObjectTypeCode39Mod43Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeUPCECode,AVMetadataObjectTypePDF417Code,AVMetadataObjectTypeAztecCode];
创建预览图层
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];previewLayer.frame = self.view.bounds;[self.view.layer insertSublayer:previewLayer atIndex:0];
设置有效扫描区域
CGRect rect = CGRectMake(0.1,0.1,0.8,0.8);output.rectOfInterest = rect;
设置周边空白区域(中间扫描区域透明度比其他地方低)
UIView *maskView = [[UIView alloc] initWithFrame:self.view.bounds];maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4f];[self.view addSubview:maskView];UIBezierPath *rectPath = [UIBezierPath bezierPathWithRect:self.view.bounds];[rectPath appendPath:[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(20.0f, 20.0f, 200.0f, 200.0f) cornerRadius:1] bezierPathByReversingPath]];CAShapeLayer *shapeLayer = [CAShapeLayer layer];shapeLayer.path = rectPath.CGPath;maskView.layer.mask = shapeLayer;
扫描成功后的回调
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{ if (metadataObjects.count > 0) { //停止扫描(不停止有可能这个方法会执行好几次) [self.session stopRunning]; //取出扫描到得数据 AVMetadataMachineReadableCodeObject *obj = [metadataObjects lastObject]; if (obj) { NSLog(@"result = %@",[obj stringValue]); } }}
2.生成二维码
二维码滤镜
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
设置滤镜的默认属性
[filter setDefaults];
将字符串转成二进制数据
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
通过KVO设置滤镜inputmessage数据
[filter setValue:data forKey:@"inputMessage"];
获取滤镜输出的图片
CIImage *outputImage = [filter outputImage];//转成UIImage返回UIImage *image = [UIImage imageWithCIImage:outputImage];
1 0
- iOS扫描二维码与生成二维码
- iOS二维码生成与扫描
- 二维码扫描与生成二维码
- 二维码生成与扫描
- 二维码生成与扫描
- 二维码扫描与生成
- 二维码扫描与生成
- ios二维码的生成与二维码图片的扫描
- ios二维码的生成与扫描
- iOS自己实现二维码生成与扫描
- iOS生成二维码,以及二维码扫描
- iOS开发之 二维码生成/二维码扫描
- iOS中 扫描二维码/生成二维码详解
- iOS中 扫描二维码/生成二维码详解
- IOS中生成二维码、扫描二维码
- iOS原生二维码扫描&libqrencode生成二维码
- iOS中 扫描二维码/生成二维码详解
- 扫描二维码+生成二维码
- spring boot(六):如何优雅的使用mybatis
- keytool生成keystore、truststore、证书
- TensorFlow学习笔记(1)--TensorFlow简介,常用基本操作
- Android进阶之RadioButton选中值的获取
- Makefile基础
- iOS扫描二维码与生成二维码
- JDK,JRE,JVM区别与联系
- C#与工业控制-串口下
- 《跟我一起写makefile》(笔记8--makefile中的隐含规则#模式规则#隐含规则变量#自动化变量)
- linux批量替换文件名
- Freeline 让AndroidStudio快的飞起来
- daemon守护进程初识
- sql查询优化
- 命令行切换到目标文件夹、目标文件