ScannerViewController.m 53 KB

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