ScannerViewController.m 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /*
  2. Copyright 2013 Scott Logic Ltd
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #import "ScannerViewController.h"
  14. @import AVFoundation;
  15. #import <AudioToolbox/AudioToolbox.h>
  16. #import "SCShapeView.h"
  17. #import "ScannerControllerView.h"
  18. //#import "DetailViewController.h"
  19. #import "const.h"
  20. #import "AppDelegate.h"
  21. //#import "MainViewController.h"
  22. //#import "ContactListViewController.h"
  23. //#import "CartUtils.h"
  24. #define SCANNER_TARGET_DETAIL 0
  25. #define SCANNER_TARGET_CART 1
  26. //#import "ScannerControlViewController.h"
  27. @interface ScannerViewController () <AVCaptureMetadataOutputObjectsDelegate> {
  28. AVCaptureVideoPreviewLayer *_previewLayer;
  29. SCShapeView *_boundingBox;
  30. NSTimer *_boxHideTimer;
  31. UILabel *_decodedMessage;
  32. }
  33. @end
  34. @implementation ScannerViewController
  35. //- (UIInterfaceOrientationMask)supportedInterfaceOrientations
  36. //{
  37. // return UIInterfaceOrientationMaskPortrait;
  38. //}
  39. //- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  40. //{
  41. //
  42. // return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
  43. //
  44. //}
  45. - (BOOL)shouldAutorotate
  46. {
  47. return false;
  48. }
  49. -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  50. {
  51. [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
  52. CGRect rect=self.view.bounds;
  53. DebugLog(NSStringFromCGRect(self.focusZone.frame));
  54. // rect.origin.y=rect.origin.y+40;
  55. // rect.size.height = rect.size.height-40;
  56. [CATransaction begin];
  57. [CATransaction setAnimationDuration:0.5];
  58. [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  59. // [self updatePreviewLayerForOrientation:toInterfaceOrientation];
  60. _previewLayer.position = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
  61. // self.backgroundView.highLightRect = CGRectInset(self.focusZone.frame,6,6);
  62. _previewLayer.bounds = rect;
  63. UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  64. switch (orientation) {
  65. case UIInterfaceOrientationPortrait:
  66. // [_previewLayer setAffineTransform:CGAffineTransformMakeRotation(0)];
  67. _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationPortrait;
  68. break;
  69. case UIInterfaceOrientationPortraitUpsideDown:
  70. //[_previewLayer setAffineTransform:CGAffineTransformMakeRotation(M_PI)];
  71. _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationPortraitUpsideDown;
  72. break;
  73. case UIInterfaceOrientationLandscapeLeft:
  74. // [_previewLayer setAffineTransform:CGAffineTransformMakeRotation(M_PI/2)];
  75. _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationLandscapeLeft;
  76. break;
  77. case UIInterfaceOrientationLandscapeRight:
  78. // [_previewLayer setAffineTransform:CGAffineTransformMakeRotation(-M_PI/2)];
  79. _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationLandscapeRight;
  80. break;
  81. default:
  82. break;
  83. }
  84. [CATransaction commit];
  85. }
  86. //- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
  87. //{
  88. // return UIInterfaceOrientationPortrait;
  89. //}
  90. -(void) playSound
  91. {
  92. /*
  93. SystemSoundID sameViewSoundID;
  94. // NSString *filePath = [[NSBundle mainBundle]pathForResource:@"sound" ofType:@"m4r"];
  95. // NSString *thesoundFilePath = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"]; //音乐文件路径
  96. NSString *path = @"/System/Library/Audio/UISounds/begin_video_record.caf";
  97. CFURLRef thesoundURL = (__bridge CFURLRef) [NSURL fileURLWithPath:path] ;
  98. AudioServicesCreateSystemSoundID(thesoundURL, &sameViewSoundID);
  99. //变量SoundID与URL对应
  100. DebugLog(@"%u",(unsigned int)sameViewSoundID);
  101. AudioServicesPlaySystemSound(1112); //播放SoundID声音
  102. */
  103. CFBundleRef mainBundle;
  104. SystemSoundID soundFileObject;
  105. mainBundle = CFBundleGetMainBundle ();
  106. CFURLRef soundFileURLRef = CFBundleCopyResourceURL (
  107. mainBundle,
  108. CFSTR ("softScanBeep" ),
  109. CFSTR ("wav" ),
  110. NULL
  111. );
  112. AudioServicesCreateSystemSoundID (
  113. soundFileURLRef,
  114. &soundFileObject
  115. );
  116. AudioServicesPlaySystemSound(soundFileObject);
  117. }
  118. - (void)viewWillAppear:(BOOL)animated
  119. {
  120. [super viewWillAppear:animated];
  121. [[self navigationController] setNavigationBarHidden:NO animated:NO];
  122. //
  123. //[ attemptRotationToDeviceOrientation];
  124. //[UIViewController attemptRotationToDeviceOrientation];
  125. //
  126. // [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
  127. // self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
  128. // CGRect frame = [UIScreen mainScreen].applicationFrame;
  129. // self.view.bounds = CGRectMake(0, 0, 768, 1024);
  130. }
  131. - (void)onBackClick:(UIButton *)sender {
  132. [self.navigationController popViewControllerAnimated:FALSE];
  133. }
  134. - (void)viewDidLoad
  135. {
  136. [super viewDidLoad];
  137. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  138. style:UIBarButtonItemStylePlain
  139. target:self
  140. action:@selector( onBackClick:)];
  141. // closeButton.tintColor = UIColorFromRGB(0x996633);
  142. self.navigationItem.leftBarButtonItem = closeButton;
  143. }
  144. - (void)viewDidAppear:(BOOL)animated {
  145. [super viewDidAppear:animated];
  146. // 只有在此方法中取得的self.focusZone.frame才正确。
  147. // return;
  148. // [self.backgroundView removeFromSuperview];
  149. //self.back = (ScannerControllerView*)self.view;
  150. self.backgroundView.highLightRect = CGRectInset(self.focusZone.frame,6,6);
  151. [self.backgroundView setNeedsDisplay];
  152. // self.backgroundView.autoresizingMask=0xff;
  153. self.handelOutput = false;
  154. // self.ScannerControl =[self.storyboard instantiateViewControllerWithIdentifier:@"ScannerControlViewController"];
  155. // self.ScannerControl.Scannerdelegate = self;
  156. // self.view.layer.
  157. // Do any additional setup after loading the view, typically from a nib.
  158. // Create a new AVCaptureSession
  159. AVCaptureSession *session = [[AVCaptureSession alloc] init];
  160. session.sessionPreset = AVCaptureSessionPresetHigh;
  161. AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  162. // float factor = device.videoZoomFactor;
  163. // float up = device.activeFormat.videoZoomFactorUpscaleThreshold;
  164. // [device lockForConfiguration:nil];
  165. // device.videoZoomFactor = device.activeFormat.videoZoomFactorUpscaleThreshold;
  166. // [device unlockForConfiguration];
  167. NSError *error = nil;
  168. // Want the normal device
  169. AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
  170. if(input) {
  171. // Add the input to the session
  172. [session addInput:input];
  173. } else {
  174. DebugLog(@"error: %@", error);
  175. return;
  176. }
  177. AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
  178. // Have to add the output before setting metadata types
  179. [session addOutput:output];
  180. // What different things can we register to recognise?
  181. DebugLog(@"%@", [output availableMetadataObjectTypes]);
  182. // We're only interested in QR Codes
  183. [output setMetadataObjectTypes:@[AVMetadataObjectTypeUPCECode,AVMetadataObjectTypeCode39Code,AVMetadataObjectTypeCode39Mod43Code,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeCode93Code,AVMetadataObjectTypeCode128Code,AVMetadataObjectTypePDF417Code,AVMetadataObjectTypeQRCode,AVMetadataObjectTypeAztecCode]];
  184. DebugLog(@"%@", [output metadataObjectTypes]);
  185. // NSArray* supporttype=output.availableMetadataObjectTypes;
  186. // [output setMetadataObjectTypes:output.availableMetadataObjectTypes];
  187. // This VC is the delegate. Please call us on the main queue
  188. [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
  189. //
  190. // CGRect rt1 = self.ScannerControl.scannerZone.frame;
  191. // Display on screen
  192. _previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
  193. _previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
  194. // _previewLayer.orientation= AVCaptureVideoOrientationLandscapeRight;
  195. UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  196. switch (orientation) {
  197. // UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
  198. // UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
  199. // UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
  200. // UIInterfaceOrientationLandscapeRight
  201. // AVCaptureVideoOrientationPortrait = 1,
  202. // AVCaptureVideoOrientationPortraitUpsideDown = 2,
  203. // AVCaptureVideoOrientationLandscapeRight = 3,
  204. // AVCaptureVideoOrientationLandscapeLeft = 4,
  205. case UIInterfaceOrientationPortrait:
  206. _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationPortrait;
  207. break;
  208. case UIInterfaceOrientationPortraitUpsideDown:
  209. _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationPortraitUpsideDown;
  210. break;
  211. case UIInterfaceOrientationLandscapeLeft:
  212. _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationLandscapeLeft;
  213. break;
  214. case UIInterfaceOrientationLandscapeRight:
  215. _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationLandscapeRight;
  216. break;
  217. default:
  218. break;
  219. }
  220. CGRect rect=self.view.bounds;
  221. // rect.origin.y=rect.origin.y+40;
  222. // rect.size.height = rect.size.height-40;
  223. _previewLayer.bounds = rect;
  224. _previewLayer.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
  225. // [self.view.layer addSublayer:_previewLayer];
  226. [self.view.layer insertSublayer:_previewLayer atIndex:0];
  227. // [self.view addSubview:self.backgroundView];
  228. [self.view bringSubviewToFront:self.backgroundView];
  229. // [self.view addSubview:self.ScannerControl.view];
  230. // CGRect rt = self.ScannerControl.scannerZone.bounds;
  231. // CGRect rt1 =output.rectOfInterest;
  232. // output.rectOfInterest = CGRectMake(0, 0, 300, 300); //self.ScannerControl.scannerZone.frame;
  233. // ScannerControllerView* view =[[ ScannerControllerView alloc] initWithFrame:self.view.frame];
  234. // view.backgroundColor = [UIColor clearColor];
  235. // ScannerLayer * layer = [[ScannerLayer alloc] init];
  236. // [self.view.layer addSublayer:layer];
  237. // Add the view to draw the bounding box for the UIView
  238. _boundingBox = [[SCShapeView alloc] initWithFrame:self.view.bounds];
  239. _boundingBox.backgroundColor = [UIColor clearColor];
  240. _boundingBox.hidden = YES;
  241. [self.view insertSubview:_boundingBox belowSubview:self.backgroundView];
  242. // Add a label to display the resultant message
  243. _decodedMessage = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds) - 75, CGRectGetWidth(self.view.bounds), 75)];
  244. _decodedMessage.numberOfLines = 0;
  245. _decodedMessage.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.9];
  246. _decodedMessage.textColor = [UIColor darkGrayColor];
  247. _decodedMessage.textAlignment = NSTextAlignmentCenter;
  248. [self.view addSubview:_decodedMessage];
  249. _decodedMessage.userInteractionEnabled = YES;
  250. UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  251. [backBtn setTitle:@"Cancel" forState:UIControlStateNormal];
  252. backBtn.titleLabel.backgroundColor = [UIColor clearColor];
  253. backBtn.titleLabel.font = [UIFont systemFontOfSize: 18.0];
  254. [backBtn setTitleColor:UIColorFromRGB(0x007aff) forState:UIControlStateNormal];
  255. backBtn.frame = CGRectMake(CGRectGetMaxX(self.view.frame) - 80, CGRectGetMaxY(self.view.frame)-CGRectGetHeight(_decodedMessage.frame), 60, CGRectGetHeight(_decodedMessage.frame));
  256. [self.view addSubview:backBtn];
  257. [backBtn addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  258. self.scanButton.layer.shadowColor = [UIColor blackColor].CGColor;
  259. self.scanButton.layer.shadowOffset = CGSizeMake(0, 0);
  260. self.scanButton.layer.shadowOpacity = 0.5;
  261. self.scanButton.layer.shadowRadius = 2.0;
  262. self.scanButton.layer.borderColor = [[UIColor darkGrayColor] CGColor];
  263. self.scanButton.layer.borderWidth = 15;
  264. // Start the AVSession running
  265. [session startRunning];
  266. }
  267. - (void)backButtonClick:(UIButton *)sender {
  268. [self dismissViewControllerAnimated:YES completion:nil];
  269. }
  270. //#pragma mark - ScannerControllerDelegate
  271. //-(void)BeginScan:(bool)begin
  272. //{
  273. // self.handelOutput = begin;
  274. //}
  275. #pragma mark - AVCaptureMetadataOutputObjectsDelegate
  276. - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
  277. {
  278. if(!self.handelOutput)
  279. return;
  280. AVMetadataObject * cadedate = nil;
  281. CGPoint centerzone =CGPointMake(CGRectGetMidX(self.focusZone.frame), CGRectGetMidY(self.focusZone.frame));
  282. float distance = MAXFLOAT;
  283. for (AVMetadataObject *metadata in metadataObjects) {
  284. DebugLog(@"%@",metadata);
  285. // if ([metadata.type isEqualToString:AVMetadataObjectTypeQRCode])
  286. {
  287. // Transform the meta-data coordinates to screen coords
  288. AVMetadataMachineReadableCodeObject *transformed = (AVMetadataMachineReadableCodeObject *)[_previewLayer transformedMetadataObjectForMetadataObject:metadata];
  289. // Update the frame on the _boundingBox view, and show it
  290. CGRect rt = transformed.bounds;
  291. CGRectGetMidX(rt);
  292. CGPoint centermeta =CGPointMake(CGRectGetMidX(rt), CGRectGetMidY(rt));
  293. if(CGRectContainsPoint(self.focusZone.frame, centermeta))
  294. {
  295. if(cadedate == nil)
  296. cadedate = metadata;
  297. float distancemeta = (centermeta.x-centerzone.x)*(centermeta.x-centerzone.x)+(centermeta.y-centerzone.y)*(centermeta.y-centerzone.y);
  298. if(distancemeta<distance)
  299. {
  300. distance = distancemeta;
  301. cadedate = metadata;
  302. }
  303. }
  304. }
  305. }
  306. if(cadedate==nil)
  307. return;
  308. AVMetadataMachineReadableCodeObject *transformed = (AVMetadataMachineReadableCodeObject *)[_previewLayer transformedMetadataObjectForMetadataObject:cadedate];
  309. // Update the frame on the _boundingBox view, and show it
  310. // CGRect rt = transformed.bounds;
  311. _boundingBox.frame = transformed.bounds;
  312. _boundingBox.hidden = NO;
  313. // Now convert the corners array into CGPoints in the coordinate system
  314. // of the bounding box itself
  315. // NSArray * corners = transformed.corners;
  316. NSArray *translatedCorners = [self translatePoints:transformed.corners
  317. fromView:self.view
  318. toView:_boundingBox];
  319. // Set the corners array
  320. _boundingBox.corners = translatedCorners;
  321. // Update the view with the decoded text
  322. _decodedMessage.text = [transformed stringValue];
  323. // Start the timer which will hide the overlay
  324. [self startOverlayHideTimer];
  325. [self playSound];
  326. self.handelOutput = false;
  327. [self StopScan];
  328. self.scan_val = [transformed stringValue];
  329. [self dismissViewControllerAnimated:false completion:^{
  330. if(self.returnCode)
  331. self.returnCode(self.scan_val);
  332. }];
  333. // switch (self.target) {
  334. // case SCANNER_TARGET_DETAIL:
  335. // {
  336. // DetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  337. // // dvc
  338. //
  339. //
  340. // dvc.use_model_name = true;
  341. // dvc.model_name=[transformed stringValue];
  342. // dvc.category_id=nil;
  343. // dvc.ispush = true;
  344. //
  345. // [dvc reload];
  346. // [self.navigationController pushViewController:dvc animated:true];
  347. // }
  348. // break;
  349. // case SCANNER_TARGET_CART:
  350. // {
  351. //
  352. //
  353. // [CartUtils add_to_cart:self selectorholder:self selector:@selector(addtocart)];
  354. //// return;
  355. //// [self add_toCart];
  356. // // DetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  357. // // // dvc
  358. // //
  359. // //
  360. // // dvc.use_model_name = true;
  361. // // dvc.model_name=[transformed stringValue];
  362. // // dvc.category_id=nil;
  363. // // dvc.ispush = true;
  364. // //
  365. // // [dvc reload];
  366. // // [self.navigationController pushViewController:dvc animated:true];
  367. // }
  368. // break;
  369. // default:
  370. // break;
  371. // }
  372. }
  373. //-(void)add_toCart
  374. //{
  375. //
  376. //
  377. //
  378. // UIApplication * app = [UIApplication sharedApplication];
  379. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  380. // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  381. // if(appDelegate.bLogin==false)
  382. // {
  383. // LoginViewController * loginvc =[ self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  384. //
  385. //
  386. // loginvc.returnValue = ^(bool blogin){
  387. //
  388. //
  389. //
  390. //
  391. //
  392. // if(blogin)
  393. // {
  394. // if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE*/ true)
  395. // {
  396. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  397. // [main_vc checklogin:false];
  398. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"Do you want to add to pending a order or a new order", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil),NSLocalizedString(@"Add to new order", nil), nil];
  399. //
  400. //
  401. //
  402. // // alert.
  403. // [alert show];
  404. // }
  405. // else
  406. // {
  407. // [main_vc checklogin:true];
  408. // }
  409. // }
  410. //
  411. //
  412. //
  413. // };
  414. //
  415. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  416. //
  417. //
  418. //
  419. //
  420. //
  421. // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  422. //
  423. // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  424. // [self presentViewController:navi animated:YES completion:^{
  425. //
  426. // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  427. //
  428. // DebugLog(@"LoginViewController present.........");
  429. //
  430. // // self.btop = false;
  431. // // <#code#>
  432. // }];
  433. // }
  434. // else
  435. // {
  436. //
  437. //
  438. // if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE&&/*appDelegate.contact_id==nil&&*/appDelegate.order_code==nil)
  439. // {
  440. // [main_vc checklogin:false];
  441. //
  442. // if(appDelegate.can_create_order)
  443. // {
  444. // NSString* msg =@"";
  445. // if(appDelegate.contact_id.length>0)
  446. // {
  447. // msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  448. // msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  449. //
  450. // }
  451. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil),NSLocalizedString(@"Add to new order", nil), nil];
  452. //
  453. // // alert.
  454. // [alert show];
  455. // }
  456. // else
  457. // {
  458. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil), nil];
  459. //
  460. // // alert.
  461. // [alert show];
  462. // }
  463. // }
  464. // else
  465. // {
  466. //
  467. //
  468. // if(appDelegate.order_code==nil)
  469. // [ self neworder];
  470. // else
  471. // [self addtocart];
  472. //
  473. //
  474. // }
  475. // }
  476. //
  477. //
  478. //
  479. //
  480. //
  481. //
  482. //
  483. //
  484. //}
  485. //-(void) neworder
  486. //{
  487. //
  488. // UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Create Order"];
  489. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  490. //
  491. // NSDictionary* return_json = [iSalesNetwork new_Order];
  492. //
  493. // dispatch_async(dispatch_get_main_queue(), ^{
  494. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  495. //
  496. //
  497. // if([[return_json valueForKey:@"result"] intValue]==2)
  498. // {
  499. // int result=[[return_json valueForKey:@"result"] intValue];
  500. // if(result==2)
  501. // {
  502. // //successed.
  503. //
  504. // NSString* order_code = [return_json valueForKey:@"orderCode"];
  505. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  506. // appDelegate.order_code = order_code;
  507. // appDelegate.order_status = [[return_json valueForKey:@"orderStatus"] intValue];
  508. //
  509. //
  510. // [self addtocart];
  511. //
  512. //
  513. //
  514. // }
  515. // }
  516. // else
  517. // {
  518. //
  519. // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  520. // }
  521. //
  522. //
  523. //
  524. //
  525. // });
  526. // });
  527. //}
  528. //-(void) addtocart
  529. //{
  530. //
  531. //
  532. //
  533. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  534. //
  535. // NSDictionary* return_json = [iSalesNetwork add_toCart_byName: self.scan_val];
  536. //
  537. // dispatch_async(dispatch_get_main_queue(), ^{
  538. //
  539. //
  540. //
  541. // if([[return_json valueForKey:@"result"] intValue]==2)
  542. // {
  543. //
  544. //
  545. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  546. //
  547. // [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  548. //
  549. //
  550. //
  551. //
  552. //
  553. // }
  554. // else
  555. // {
  556. //
  557. // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  558. // }
  559. //
  560. //
  561. //
  562. //
  563. // });
  564. // });
  565. //
  566. //
  567. //}
  568. //- (IBAction)TargetButtonClick:(id)sender {
  569. //
  570. // UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Change scanner target to" message:nil preferredStyle:UIAlertControllerStyleAlert];
  571. // //block代码块取代了delegate
  572. //
  573. //
  574. //
  575. // UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Model Detail" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  576. //
  577. // [self.targetButton setTitle:@"Target: Model Detail" forState:UIControlStateNormal];
  578. //
  579. // self.target = SCANNER_TARGET_DETAIL;
  580. // // UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Remove Models From Cart"];
  581. // // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  582. // //
  583. // // NSDictionary* cart_json = [iSalesNetwork cart_remove:ids];
  584. // //
  585. // // dispatch_async(dispatch_get_main_queue(), ^{
  586. // //
  587. // // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  588. // //
  589. // // if([[cart_json valueForKey:@"result"] intValue]==2)
  590. // // {
  591. // //
  592. // // [self end_edit];
  593. // // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  594. // // [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:true];
  595. // // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:true];
  596. // // }
  597. // // else
  598. // // {
  599. // // [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Delete Model" controller:self] ;
  600. // // }
  601. // //
  602. // //
  603. // //
  604. // // });
  605. // // });
  606. //
  607. //
  608. // }];
  609. //
  610. // UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cart" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  611. //
  612. //
  613. // [self.targetButton setTitle:@"Target: Cart" forState:UIControlStateNormal];
  614. // self.target = SCANNER_TARGET_CART;
  615. //
  616. // DebugLog(@"No");
  617. // }];
  618. // [alertControl addAction:actionOne];
  619. //
  620. // [alertControl addAction:alertthree];
  621. //
  622. // //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  623. //
  624. //
  625. // [self presentViewController:alertControl animated:YES completion:nil];
  626. //
  627. //
  628. //
  629. //
  630. //
  631. //}
  632. - (IBAction)ScanButtonClick:(id)sender {
  633. self.scanButton.backgroundColor = [UIColor greenColor];
  634. [self.scanButton setTitle:@"Scanning" forState:UIControlStateNormal];
  635. // self.scanButton.layer.borderWidth = 1;
  636. self.handelOutput = true;
  637. // if (self.Scannerdelegate && [self.Scannerdelegate respondsToSelector:@selector(BeginScan:)]) {
  638. // [self.Scannerdelegate BeginScan:true];
  639. // }
  640. }
  641. -(void) StopScan
  642. {
  643. self.scanButton.backgroundColor = [UIColor redColor];
  644. [self.scanButton setTitle:@"Tap" forState:UIControlStateNormal];
  645. }
  646. #pragma mark - Utility Methods
  647. - (void)startOverlayHideTimer
  648. {
  649. // Cancel it if we're already running
  650. if(_boxHideTimer) {
  651. [_boxHideTimer invalidate];
  652. }
  653. // Restart it to hide the overlay when it fires
  654. _boxHideTimer = [NSTimer scheduledTimerWithTimeInterval:0.2
  655. target:self
  656. selector:@selector(removeBoundingBox:)
  657. userInfo:nil
  658. repeats:NO];
  659. }
  660. - (void)removeBoundingBox:(id)sender
  661. {
  662. // Hide the box and remove the decoded text
  663. _boundingBox.hidden = YES;
  664. // _decodedMessage.text = @"";
  665. }
  666. - (NSArray *)translatePoints:(NSArray *)points fromView:(UIView *)fromView toView:(UIView *)toView
  667. {
  668. NSMutableArray *translatedPoints = [NSMutableArray new];
  669. // The points are provided in a dictionary with keys X and Y
  670. for (NSDictionary *point in points) {
  671. // Let's turn them into CGPoints
  672. CGPoint pointValue = CGPointMake([point[@"X"] floatValue], [point[@"Y"] floatValue]);
  673. // Now translate from one view to the other
  674. CGPoint translatedPoint = [fromView convertPoint:pointValue toView:toView];
  675. // Box them up and add to the array
  676. [translatedPoints addObject:[NSValue valueWithCGPoint:translatedPoint]];
  677. }
  678. return [translatedPoints copy];
  679. }
  680. //#pragma mark - UIAlertViewDelegate
  681. //// Called when a button is clicked. The view will be automatically dismissed after this call returns
  682. //- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  683. //{
  684. // UIApplication * app = [UIApplication sharedApplication];
  685. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  686. //
  687. //
  688. // // int count=[self.quantity_text.text intValue];
  689. //
  690. // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  691. // if(buttonIndex==alertView.cancelButtonIndex)
  692. // {
  693. //
  694. //
  695. //
  696. // }
  697. // else if(buttonIndex==1)
  698. // {
  699. // //open exist
  700. // OrderListViewController* ovc =[ self.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  701. // ovc.showNavibar = true;
  702. // ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  703. // if(appDelegate.order_code==nil)
  704. // {
  705. // [self neworder];
  706. // }
  707. // else
  708. // {
  709. // [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  710. //
  711. //
  712. // [self addtocart];
  713. // [main_vc checklogin:true];
  714. //
  715. //
  716. // }
  717. //
  718. // };
  719. // ovc.init_style = OL_OPEN;
  720. // ovc.onCancel = ^(){
  721. //
  722. // [main_vc checklogin:true];
  723. //
  724. // };
  725. //
  726. // [self.navigationController pushViewController:ovc animated:true];
  727. // }
  728. // else
  729. // {
  730. // //create new;
  731. // if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  732. // {
  733. //
  734. //
  735. // ContactListViewController* cvc = [self.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  736. // cvc.showNavibar = true;
  737. // cvc.contact_type = @"Sales_Order_Customer";
  738. // cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  739. //
  740. // appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  741. // appDelegate.customerInfo = value;
  742. //
  743. //
  744. // if(appDelegate.order_code==nil)
  745. // [self neworder];
  746. //
  747. // [main_vc checklogin:true];
  748. // // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  749. //
  750. // //
  751. // // if(self.returnValue)
  752. // // self.returnValue(value);
  753. // };
  754. //
  755. // cvc.onCancel = ^(){
  756. //
  757. // UIViewController *vc= [RAUtils getViewController:self];
  758. // [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order" controller:vc];
  759. //
  760. // };
  761. // cvc.onReset = ^(){
  762. // [main_vc checklogin:true];
  763. // };
  764. //
  765. // [self.navigationController pushViewController:cvc animated:true];
  766. // }
  767. // else
  768. // {
  769. // [self neworder];
  770. // }
  771. //
  772. //
  773. // }
  774. //}
  775. @end