112 系统自带二维码扫描
2015-11-13 19:04
267 查看
1.生成二维码:
2.扫描二维码:
1>需要的属性和头文件:
2>实例化对象:
3>扫描后的代理方法:
- (void)getImage{ // 1.实例化二维码滤镜 CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; // 2.恢复滤镜的默认属性 (因为滤镜有可能保存上一次的属性) [filter setDefaults]; // 3.将字符串转换成NSdata NSData *data = [@"http://www.itheima.com" dataUsingEncoding:NSUTF8StringEncoding]; // 4.通过KVO设置滤镜, 传入data, 将来滤镜就知道要通过传入的数据生成二维码 [filter setValue:data forKey:@"inputMessage"]; // 5.生成二维码 CIImage *outputImage = [filter outputImage]; UIImage *image = [UIImage imageWithCIImage:outputImage]; }
2.扫描二维码:
1>需要的属性和头文件:
#import <AVFoundation/AVFoundation.h> @interface ViewController ()<AVCaptureMetadataOutputObjectsDelegate> @property (nonatomic, strong) AVCaptureSession *session; @property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer; @end
2>实例化对象:
- (void)saomiao { // 1. 实例化拍摄设备 AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; // 2. 设置输入设备 AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; // 3. 设置元数据输出 // 3.1 实例化拍摄元数据输出 AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init]; // 3.3 设置输出数据代理 [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; // 4. 添加拍摄会话 // 4.1 实例化拍摄会话 AVCaptureSession *session = [[AVCaptureSession alloc] init]; // 4.2 添加会话输入 [session addInput:input]; // 4.3 添加会话输出 [session addOutput:output]; // 4.3 设置输出数据类型,需要将元数据输出添加到会话后,才能指定元数据类型,否则会报错 [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]]; self.session = session; // 5. 视频预览图层 // 5.1 实例化预览图层, 传递_session是为了告诉图层将来显示什么内容 AVCaptureVideoPreviewLayer *preview = [AVCaptureVideoPreviewLayer layerWithSession:_session]; preview.videoGravity = AVLayerVideoGravityResizeAspectFill; preview.frame = self.view.bounds; // 5.2 将图层插入当前视图 [self.view.layer insertSublayer:preview atIndex:100]; self.previewLayer = preview; // 6. 启动会话 [_session startRunning]; }
3>扫描后的代理方法:
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{ // 会频繁的扫描,调用代理方法 // 1. 如果扫描完成,停止会话 [self.session stopRunning]; // 2. 删除预览图层 [self.previewLayer removeFromSuperlayer]; NSLog(@"%@", metadataObjects); // 3. 设置界面显示扫描结果 if (metadataObjects.count > 0) { AVMetadataMachineReadableCodeObject *obj = metadataObjects[0]; // 提示:如果需要对url或者名片等信息进行扫描,可以在此进行扩展! NSLog(@"%@", obj.stringValue); } }
相关文章推荐
- PHP http://localhost:63342/untitled1/index.php/Home URL格式访问不到报404 的解决方案
- Daily Scrum - 11/13
- Android Wifi的工作流程
- 分别使用Jmeter/Loadrunner/SoapUI做Web Service接口测试的简单操作
- 怎样从C#中打开数据库并进行 增 删 改 查 操作
- 【数据结构】二叉树
- 使用c++builder中stringgird控件和设定行名称和列名称的问题
- iOS 基础知识的回顾和梳理
- 网上编译系统提示的错误?求解?
- 【数据结构】二叉树
- python 解析json
- Android WiFi框架学习-打开WiFi
- Hadoop运维记录系列(十七)
- Hadoop运维记录系列(十七)
- protobuf java 反射
- android 进程优先级
- Modeling Video Evolution For Action Recognition - cvpr - 2015
- Spring 学习日记 (1)配置文件的装载
- Android之Toast显示不出来原因
- html