Advertisement
Guest User

Untitled

a guest
Dec 7th, 2011
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.91 KB | None | 0 0
  1. /* (c) 2009 eBay Inc. */
  2. #import "BarCodeAppViewController.h"
  3. #import "OverlayController.h"
  4. #if defined(RL_LOCALIZED)
  5. #import "Localization.h"
  6. #endif
  7.  
  8. @implementation OverlayController
  9. @synthesize lightbtn;
  10. @synthesize flash,buttonid;
  11. - (void)barcodePickerController:(BarcodePickerController*)picker statusUpdated:(NSDictionary*)status
  12. {
  13. // In the status dictionary:
  14.  
  15. // "FoundBarcodes" key is a NSSet of all discovered barcodes this scan session
  16. // "NewFoundBarcodes" is a NSSet of barcodes discovered in the most recent pass.
  17. // When a barcode is found, it is added to both sets. The NewFoundBarcodes
  18. // set is cleaned out each pass.
  19.  
  20. // "Guidance" can be used to help guide the user through the process of discovering
  21. // a long barcode in sections. Currently only works for Code 39.
  22.  
  23. // "Valid" is TRUE once there are valid barcode results.
  24. // "InRange" is TRUE if there's currently a barcode detected the viewfinder. The barcode
  25. // may not have been decoded yet.
  26.  
  27.  
  28. BOOL isValid = [(NSNumber*)[status objectForKey:@"Valid"] boolValue];
  29. BOOL inRange = [(NSNumber*)[status objectForKey:@"InRange"] boolValue];
  30.  
  31. // Make the RedLaser stripe more vivid when Barcode is in Range.
  32. if (inRange)
  33. {
  34. _rectLayer.strokeColor = [[UIColor greenColor] CGColor];
  35. }
  36. else
  37. {
  38. _rectLayer.strokeColor = [[UIColor whiteColor] CGColor];
  39. }
  40.  
  41. if (isValid)
  42. {
  43. [self beepOrVibrate];
  44. [self.parentPicker doneScanning];
  45. }
  46.  
  47. int guidanceLevel = [[status objectForKey:@"Guidance"] intValue];
  48. if (guidanceLevel == 1)
  49. {
  50. textCue.text = @"Try moving the camera close to each part of the barcode";
  51. } else if (guidanceLevel == 2)
  52. {
  53. textCue.text = [status objectForKey:@"PartialBarcode"];
  54. } else
  55. {
  56. textCue.text = @"";
  57. }
  58. }
  59.  
  60.  
  61.  
  62.  
  63.  
  64. // Checks if the phone is in vibrate mode, in which case the scanner
  65. // vibrates instead of beeps.
  66.  
  67. -(void)beepOrVibrate
  68. {
  69. if(!_isSilent)
  70. {
  71. UInt32 routeSize = sizeof (CFStringRef);
  72. CFStringRef route = NULL;
  73. AudioSessionGetProperty (
  74. kAudioSessionProperty_AudioRoute,
  75. &routeSize,
  76. &route
  77. );
  78.  
  79. if (CFStringGetLength(route) == 0) {
  80. AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
  81. }
  82. else {
  83. AudioServicesPlaySystemSound(_scanSuccessSound);
  84. }
  85. }
  86. }
  87.  
  88.  
  89. - (void)viewDidLoad
  90. {
  91. [super viewDidLoad];
  92. isGreen = FALSE;
  93.  
  94. // Create active region rectangle
  95. _rectLayer = [[CAShapeLayer layer] retain];
  96. _rectLayer.fillColor = [[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.2] CGColor];
  97. _rectLayer.strokeColor = [[UIColor whiteColor] CGColor];
  98. _rectLayer.lineWidth = 3;
  99. _rectLayer.anchorPoint = CGPointZero;
  100. [self.view.layer addSublayer:_rectLayer];
  101.  
  102. self->_isSilent = [[NSUserDefaults standardUserDefaults] boolForKey:@"silent_pref"];
  103.  
  104. if(!_isSilent) // If silent, no need to do this.
  105. {
  106. NSURL* aFileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"beep" ofType:@"wav"] isDirectory:NO];
  107. AudioServicesCreateSystemSoundID((CFURLRef)aFileURL, &_scanSuccessSound);
  108.  
  109. UInt32 flag = 0;
  110. OSStatus error = AudioServicesSetProperty(kAudioServicesPropertyIsUISound,
  111. sizeof(UInt32),
  112. &_scanSuccessSound,
  113. sizeof(UInt32),
  114. &flag);
  115.  
  116. float aBufferLength = 1.0; // In seconds
  117. error = AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration,
  118. sizeof(aBufferLength), &aBufferLength);
  119.  
  120. /* Create and warm up an audio session */
  121. AudioSessionInitialize(NULL, NULL, NULL, NULL);
  122. AudioSessionSetActive(TRUE);
  123. }
  124.  
  125. flash = FALSE;
  126. UIImage *lookup_img = [UIImage imageNamed:@"cancel_bt.png"];
  127.  
  128. UIButton *cancelbtn = [UIButton buttonWithType:UIButtonTypeCustom];
  129. [cancelbtn setFrame:CGRectMake(10,435, 80, 30)];
  130. [cancelbtn setTitle:@"Cancel" forState:UIControlStateNormal];
  131. [cancelbtn setBackgroundImage:lookup_img forState:UIControlStateNormal];
  132.  
  133. cancelbtn.backgroundColor=[UIColor blackColor];
  134. [cancelbtn addTarget:self action:@selector(cancelPressed)
  135. forControlEvents:UIControlEventTouchUpInside];
  136.  
  137.  
  138. UIImageView *imageView3;
  139. imageView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cancel.png"]];
  140. imageView3.frame = CGRectMake(0,420, 320, 60);
  141. [imageView3 bringSubviewToFront:cancelbtn];
  142.  
  143.  
  144.  
  145.  
  146. lightbtn = [UIButton buttonWithType:UIButtonTypeCustom];
  147. [lightbtn setFrame:CGRectMake(220,435, 80, 30)];
  148. lightbtn.tag=1;
  149. [lightbtn setTitle:@"Light" forState:UIControlStateNormal];
  150. [lightbtn setBackgroundImage:lookup_img forState:UIControlStateNormal];
  151. //btn.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
  152. lightbtn.backgroundColor=[UIColor blackColor];
  153. [lightbtn addTarget:self action:@selector(flashPressed)
  154. forControlEvents:UIControlEventTouchUpInside];
  155.  
  156.  
  157. [imageView3 bringSubviewToFront:lightbtn];
  158.  
  159. [self.view addSubview:imageView3];
  160. [self.view addSubview:cancelbtn];
  161.  
  162.  
  163. [self.view addSubview:lightbtn];
  164.  
  165.  
  166.  
  167.  
  168.  
  169. }
  170.  
  171.  
  172.  
  173. -(void)cancelPressed
  174. {
  175. if(self.parentPicker != nil)
  176. {
  177. // Tell the picker we're done
  178. [self.parentPicker doneScanning];
  179. }
  180. }
  181.  
  182. - (void)buttonAction:(UIButton*)sender
  183. {
  184. /* if (sender.selected)
  185. return;*/
  186. if (sender.tag == 1){
  187.  
  188. if(lightbtn.selected == FALSE){
  189. buttonid=@"ON";
  190.  
  191. NSLog(@"ON");
  192.  
  193. }
  194. else if(lightbtn.selected == TRUE)
  195. {
  196. // lightbtn.selected = FALSE;
  197. buttonid=@"OFF";
  198.  
  199. NSLog(@"OFF");
  200.  
  201. }
  202. }
  203. }
  204.  
  205. -(void)flashPressed {
  206. //buttonid=@"ON";
  207.  
  208. buttonid=@"ON";
  209. if([buttonid isEqualToString:@"ON"])
  210. // if(buttonid==@"ON")
  211. // if ([flashButton style] == UIBarButtonItemStyleBordered)
  212. {
  213. flash = TRUE;
  214. NSLog(@"flash ON");
  215. //[flashButton setStyle:UIBarButtonItemStyleDone];
  216. [self.parentPicker turnFlash:YES];
  217. }
  218. else //(lightbtn.state==TRUE)
  219. {
  220. flash = FALSE;
  221. NSLog(@"flash OFF");
  222. //[flashButton setStyle:UIBarButtonItemStyleBordered];
  223. [self.parentPicker turnFlash:NO];
  224. }
  225. }
  226.  
  227. // Optionally, you can change the active scanning region.
  228. // The region specified below is the default, and lines up
  229. // with the default overlay. It is recommended to keep the
  230. // active region similar in size to the default region.
  231. // Additionally, the iPhone 3GS may not focus as well if
  232. // the region is too far away from center.
  233. //
  234. // In portrait mode only the top and bottom of this rectangle
  235. // is used. The x-position and width specified are ignored.
  236.  
  237.  
  238. - (void) setPortraitLayout
  239. {
  240. // Set portrait
  241. self.parentPicker.orientation = UIImageOrientationUp;
  242.  
  243. // Set the active scanning region for portrait mode
  244. [self.parentPicker setActiveRegion:CGRectMake(0, 100, 320, 250)];
  245.  
  246. // Animate the UI changes
  247. CGAffineTransform transform = CGAffineTransformMakeRotation(0);
  248. //self.view.transform = transform;
  249.  
  250. [UIView beginAnimations:@"rotateToPortrait" context:nil]; // Tell UIView we're ready to start animations.
  251. [UIView setAnimationDelegate:self];
  252. //[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
  253. [UIView setAnimationCurve: UIViewAnimationCurveLinear ];
  254. [UIView setAnimationDuration: 0.5];
  255.  
  256. redlaserLogo.transform = transform;
  257.  
  258. [self setActiveRegionRect];
  259.  
  260. [UIView commitAnimations]; // Animate!
  261. }
  262.  
  263. - (void) setLandscapeLayout
  264. {
  265. // Set landscape
  266. self.parentPicker.orientation = UIImageOrientationRight;
  267.  
  268. // Set the active scanning region for landscape mode
  269. [self.parentPicker setActiveRegion:CGRectMake(100, 0, 120, 436)];
  270.  
  271. // Animate the UI changes
  272. CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159/2);
  273. //self.view.transform = transform;
  274.  
  275. //[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
  276.  
  277. [UIView beginAnimations:@"rotateToLandscape" context:nil]; // Tell UIView we're ready to start animations.
  278. [UIView setAnimationDelegate:self];
  279. //[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
  280. [UIView setAnimationCurve: UIViewAnimationCurveLinear ];
  281. [UIView setAnimationDuration: 0.5];
  282.  
  283. redlaserLogo.transform = transform;
  284.  
  285. [self setActiveRegionRect];
  286.  
  287. [UIView commitAnimations]; // Animate!
  288. }
  289.  
  290. - (IBAction) rotatePressed
  291. {
  292. // Swap the orientation
  293. if (self.parentPicker.orientation == UIImageOrientationUp)
  294. {
  295. [self setLandscapeLayout];
  296. }
  297. else
  298. {
  299. [self setPortraitLayout];
  300. }
  301. }
  302.  
  303. - (CGMutablePathRef)newRectPathInRect:(CGRect)rect
  304. {
  305. CGMutablePathRef path = CGPathCreateMutable();
  306. CGPathAddRect(path, NULL, rect);
  307. return path;
  308. }
  309.  
  310. - (void) setActiveRegionRect
  311. {
  312. [_rectLayer setFrame:CGRectMake(self.parentPicker.activeRegion.origin.x,
  313. self.parentPicker.activeRegion.origin.y,
  314. self.parentPicker.activeRegion.size.width,
  315. self.parentPicker.activeRegion.size.height)];
  316.  
  317. CGPathRef path = [self newRectPathInRect:_rectLayer.bounds];
  318. [_rectLayer setPath:path];
  319. CGPathRelease(path);
  320. [_rectLayer needsLayout];
  321. }
  322.  
  323. -(void)back{
  324. BarCodeAppViewController *ob = [[BarCodeAppViewController alloc] init];
  325. [self presentModalViewController:ob animated:YES];
  326.  
  327. }
  328. - (void)viewDidUnload
  329. {
  330.  
  331. AudioServicesDisposeSystemSoundID(_scanSuccessSound);
  332. if(!_isSilent) { AudioSessionSetActive(FALSE); }
  333. }
  334.  
  335. - (void)viewWillAppear:(BOOL)animated
  336. {
  337.  
  338.  
  339.  
  340. if (self.parentPicker.orientation == UIImageOrientationUp)
  341. {
  342. [self setPortraitLayout];
  343. }
  344. else
  345. {
  346.  
  347. [self setLandscapeLayout];
  348. }
  349.  
  350. if (![self.parentPicker hasFlash])
  351. {
  352.  
  353. [flashButton setEnabled:NO];
  354. NSMutableArray * items = [[toolBar items] mutableCopy];
  355. // NSLog(@"in view");
  356. [items removeObject:flashButton];
  357. [toolBar setItems:items animated:NO];
  358. [items release];
  359. }else
  360. {
  361. [flashButton setEnabled:YES];
  362. [flashButton setStyle:UIBarButtonItemStyleBordered];
  363. }
  364.  
  365. textCue.text = @"";
  366. }
  367.  
  368.  
  369. - (void)dealloc
  370. {
  371. [lightbtn release];
  372. [super dealloc];
  373. }
  374.  
  375. @end
  376.  
  377.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement