iOS7开发的新特性之扫描二维码
2014-03-19 16:14
274 查看
苹果的扫描二维码的确使用起来超级简单,直接上代码:
.h文件
#import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @interface ViewController : UIViewController<AVCaptureMetadataOutputObjectsDelegate>{ int num; BOOL upOrdown; NSTimer * timer; } @property (strong,nonatomic)AVCaptureDevice * device; @property (strong,nonatomic)AVCaptureDeviceInput * input; @property (strong,nonatomic)AVCaptureMetadataOutput * output; @property (strong,nonatomic)AVCaptureSession * session; @property (strong,nonatomic)AVCaptureVideoPreviewLayer * preview; @property (nonatomic, retain) UIImageView * line; @end
.m文件
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self setupCamera]; upOrdown = NO; num =0; _line = [[UIImageView alloc] initWithFrame:CGRectMake(50, 110, 220, 2)]; _line.image = [UIImage imageNamed:@"line.png"]; [self.view addSubview:_line]; timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation1) userInfo:nil repeats:YES]; } -(void)animation1 { if (upOrdown == NO) { num ++; _line.frame = CGRectMake(50, 110+2*num, 220, 2); if (2*num == 280) { upOrdown = YES; } } else { num --; _line.frame = CGRectMake(50, 110+2*num, 220, 2); if (num == 0) { upOrdown = NO; } } } #pragma mark - AVCaptureMetadataOutputObjectsDelegate - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { NSString *QRCode = nil; for (AVMetadataObject *metadata in metadataObjects) { if ([metadata.type isEqualToString:AVMetadataObjectTypeQRCode]) { // This will never happen; nobody has ever scanned a QR code... ever QRCode = [(AVMetadataMachineReadableCodeObject *)metadata stringValue]; break; [_session stopRunning]; } } NSLog(@"QR Code: %@", QRCode); } - (void)setupCamera { // Device _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; // Input _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil]; // Output _output = [[AVCaptureMetadataOutput alloc]init]; [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()]; // Session _session = [[AVCaptureSession alloc]init]; [_session setSessionPreset:AVCaptureSessionPresetHigh]; if ([_session canAddInput:self.input]) { [_session addInput:self.input]; } if ([_session canAddOutput:self.output]) { [_session addOutput:self.output]; } // 条码类型 AVMetadataObjectTypeQRCode _output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode]; // 设置扫描器涂层的相关 _preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session]; _preview.videoGravity = AVLayerVideoGravityResizeAspectFill; _preview.frame =CGRectMake(20,110,280,280); [self.view.layer insertSublayer:self.preview atIndex:0]; // Start [_session startRunning]; } @end
相关文章推荐
- iOS7自带扫描二维码、条形码功能实现
- android开发之集成zxing,二维码,以及扫描二维码的功能实现。带源代码下载
- iOS开发之扫描二维码功能的实现
- IOS开发小功能2:二维码扫描界面的设计(横线上下移动)
- iOS开发——iOS7(及以后版本) SDK自带二维码(含条形码)扫码、二维码生成
- ios开发原生的扫描二维码的实现以及限制扫描区域rectOfInterest遇到的一些坑
- Android开发实例——扫描二维码
- iOS开发笔记--开发者所需要知道的iOS7 SDK新特性
- [置顶] Android移动开发-在Android项目里集成开源框架ZXing实现扫描二维码的功能
- Android开发——通过扫描二维码,打开或者下载Android应用
- iOS7或之前版本中 二维码的生成与扫描 开灯
- Force.com微信开发系列 WeChat 扫描带参数二维码事件
- 钉钉开发系列(八)二维码扫描登录的实现
- ios7扫描二维码重复回调的问题
- ios开发原生的扫描二维码的实现以及限制扫描区域rectOfInterest遇到的一些坑
- Android开发之Zbar实现二维码扫描功能
- 开发一个基于ZXing库以及安卓Studio的二维码扫描小程序(一)
- 用c#开发微信(2)扫描二维码,用户授权后获取用户基本信息 (源码下载)
- iOS开发--二维码/条形码(扫描(可区域)和生成)
- 使用 IOS7 原生API进行二维码条形码的扫描