// // IOS7EndViewController.h // NewProject // // Created by // Copyright (c) 2013年 Steven. All rights reserved. // #import "ScanViewController.h" #define Screen_Width [[UIScreen mainScreen] bounds].size.width #if TARGET_IPHONE_SIMULATOR #define SIMULATOR 1 #elif TARGET_OS_IPHONE #define SIMULATOR 0 #endif @interface ScanViewController (){ UIView *bgView; } @end @implementation ScanViewController #pragma mark - 公共事件 /** 初始化 @param nibNameOrNil <#nibNameOrNil description#> @param nibBundleOrNil <#nibBundleOrNil description#> @return <#return value description#> */ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } /** viewWillAppear @param animated <#animated description#> */ - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.extendedLayoutIncludesOpaqueBars = YES; if(!SIMULATOR){ [self setupCamera]; } } /** viewDidLoad */ - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor grayColor]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(GotoBack) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(0, 0,45,22); UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button]; self.navigationItem.leftBarButtonItem = menuButton; //self.view.alpha=0.5; bgView = [UIView new]; bgView.frame=CGRectMake(Screen_Width/2-300/2, Screen_Height2/2-400/2, 300, 410); [self.view addSubview:bgView]; bgView.backgroundColor = [UIColor clearColor]; UIButton * scanButton = [UIButton buttonWithType:UIButtonTypeCustom]; scanButton.backgroundColor = [UIColor colorWithRed:205/255.0 green:1/255.0 blue:15/255.0 alpha:1.0]; [scanButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [scanButton setTitle:@"取消" forState:UIControlStateNormal]; scanButton.frame = CGRectMake(10, 370, 280, 40); [scanButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; [bgView addSubview:scanButton]; UILabel * labIntroudction= [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 290, 50)]; labIntroudction.backgroundColor = [UIColor clearColor]; labIntroudction.numberOfLines=2; labIntroudction.textColor = [UIColor whiteColor]; labIntroudction.text = @"请将扫描的条码至于下面的框内!"; [bgView addSubview:labIntroudction]; UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 65, 300, 300)]; imageView.image = [UIImage imageNamed:@"pick_bg"]; [bgView addSubview:imageView]; upOrdown = NO; num =0; _line = [[UIImageView alloc] initWithFrame:CGRectMake(50, 75, 220, 2)]; _line.image = [UIImage imageNamed:@"line.png"]; [bgView addSubview:_line]; timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation) userInfo:nil repeats:YES]; } /** didReceiveMemoryWarning */ - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)GotoBack{ [self.navigationController popViewControllerAnimated:YES]; } #pragma mark - 事件 /** 旋转 */ - (void)animation { if (upOrdown == NO) { num ++; // _line.frame = CGRectMake(50, 110+2*num, 220, 2); _line.frame = CGRectMake(50, 60+2*num, 220, 2); if (2*num == 280) { upOrdown = YES; } } else { num --; _line.frame = CGRectMake(50, 60+2*num, 220, 2); if (num == 0) { upOrdown = NO; } } } /** 返回 */ - (void)backAction { [self.navigationController popViewControllerAnimated:YES]; } /** 打开摄像头 */ - (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 =@[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode,AVMetadataObjectTypeDataMatrixCode]; // Preview _preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session]; _preview.videoGravity = AVLayerVideoGravityResizeAspectFill; _preview.frame =CGRectMake(10,60,280,280); [bgView.layer insertSublayer:self.preview atIndex:0]; // Start [_session startRunning]; } #pragma mark - AVCaptureMetadataOutputObjectsDelegate - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { NSString *stringValue; if ([metadataObjects count] >0) { AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0]; stringValue = metadataObject.stringValue; } [_session stopRunning]; [self.navigationController popViewControllerAnimated:YES]; if ([self.rootVC respondsToSelector:@selector(reloadDataWithOnlyCode:)]) { self.navigationController.navigationBar.translucent=NO; [self.rootVC reloadDataWithOnlyCode:stringValue]; } } @end