您的位置:首页 > 移动开发 > IOS开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: