ScannerViewController.m 38 KB

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