ScanViewController.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. [self.navigationController setNavigationBarHidden:YES animated: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 setBackgroundImage:[UIImage imageNamed:@"icon_back"]
  56. forState:UIControlStateNormal];
  57. [button addTarget:self action:@selector(GotoBack)
  58. forControlEvents:UIControlEventTouchUpInside];
  59. button.frame = CGRectMake(0, 0, 15, 18);
  60. UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
  61. self.navigationItem.leftBarButtonItem = menuButton;
  62. //self.view.alpha=0.5;
  63. bgView=[UIView new];
  64. bgView.frame=CGRectMake(Screen_Width/2-300/2, Screen_Height2/2-400/2, 300, 410);
  65. [self.view addSubview:bgView];
  66. bgView.backgroundColor=[UIColor clearColor];
  67. UIButton * scanButton = [UIButton buttonWithType:UIButtonTypeCustom];
  68. scanButton.backgroundColor=[UIColor colorWithRed:205/255.0 green:1/255.0 blue:15/255.0 alpha:1.0];
  69. [scanButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  70. [scanButton setTitle:@"取消" forState:UIControlStateNormal];
  71. scanButton.frame = CGRectMake(10, 370, 280, 40);
  72. [scanButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  73. [bgView addSubview:scanButton];
  74. UILabel * labIntroudction= [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 290, 50)];
  75. labIntroudction.backgroundColor = [UIColor clearColor];
  76. labIntroudction.numberOfLines=2;
  77. labIntroudction.textColor=[UIColor whiteColor];
  78. labIntroudction.text=@"请将扫描的条码至于下面的框内!";
  79. [bgView addSubview:labIntroudction];
  80. UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 65, 300, 300)];
  81. imageView.image = [UIImage imageNamed:@"pick_bg"];
  82. [bgView addSubview:imageView];
  83. upOrdown = NO;
  84. num =0;
  85. _line = [[UIImageView alloc] initWithFrame:CGRectMake(50, 75, 220, 2)];
  86. _line.image = [UIImage imageNamed:@"line.png"];
  87. [bgView addSubview:_line];
  88. timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation) userInfo:nil repeats:YES];
  89. }
  90. /**
  91. didReceiveMemoryWarning
  92. */
  93. - (void)didReceiveMemoryWarning
  94. {
  95. [super didReceiveMemoryWarning];
  96. // Dispose of any resources that can be recreated.
  97. }
  98. - (void)GotoBack{
  99. [self.navigationController popViewControllerAnimated:YES];
  100. }
  101. #pragma mark - 事件
  102. /**
  103. 旋转
  104. */
  105. -(void)animation
  106. {
  107. if (upOrdown == NO) {
  108. num ++;
  109. // _line.frame = CGRectMake(50, 110+2*num, 220, 2);
  110. _line.frame = CGRectMake(50, 60+2*num, 220, 2);
  111. if (2*num == 280) {
  112. upOrdown = YES;
  113. }
  114. }
  115. else {
  116. num --;
  117. _line.frame = CGRectMake(50, 60+2*num, 220, 2);
  118. if (num == 0) {
  119. upOrdown = NO;
  120. }
  121. }
  122. }
  123. - (void)viewWillDisappear:(BOOL)animated {
  124. [super viewWillDisappear:animated];
  125. [self.navigationController setNavigationBarHidden:NO animated:animated];
  126. }
  127. /**
  128. 返回
  129. */
  130. -(void)backAction
  131. {
  132. [self.navigationController popViewControllerAnimated:YES];
  133. }
  134. /**
  135. 打开摄像头
  136. */
  137. - (void)setupCamera
  138. {
  139. // Device
  140. _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  141. // Input
  142. _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
  143. // Output
  144. _output = [[AVCaptureMetadataOutput alloc]init];
  145. [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
  146. // Session
  147. _session = [[AVCaptureSession alloc]init];
  148. [_session setSessionPreset:AVCaptureSessionPresetHigh];
  149. if ([_session canAddInput:self.input])
  150. {
  151. [_session addInput:self.input];
  152. }
  153. if ([_session canAddOutput:self.output])
  154. {
  155. [_session addOutput:self.output];
  156. }
  157. // 条码类型 AVMetadataObjectTypeQRCode
  158. _output.metadataObjectTypes =@[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code,
  159. AVMetadataObjectTypeCode39Mod43Code, AVMetadataObjectTypeEAN13Code,
  160. AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code,
  161. AVMetadataObjectTypeCode128Code, AVMetadataObjectTypePDF417Code,
  162. AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode];
  163. // Preview
  164. _preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session];
  165. _preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
  166. _preview.frame =CGRectMake(10,60,280,280);
  167. [bgView.layer insertSublayer:self.preview atIndex:0];
  168. // Start
  169. [_session startRunning];
  170. }
  171. #pragma mark - AVCaptureMetadataOutputObjectsDelegate
  172. - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
  173. {
  174. NSString *stringValue;
  175. if ([metadataObjects count] >0)
  176. {
  177. AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
  178. stringValue = metadataObject.stringValue;
  179. }
  180. [_session stopRunning];
  181. [self.navigationController popViewControllerAnimated:YES];
  182. if ([self.rootVC respondsToSelector:@selector(reloadDataWithOnlyCode:)]) {
  183. [self.rootVC reloadDataWithOnlyCode:stringValue];
  184. }
  185. }
  186. @end