ScannerViewController.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. __weak typeof(self) weakself = self;
  175. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Warning" message:@"Please goto setting -> privacy -> camera enable RA Image" preferredStyle:UIAlertControllerStyleAlert];
  176. UIAlertAction *action = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  177. [weakself dismissViewControllerAnimated:YES completion:nil];
  178. }];
  179. [alert addAction:action];
  180. [self presentViewController:alert animated:YES completion:nil];
  181. return;
  182. }
  183. AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
  184. // Have to add the output before setting metadata types
  185. [session addOutput:output];
  186. // What different things can we register to recognise?
  187. DebugLog(@"%@", [output availableMetadataObjectTypes]);
  188. // We're only interested in QR Codes
  189. [output setMetadataObjectTypes:@[AVMetadataObjectTypeUPCECode,AVMetadataObjectTypeCode39Code,AVMetadataObjectTypeCode39Mod43Code,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeCode93Code,AVMetadataObjectTypeCode128Code,AVMetadataObjectTypePDF417Code,AVMetadataObjectTypeQRCode,AVMetadataObjectTypeAztecCode]];
  190. DebugLog(@"%@", [output metadataObjectTypes]);
  191. // NSArray* supporttype=output.availableMetadataObjectTypes;
  192. // [output setMetadataObjectTypes:output.availableMetadataObjectTypes];
  193. // This VC is the delegate. Please call us on the main queue
  194. [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
  195. //
  196. // CGRect rt1 = self.ScannerControl.scannerZone.frame;
  197. // Display on screen
  198. _previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
  199. _previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
  200. // _previewLayer.orientation= AVCaptureVideoOrientationLandscapeRight;
  201. UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  202. switch (orientation) {
  203. // UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
  204. // UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
  205. // UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
  206. // UIInterfaceOrientationLandscapeRight
  207. // AVCaptureVideoOrientationPortrait = 1,
  208. // AVCaptureVideoOrientationPortraitUpsideDown = 2,
  209. // AVCaptureVideoOrientationLandscapeRight = 3,
  210. // AVCaptureVideoOrientationLandscapeLeft = 4,
  211. case UIInterfaceOrientationPortrait:
  212. _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationPortrait;
  213. break;
  214. case UIInterfaceOrientationPortraitUpsideDown:
  215. _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationPortraitUpsideDown;
  216. break;
  217. case UIInterfaceOrientationLandscapeLeft:
  218. _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationLandscapeLeft;
  219. break;
  220. case UIInterfaceOrientationLandscapeRight:
  221. _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationLandscapeRight;
  222. break;
  223. default:
  224. break;
  225. }
  226. CGRect rect=self.view.bounds;
  227. // rect.origin.y=rect.origin.y+40;
  228. // rect.size.height = rect.size.height-40;
  229. _previewLayer.bounds = rect;
  230. _previewLayer.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
  231. // [self.view.layer addSublayer:_previewLayer];
  232. [self.view.layer insertSublayer:_previewLayer atIndex:0];
  233. // [self.view addSubview:self.backgroundView];
  234. [self.view bringSubviewToFront:self.backgroundView];
  235. // [self.view addSubview:self.ScannerControl.view];
  236. // CGRect rt = self.ScannerControl.scannerZone.bounds;
  237. // CGRect rt1 =output.rectOfInterest;
  238. // output.rectOfInterest = CGRectMake(0, 0, 300, 300); //self.ScannerControl.scannerZone.frame;
  239. // ScannerControllerView* view =[[ ScannerControllerView alloc] initWithFrame:self.view.frame];
  240. // view.backgroundColor = [UIColor clearColor];
  241. // ScannerLayer * layer = [[ScannerLayer alloc] init];
  242. // [self.view.layer addSublayer:layer];
  243. // Add the view to draw the bounding box for the UIView
  244. _boundingBox = [[SCShapeView alloc] initWithFrame:self.view.bounds];
  245. _boundingBox.backgroundColor = [UIColor clearColor];
  246. _boundingBox.hidden = YES;
  247. [self.view insertSubview:_boundingBox belowSubview:self.backgroundView];
  248. // Add a label to display the resultant message
  249. _decodedMessage = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds) - 75, CGRectGetWidth(self.view.bounds), 75)];
  250. _decodedMessage.numberOfLines = 0;
  251. _decodedMessage.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.9];
  252. _decodedMessage.textColor = [UIColor darkGrayColor];
  253. _decodedMessage.textAlignment = NSTextAlignmentCenter;
  254. [self.view addSubview:_decodedMessage];
  255. _decodedMessage.userInteractionEnabled = YES;
  256. UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  257. [backBtn setTitle:@"Cancel" forState:UIControlStateNormal];
  258. backBtn.titleLabel.backgroundColor = [UIColor clearColor];
  259. backBtn.titleLabel.font = [UIFont systemFontOfSize: 18.0];
  260. [backBtn setTitleColor:UIColorFromRGB(0x007aff) forState:UIControlStateNormal];
  261. backBtn.frame = CGRectMake(CGRectGetMaxX(self.view.frame) - 80, CGRectGetMaxY(self.view.frame)-CGRectGetHeight(_decodedMessage.frame), 60, CGRectGetHeight(_decodedMessage.frame));
  262. [self.view addSubview:backBtn];
  263. [backBtn addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  264. self.scanButton.layer.shadowColor = [UIColor blackColor].CGColor;
  265. self.scanButton.layer.shadowOffset = CGSizeMake(0, 0);
  266. self.scanButton.layer.shadowOpacity = 0.5;
  267. self.scanButton.layer.shadowRadius = 2.0;
  268. self.scanButton.layer.borderColor = [[UIColor darkGrayColor] CGColor];
  269. self.scanButton.layer.borderWidth = 15;
  270. // Start the AVSession running
  271. [session startRunning];
  272. }
  273. - (void)backButtonClick:(UIButton *)sender {
  274. [self dismissViewControllerAnimated:YES completion:nil];
  275. }
  276. //#pragma mark - ScannerControllerDelegate
  277. //-(void)BeginScan:(bool)begin
  278. //{
  279. // self.handelOutput = begin;
  280. //}
  281. #pragma mark - AVCaptureMetadataOutputObjectsDelegate
  282. - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
  283. {
  284. if(!self.handelOutput)
  285. return;
  286. AVMetadataObject * cadedate = nil;
  287. CGPoint centerzone =CGPointMake(CGRectGetMidX(self.focusZone.frame), CGRectGetMidY(self.focusZone.frame));
  288. float distance = MAXFLOAT;
  289. for (AVMetadataObject *metadata in metadataObjects) {
  290. DebugLog(@"%@",metadata);
  291. // if ([metadata.type isEqualToString:AVMetadataObjectTypeQRCode])
  292. {
  293. // Transform the meta-data coordinates to screen coords
  294. AVMetadataMachineReadableCodeObject *transformed = (AVMetadataMachineReadableCodeObject *)[_previewLayer transformedMetadataObjectForMetadataObject:metadata];
  295. // Update the frame on the _boundingBox view, and show it
  296. CGRect rt = transformed.bounds;
  297. CGRectGetMidX(rt);
  298. CGPoint centermeta =CGPointMake(CGRectGetMidX(rt), CGRectGetMidY(rt));
  299. if(CGRectContainsPoint(self.focusZone.frame, centermeta))
  300. {
  301. if(cadedate == nil)
  302. cadedate = metadata;
  303. float distancemeta = (centermeta.x-centerzone.x)*(centermeta.x-centerzone.x)+(centermeta.y-centerzone.y)*(centermeta.y-centerzone.y);
  304. if(distancemeta<distance)
  305. {
  306. distance = distancemeta;
  307. cadedate = metadata;
  308. }
  309. }
  310. }
  311. }
  312. if(cadedate==nil)
  313. return;
  314. AVMetadataMachineReadableCodeObject *transformed = (AVMetadataMachineReadableCodeObject *)[_previewLayer transformedMetadataObjectForMetadataObject:cadedate];
  315. // Update the frame on the _boundingBox view, and show it
  316. // CGRect rt = transformed.bounds;
  317. _boundingBox.frame = transformed.bounds;
  318. _boundingBox.hidden = NO;
  319. // Now convert the corners array into CGPoints in the coordinate system
  320. // of the bounding box itself
  321. // NSArray * corners = transformed.corners;
  322. NSArray *translatedCorners = [self translatePoints:transformed.corners
  323. fromView:self.view
  324. toView:_boundingBox];
  325. // Set the corners array
  326. _boundingBox.corners = translatedCorners;
  327. // Update the view with the decoded text
  328. _decodedMessage.text = [transformed stringValue];
  329. // Start the timer which will hide the overlay
  330. [self startOverlayHideTimer];
  331. [self playSound];
  332. self.handelOutput = false;
  333. [self StopScan];
  334. self.scan_val = [transformed stringValue];
  335. [self dismissViewControllerAnimated:false completion:^{
  336. if(self.returnCode)
  337. self.returnCode(self.scan_val);
  338. }];
  339. // switch (self.target) {
  340. // case SCANNER_TARGET_DETAIL:
  341. // {
  342. // DetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  343. // // dvc
  344. //
  345. //
  346. // dvc.use_model_name = true;
  347. // dvc.model_name=[transformed stringValue];
  348. // dvc.category_id=nil;
  349. // dvc.ispush = true;
  350. //
  351. // [dvc reload];
  352. // [self.navigationController pushViewController:dvc animated:true];
  353. // }
  354. // break;
  355. // case SCANNER_TARGET_CART:
  356. // {
  357. //
  358. //
  359. // [CartUtils add_to_cart:self selectorholder:self selector:@selector(addtocart)];
  360. //// return;
  361. //// [self add_toCart];
  362. // // DetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  363. // // // dvc
  364. // //
  365. // //
  366. // // dvc.use_model_name = true;
  367. // // dvc.model_name=[transformed stringValue];
  368. // // dvc.category_id=nil;
  369. // // dvc.ispush = true;
  370. // //
  371. // // [dvc reload];
  372. // // [self.navigationController pushViewController:dvc animated:true];
  373. // }
  374. // break;
  375. // default:
  376. // break;
  377. // }
  378. }
  379. //-(void)add_toCart
  380. //{
  381. //
  382. //
  383. //
  384. // UIApplication * app = [UIApplication sharedApplication];
  385. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  386. // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  387. // if(appDelegate.bLogin==false)
  388. // {
  389. // LoginViewController * loginvc =[ self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  390. //
  391. //
  392. // loginvc.returnValue = ^(bool blogin){
  393. //
  394. //
  395. //
  396. //
  397. //
  398. // if(blogin)
  399. // {
  400. // if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE*/ true)
  401. // {
  402. // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  403. // [main_vc checklogin:false];
  404. // 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];
  405. //
  406. //
  407. //
  408. // // alert.
  409. // [alert show];
  410. // }
  411. // else
  412. // {
  413. // [main_vc checklogin:true];
  414. // }
  415. // }
  416. //
  417. //
  418. //
  419. // };
  420. //
  421. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  422. //
  423. //
  424. //
  425. //
  426. //
  427. // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  428. //
  429. // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  430. // [self presentViewController:navi animated:YES completion:^{
  431. //
  432. // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  433. //
  434. // DebugLog(@"LoginViewController present.........");
  435. //
  436. // // self.btop = false;
  437. // // <#code#>
  438. // }];
  439. // }
  440. // else
  441. // {
  442. //
  443. //
  444. // if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE&&/*appDelegate.contact_id==nil&&*/appDelegate.order_code==nil)
  445. // {
  446. // [main_vc checklogin:false];
  447. //
  448. // if(appDelegate.can_create_order)
  449. // {
  450. // NSString* msg =@"";
  451. // if(appDelegate.contact_id.length>0)
  452. // {
  453. // msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  454. // msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  455. //
  456. // }
  457. // 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];
  458. //
  459. // // alert.
  460. // [alert show];
  461. // }
  462. // else
  463. // {
  464. // 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];
  465. //
  466. // // alert.
  467. // [alert show];
  468. // }
  469. // }
  470. // else
  471. // {
  472. //
  473. //
  474. // if(appDelegate.order_code==nil)
  475. // [ self neworder];
  476. // else
  477. // [self addtocart];
  478. //
  479. //
  480. // }
  481. // }
  482. //
  483. //
  484. //
  485. //
  486. //
  487. //
  488. //
  489. //
  490. //}
  491. //-(void) neworder
  492. //{
  493. //
  494. // UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Create Order"];
  495. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  496. //
  497. // NSDictionary* return_json = [iSalesNetwork new_Order];
  498. //
  499. // dispatch_async(dispatch_get_main_queue(), ^{
  500. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  501. //
  502. //
  503. // if([[return_json valueForKey:@"result"] intValue]==2)
  504. // {
  505. // int result=[[return_json valueForKey:@"result"] intValue];
  506. // if(result==2)
  507. // {
  508. // //successed.
  509. //
  510. // NSString* order_code = [return_json valueForKey:@"orderCode"];
  511. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  512. // appDelegate.order_code = order_code;
  513. // appDelegate.order_status = [[return_json valueForKey:@"orderStatus"] intValue];
  514. //
  515. //
  516. // [self addtocart];
  517. //
  518. //
  519. //
  520. // }
  521. // }
  522. // else
  523. // {
  524. //
  525. // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  526. // }
  527. //
  528. //
  529. //
  530. //
  531. // });
  532. // });
  533. //}
  534. //-(void) addtocart
  535. //{
  536. //
  537. //
  538. //
  539. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  540. //
  541. // NSDictionary* return_json = [iSalesNetwork add_toCart_byName: self.scan_val];
  542. //
  543. // dispatch_async(dispatch_get_main_queue(), ^{
  544. //
  545. //
  546. //
  547. // if([[return_json valueForKey:@"result"] intValue]==2)
  548. // {
  549. //
  550. //
  551. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  552. //
  553. // [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  554. //
  555. //
  556. //
  557. //
  558. //
  559. // }
  560. // else
  561. // {
  562. //
  563. // [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Add To Cart" controller:self] ;
  564. // }
  565. //
  566. //
  567. //
  568. //
  569. // });
  570. // });
  571. //
  572. //
  573. //}
  574. //- (IBAction)TargetButtonClick:(id)sender {
  575. //
  576. // UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Change scanner target to" message:nil preferredStyle:UIAlertControllerStyleAlert];
  577. // //block代码块取代了delegate
  578. //
  579. //
  580. //
  581. // UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Model Detail" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  582. //
  583. // [self.targetButton setTitle:@"Target: Model Detail" forState:UIControlStateNormal];
  584. //
  585. // self.target = SCANNER_TARGET_DETAIL;
  586. // // UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Remove Models From Cart"];
  587. // // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  588. // //
  589. // // NSDictionary* cart_json = [iSalesNetwork cart_remove:ids];
  590. // //
  591. // // dispatch_async(dispatch_get_main_queue(), ^{
  592. // //
  593. // // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  594. // //
  595. // // if([[cart_json valueForKey:@"result"] intValue]==2)
  596. // // {
  597. // //
  598. // // [self end_edit];
  599. // // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  600. // // [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:true];
  601. // // [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:true];
  602. // // }
  603. // // else
  604. // // {
  605. // // [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Delete Model" controller:self] ;
  606. // // }
  607. // //
  608. // //
  609. // //
  610. // // });
  611. // // });
  612. //
  613. //
  614. // }];
  615. //
  616. // UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cart" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  617. //
  618. //
  619. // [self.targetButton setTitle:@"Target: Cart" forState:UIControlStateNormal];
  620. // self.target = SCANNER_TARGET_CART;
  621. //
  622. // DebugLog(@"No");
  623. // }];
  624. // [alertControl addAction:actionOne];
  625. //
  626. // [alertControl addAction:alertthree];
  627. //
  628. // //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  629. //
  630. //
  631. // [self presentViewController:alertControl animated:YES completion:nil];
  632. //
  633. //
  634. //
  635. //
  636. //
  637. //}
  638. - (IBAction)ScanButtonClick:(id)sender {
  639. self.scanButton.backgroundColor = [UIColor greenColor];
  640. [self.scanButton setTitle:@"Scanning" forState:UIControlStateNormal];
  641. // self.scanButton.layer.borderWidth = 1;
  642. self.handelOutput = true;
  643. // if (self.Scannerdelegate && [self.Scannerdelegate respondsToSelector:@selector(BeginScan:)]) {
  644. // [self.Scannerdelegate BeginScan:true];
  645. // }
  646. }
  647. -(void) StopScan
  648. {
  649. self.scanButton.backgroundColor = [UIColor redColor];
  650. [self.scanButton setTitle:@"Tap" forState:UIControlStateNormal];
  651. }
  652. #pragma mark - Utility Methods
  653. - (void)startOverlayHideTimer
  654. {
  655. // Cancel it if we're already running
  656. if(_boxHideTimer) {
  657. [_boxHideTimer invalidate];
  658. }
  659. // Restart it to hide the overlay when it fires
  660. _boxHideTimer = [NSTimer scheduledTimerWithTimeInterval:0.2
  661. target:self
  662. selector:@selector(removeBoundingBox:)
  663. userInfo:nil
  664. repeats:NO];
  665. }
  666. - (void)removeBoundingBox:(id)sender
  667. {
  668. // Hide the box and remove the decoded text
  669. _boundingBox.hidden = YES;
  670. // _decodedMessage.text = @"";
  671. }
  672. - (NSArray *)translatePoints:(NSArray *)points fromView:(UIView *)fromView toView:(UIView *)toView
  673. {
  674. NSMutableArray *translatedPoints = [NSMutableArray new];
  675. // The points are provided in a dictionary with keys X and Y
  676. for (NSDictionary *point in points) {
  677. // Let's turn them into CGPoints
  678. CGPoint pointValue = CGPointMake([point[@"X"] floatValue], [point[@"Y"] floatValue]);
  679. // Now translate from one view to the other
  680. CGPoint translatedPoint = [fromView convertPoint:pointValue toView:toView];
  681. // Box them up and add to the array
  682. [translatedPoints addObject:[NSValue valueWithCGPoint:translatedPoint]];
  683. }
  684. return [translatedPoints copy];
  685. }
  686. //#pragma mark - UIAlertViewDelegate
  687. //// Called when a button is clicked. The view will be automatically dismissed after this call returns
  688. //- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  689. //{
  690. // UIApplication * app = [UIApplication sharedApplication];
  691. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  692. //
  693. //
  694. // // int count=[self.quantity_text.text intValue];
  695. //
  696. // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  697. // if(buttonIndex==alertView.cancelButtonIndex)
  698. // {
  699. //
  700. //
  701. //
  702. // }
  703. // else if(buttonIndex==1)
  704. // {
  705. // //open exist
  706. // OrderListViewController* ovc =[ self.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  707. // ovc.showNavibar = true;
  708. // ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  709. // if(appDelegate.order_code==nil)
  710. // {
  711. // [self neworder];
  712. // }
  713. // else
  714. // {
  715. // [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  716. //
  717. //
  718. // [self addtocart];
  719. // [main_vc checklogin:true];
  720. //
  721. //
  722. // }
  723. //
  724. // };
  725. // ovc.init_style = OL_OPEN;
  726. // ovc.onCancel = ^(){
  727. //
  728. // [main_vc checklogin:true];
  729. //
  730. // };
  731. //
  732. // [self.navigationController pushViewController:ovc animated:true];
  733. // }
  734. // else
  735. // {
  736. // //create new;
  737. // if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  738. // {
  739. //
  740. //
  741. // ContactListViewController* cvc = [self.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  742. // cvc.showNavibar = true;
  743. // cvc.contact_type = @"Sales_Order_Customer";
  744. // cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  745. //
  746. // appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  747. // appDelegate.customerInfo = value;
  748. //
  749. //
  750. // if(appDelegate.order_code==nil)
  751. // [self neworder];
  752. //
  753. // [main_vc checklogin:true];
  754. // // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  755. //
  756. // //
  757. // // if(self.returnValue)
  758. // // self.returnValue(value);
  759. // };
  760. //
  761. // cvc.onCancel = ^(){
  762. //
  763. // UIViewController *vc= [RAUtils getViewController:self];
  764. // [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order" controller:vc];
  765. //
  766. // };
  767. // cvc.onReset = ^(){
  768. // [main_vc checklogin:true];
  769. // };
  770. //
  771. // [self.navigationController pushViewController:cvc animated:true];
  772. // }
  773. // else
  774. // {
  775. // [self neworder];
  776. // }
  777. //
  778. //
  779. // }
  780. //}
  781. @end