扫描二维码
2016-01-18 21:40
381 查看
#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewController () <AVCaptureMetadataOutputObjectsDelegate> @property (nonatomic, weak) AVCaptureSession *session; @property (nonatomic, weak) AVCaptureVideoPreviewLayer *layer; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 1.创建捕捉会话 AVCaptureSession *session = [[AVCaptureSession alloc] init]; self.session = session; // 2.添加输入设备(数据从摄像头输入) AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil]; [session addInput:input]; // 3.添加输出数据(示例对象-->类对象-->元类对象-->根元类对象) AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init]; [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; [session addOutput:output]; // 3.1.设置输入元数据的类型(类型是二维码数据) [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]]; // 4.添加扫描图层 AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer layerWithSession:session]; layer.frame = self.view.bounds; [self.view.layer addSublayer:layer]; self.layer = layer; // 5.开始扫描 [session startRunning]; } #pragma mark - 实现output的回调方法 // 当扫描到数据时就会执行该方法 - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { if (metadataObjects.count > 0) { AVMetadataMachineReadableCodeObject *object = [metadataObjects lastObject]; NSLog(@"%@", object.stringValue); // 停止扫描 [self.session stopRunning]; // 将预览图层移除 [self.layer removeFromSuperlayer]; } else { NSLog(@"没有扫描到数据"); } } @end
相关文章推荐
- 20130118
- 【BZOJ2199】 [Usaco2011 Jan]奶牛议会
- CentOS(6.5)安装lsb、查看系统版本信息
- Android笔记--对Android的四种LaunchMode和smail文件和scheme和http 的get、post的理解
- bzoj4305 数学
- R和JSON的傻瓜式编程
- Dijkstra + 优先队列优化 模板
- Tomcat普通用户启动注意事项
- Xcode6中手动添加Precompile Prefix Header
- Map集合
- C语言 - typedef关键字
- 【android基础二】android重要控件——你有多熟悉listview?
- 328. Odd Even Linked List
- 4000 视频采集处理之Z-Turn Board
- 11、ASP.NET MVC入门到精通——AspnetMVC分页
- 【BZOJ1823】 [JSOI2010]满汉全席
- 解决linux下主机名变bogon的问题
- Windows Group Policy Startup script is not executed at startup
- shell的调试方法
- php 调试微信接口时curl无返回值,file_get_contents有返回值的解决方法