ScannerViewController.m 53 KB

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