ScanViewController.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // IOS7EndViewController.h
  3. // NewProject
  4. //
  5. // Created by
  6. // Copyright (c) 2013年 Steven. All rights reserved.
  7. //
  8. #import "ScanViewController.h"
  9. #define Screen_Width [[UIScreen mainScreen] bounds].size.width
  10. #if TARGET_IPHONE_SIMULATOR
  11. #define SIMULATOR 1
  12. #elif TARGET_OS_IPHONE
  13. #define SIMULATOR 0
  14. #endif
  15. @interface ScanViewController (){
  16. UIView *bgView;
  17. }
  18. @end
  19. @implementation ScanViewController
  20. #pragma mark - 公共事件
  21. /**
  22. 初始化
  23. @param nibNameOrNil <#nibNameOrNil description#>
  24. @param nibBundleOrNil <#nibBundleOrNil description#>
  25. @return <#return value description#>
  26. */
  27. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  28. {
  29. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  30. if (self) {
  31. // Custom initialization
  32. }
  33. return self;
  34. }
  35. /**
  36. viewWillAppear
  37. @param animated <#animated description#>
  38. */
  39. - (void)viewWillAppear:(BOOL)animated
  40. {
  41. [super viewWillAppear:animated];
  42. self.extendedLayoutIncludesOpaqueBars = YES;
  43. if(!SIMULATOR){
  44. [self setupCamera];
  45. }
  46. }
  47. /**
  48. viewDidLoad
  49. */
  50. - (void)viewDidLoad
  51. {
  52. [super viewDidLoad];
  53. self.view.backgroundColor = [UIColor grayColor];
  54. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  55. [button setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
  56. [button addTarget:self action:@selector(GotoBack)
  57. forControlEvents:UIControlEventTouchUpInside];
  58. button.frame = CGRectMake(0, 0,45,22);
  59. UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
  60. self.navigationItem.leftBarButtonItem = menuButton;
  61. //self.view.alpha=0.5;
  62. bgView = [UIView new];
  63. bgView.frame=CGRectMake(Screen_Width/2-300/2, Screen_Height2/2-400/2, 300, 410);
  64. [self.view addSubview:bgView];
  65. bgView.backgroundColor = [UIColor clearColor];
  66. UIButton * scanButton = [UIButton buttonWithType:UIButtonTypeCustom];
  67. scanButton.backgroundColor = [UIColor colorWithRed:205/255.0 green:1/255.0 blue:15/255.0 alpha:1.0];
  68. [scanButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  69. [scanButton setTitle:@"取消" forState:UIControlStateNormal];
  70. scanButton.frame = CGRectMake(10, 370, 280, 40);
  71. [scanButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  72. [bgView addSubview:scanButton];
  73. UILabel * labIntroudction= [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 290, 50)];
  74. labIntroudction.backgroundColor = [UIColor clearColor];
  75. labIntroudction.numberOfLines=2;
  76. labIntroudction.textColor = [UIColor whiteColor];
  77. labIntroudction.text = @"请将扫描的条码至于下面的框内!";
  78. [bgView addSubview:labIntroudction];
  79. UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 65, 300, 300)];
  80. imageView.image = [UIImage imageNamed:@"pick_bg"];
  81. [bgView addSubview:imageView];
  82. upOrdown = NO;
  83. num =0;
  84. _line = [[UIImageView alloc] initWithFrame:CGRectMake(50, 75, 220, 2)];
  85. _line.image = [UIImage imageNamed:@"line.png"];
  86. [bgView addSubview:_line];
  87. timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation) userInfo:nil repeats:YES];
  88. }
  89. /**
  90. didReceiveMemoryWarning
  91. */
  92. - (void)didReceiveMemoryWarning
  93. {
  94. [super didReceiveMemoryWarning];
  95. // Dispose of any resources that can be recreated.
  96. }
  97. - (void)GotoBack{
  98. [self.navigationController popViewControllerAnimated:YES];
  99. }
  100. #pragma mark - 事件
  101. /**
  102. 旋转
  103. */
  104. - (void)animation
  105. {
  106. if (upOrdown == NO) {
  107. num ++;
  108. // _line.frame = CGRectMake(50, 110+2*num, 220, 2);
  109. _line.frame = CGRectMake(50, 60+2*num, 220, 2);
  110. if (2*num == 280) {
  111. upOrdown = YES;
  112. }
  113. }
  114. else {
  115. num --;
  116. _line.frame = CGRectMake(50, 60+2*num, 220, 2);
  117. if (num == 0) {
  118. upOrdown = NO;
  119. }
  120. }
  121. }
  122. /**
  123. 返回
  124. */
  125. - (void)backAction
  126. {
  127. [self.navigationController popViewControllerAnimated:YES];
  128. }
  129. /**
  130. 打开摄像头
  131. */
  132. - (void)setupCamera
  133. {
  134. // Device
  135. _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  136. // Input
  137. _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
  138. // Output
  139. _output = [[AVCaptureMetadataOutput alloc]init];
  140. [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
  141. // Session
  142. _session = [[AVCaptureSession alloc]init];
  143. [_session setSessionPreset:AVCaptureSessionPresetHigh];
  144. if ([_session canAddInput:self.input])
  145. {
  146. [_session addInput:self.input];
  147. }
  148. if ([_session canAddOutput:self.output])
  149. {
  150. [_session addOutput:self.output];
  151. }
  152. // 条码类型 AVMetadataObjectTypeQRCode
  153. _output.metadataObjectTypes =@[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code,
  154. AVMetadataObjectTypeCode39Mod43Code, AVMetadataObjectTypeEAN13Code,
  155. AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code,
  156. AVMetadataObjectTypeCode128Code, AVMetadataObjectTypePDF417Code,
  157. AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode,AVMetadataObjectTypeDataMatrixCode];
  158. // Preview
  159. _preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
  160. _preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
  161. _preview.frame =CGRectMake(10,60,280,280);
  162. [bgView.layer insertSublayer:self.preview atIndex:0];
  163. // Start
  164. [_session startRunning];
  165. }
  166. #pragma mark - AVCaptureMetadataOutputObjectsDelegate
  167. - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
  168. {
  169. NSString *stringValue;
  170. if ([metadataObjects count] >0)
  171. {
  172. AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
  173. stringValue = metadataObject.stringValue;
  174. }
  175. [_session stopRunning];
  176. [self.navigationController popViewControllerAnimated:YES];
  177. if ([self.rootVC respondsToSelector:@selector(reloadDataWithOnlyCode:)]) {
  178. self.navigationController.navigationBar.translucent=NO;
  179. [self.rootVC reloadDataWithOnlyCode:stringValue];
  180. }
  181. }
  182. @end