ScannerViewController.m 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  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 =@"[\"1689-96*5\",\"normal+\",\"special\",\"special+\"]";// @"[\"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. PopWaitAlert* pop = [RAUtils waiting_pop:@"Create Order" completion:nil];
  646. [RANetwork request_create_order:^(NSMutableDictionary *result) {
  647. NSMutableDictionary* return_json = result;
  648. // [waitalert dismissViewControllerAnimated:YES completion:^{
  649. [pop hide];
  650. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  651. if([[return_json valueForKey:@"result"] intValue]==2)
  652. {
  653. int result=[[return_json valueForKey:@"result"] intValue];
  654. if(result==2)
  655. {
  656. //successed.
  657. NSString* order_code = [return_json valueForKey:@"orderCode"];
  658. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  659. appDelegate.order_code = order_code;
  660. appDelegate.order_status = [[return_json valueForKey:@"orderStatus"] intValue];
  661. [self addtocart];
  662. }
  663. }
  664. else
  665. {
  666. [RAUtils message_box:@"Add To Cart" message:[return_json valueForKey:@"err_msg"] completion:nil];
  667. }
  668. // }];
  669. }];
  670. // }];
  671. }
  672. -(void) addtocart
  673. {
  674. #ifdef SCANNER_ORDER
  675. // if(RASingleton.sharedInstance.enable_OfflineOrder)
  676. {
  677. NSString* key =@"usai2010";
  678. //
  679. // 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\"]]";
  680. //
  681. // NSString* str2 = [AESCrypt encrypt:self.scan_val password:key];
  682. // NSString * cart_item = [AESCrypt decrypt:self.scan_val password:key];
  683. //#if TARGET_IPHONE_SIMULATOR
  684. // cart_item = self.scan_val;
  685. //#endif
  686. NSString * cart_item = self.scan_val;
  687. if(cart_item == nil)
  688. {
  689. //二维码不是scan order的格式
  690. [RAUtils message_box:@"Add To Cart" message:@"QR CODE incorrect." completion:^{
  691. self.handelOutput = true;
  692. }];
  693. return;
  694. }
  695. // NSMutableArray* item = [[RAConvertor string2dict:cart_item] mutableCopy];
  696. NSMutableArray* item = [[RADataProvider request_scan_model_by_names:cart_item] mutableCopy];
  697. self.resultvc.scan_val = item;
  698. self.resultvc.modalPresentationStyle = UIModalPresentationFullScreen;//有三种状态,自己看看是哪种
  699. __weak typeof(self) weakself = self;
  700. __block NSMutableArray* modellist;
  701. self.resultvc.returnValue = ^(id _Nonnull val) {
  702. modellist = (NSMutableArray*)val;
  703. weakself.handelOutput=true;
  704. if(modellist==nil)
  705. return;
  706. // 加购物车
  707. if(RASingleton.sharedInstance.scan_cart ==nil)
  708. {
  709. // NSData* json =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:URL_SO_CART ofType:@"json" ]];
  710. NSMutableDictionary* cartTemplate=[OLDataProvider loadScanTemplate:@"scan_cart.json"];
  711. RASingleton.sharedInstance.scan_cart=cartTemplate;//[[RAConvertor data2dict:json] mutableCopy];
  712. }
  713. // 初始化为 whse;
  714. if(RASingleton.sharedInstance.scan_cart[@"price_type"] ==nil)
  715. {
  716. RASingleton.sharedInstance.scan_cart[@"price_type"] = @1;
  717. RASingleton.sharedInstance.price_type = 1;
  718. }
  719. NSMutableDictionary* section =[RASingleton.sharedInstance.scan_cart[@"section_0"] mutableCopy];
  720. int count =[section[@"count"] intValue];
  721. for(int j=0;j<modellist.count;j++)
  722. {
  723. if([modellist[j][@"count"] intValue]==0)
  724. continue;
  725. NSMutableDictionary* jitem = nil;
  726. jitem = [modellist[j] mutableCopy];
  727. bool newitem = true;
  728. for(int i=0;i<count;i++)
  729. {
  730. NSMutableDictionary* litem = [section[[NSString stringWithFormat:@"item_%i",i]] mutableCopy];
  731. if([litem[@"product_id"] isEqualToString:jitem[@"product_id"]])
  732. {
  733. // int oldcount = [litem[@"stockUom"] intValue];
  734. newitem = false;
  735. litem[@"count"]=@([litem[@"count"] intValue] +[jitem[@"count"] intValue]);
  736. litem[@"cuft"]=@([litem[@"count"] intValue] * [litem[@"unit_cuft"] doubleValue]);
  737. section[[NSString stringWithFormat:@"item_%i",i]] = litem;
  738. break;
  739. }
  740. }
  741. if(newitem)
  742. {
  743. jitem[@"cart_item_id"]=[NSUUID UUID].UUIDString;
  744. section[[NSString stringWithFormat:@"item_%i",count]] = jitem;
  745. section[@"count"]= @(count+1);
  746. count++;
  747. }
  748. }
  749. RASingleton.sharedInstance.scan_cart[@"section_0"] = section;
  750. //加list
  751. #ifdef RA_NOTIFICATION
  752. [ActiveViewController Notify:@"CartViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  753. #else
  754. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  755. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  756. #endif
  757. [OLDataProvider saveScanCart:RASingleton.sharedInstance.scan_cart];
  758. return;
  759. };
  760. [self presentViewController:self.resultvc animated:YES completion:nil];
  761. return;
  762. }
  763. #endif
  764. if(self.type == SCANNER_TYPE_UPCCODE)
  765. {
  766. [RANetwork request_addto_cart_byupc:self.scan_val withScreen:ScreenCodeCamScan completionHandler:^(NSMutableDictionary *result) {
  767. NSDictionary* return_json = result;
  768. if([[return_json valueForKey:@"result"] intValue]==2)
  769. {
  770. #ifdef RA_NOTIFICATION
  771. [ActiveViewController Notify:@"CartViewController,OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  772. #else
  773. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  774. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  775. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  776. #endif
  777. }
  778. else
  779. {
  780. [RAUtils message_box:@"Add To Cart" message:[return_json valueForKey:@"err_msg"] completion:nil];
  781. }
  782. }];
  783. }
  784. else
  785. {
  786. [RANetwork request_addto_cart_byname:self.scan_val withScreen:ScreenCodeCamScan completionHandler:^(NSMutableDictionary *result) {
  787. NSDictionary* return_json = result;
  788. if([[return_json valueForKey:@"result"] intValue]==2)
  789. {
  790. #ifdef RA_NOTIFICATION
  791. [ActiveViewController Notify:@"CartViewController,OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  792. #else
  793. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  794. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  795. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  796. #endif
  797. }
  798. else
  799. {
  800. [RAUtils message_box:@"Add To Cart" message:[return_json valueForKey:@"err_msg"] completion:nil];
  801. }
  802. }];
  803. }
  804. }
  805. - (IBAction)TypeButtonClick:(id)sender {
  806. BOOL shouldChangeTarget = NO;
  807. #ifdef BUILD_CONTRAST
  808. shouldChangeTarget = YES;
  809. #endif
  810. if (!shouldChangeTarget) {
  811. return;
  812. }
  813. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Change code type to" message:nil preferredStyle:UIAlertControllerStyleAlert];
  814. //block代码块取代了delegate
  815. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Model Name" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  816. [self.typeButton setTitle:@"Type: Model Name" forState:UIControlStateNormal];
  817. self.type = SCANNER_TYPE_MODELNAME;
  818. }];
  819. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"UPC CODE" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  820. [self.typeButton setTitle:@"Type: UPC Code" forState:UIControlStateNormal];
  821. self.type = SCANNER_TYPE_UPCCODE;
  822. DebugLog(@"No");
  823. }];
  824. [alertControl addAction:actionOne];
  825. [alertControl addAction:alertthree];
  826. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  827. [self presentViewController:alertControl animated:YES completion:nil];
  828. }
  829. - (IBAction)TargetButtonClick:(id)sender {
  830. BOOL shouldChangeTarget = YES;
  831. #ifdef BUILD_UWAVER
  832. shouldChangeTarget = NO;
  833. #endif
  834. if (!shouldChangeTarget) {
  835. return;
  836. }
  837. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Change scanner target to" message:nil preferredStyle:UIAlertControllerStyleAlert];
  838. //block代码块取代了delegate
  839. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Model Detail" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  840. [self.targetButton setTitle:@"Target: Model Detail" forState:UIControlStateNormal];
  841. self.target = SCANNER_TARGET_DETAIL;
  842. }];
  843. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cart" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  844. [self.targetButton setTitle:@"Target: Cart" forState:UIControlStateNormal];
  845. self.target = SCANNER_TARGET_CART;
  846. DebugLog(@"No");
  847. }];
  848. [alertControl addAction:actionOne];
  849. [alertControl addAction:alertthree];
  850. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  851. [self presentViewController:alertControl animated:YES completion:nil];
  852. }
  853. - (IBAction)ScanButtonClick:(id)sender {
  854. self.scanButton.backgroundColor = [UIColor greenColor];
  855. [self.scanButton setTitle:@"Scanning" forState:UIControlStateNormal];
  856. // self.scanButton.layer.borderWidth = 1;
  857. self.handelOutput = true;
  858. // if (self.Scannerdelegate && [self.Scannerdelegate respondsToSelector:@selector(BeginScan:)]) {
  859. // [self.Scannerdelegate BeginScan:true];
  860. // }
  861. }
  862. -(void) StopScan
  863. {
  864. if(self.auto_style)
  865. return;
  866. self.scanButton.backgroundColor = [UIColor redColor];
  867. [self.scanButton setTitle:@"Tap" forState:UIControlStateNormal];
  868. }
  869. #pragma mark - Utility Methods
  870. - (void)startOverlayHideTimer
  871. {
  872. // Cancel it if we're already running
  873. if(_boxHideTimer) {
  874. [_boxHideTimer invalidate];
  875. }
  876. // Restart it to hide the overlay when it fires
  877. _boxHideTimer = [NSTimer scheduledTimerWithTimeInterval:0.2
  878. target:self
  879. selector:@selector(removeBoundingBox:)
  880. userInfo:nil
  881. repeats:NO];
  882. }
  883. - (void)removeBoundingBox:(id)sender
  884. {
  885. // Hide the box and remove the decoded text
  886. _boundingBox.hidden = YES;
  887. // _decodedMessage.text = @"";
  888. }
  889. - (NSArray *)translatePoints:(NSArray *)points fromView:(UIView *)fromView toView:(UIView *)toView
  890. {
  891. NSMutableArray *translatedPoints = [NSMutableArray new];
  892. // The points are provided in a dictionary with keys X and Y
  893. for (NSDictionary *point in points) {
  894. // Let's turn them into CGPoints
  895. CGPoint pointValue = CGPointMake([point[@"X"] floatValue], [point[@"Y"] floatValue]);
  896. // Now translate from one view to the other
  897. CGPoint translatedPoint = [fromView convertPoint:pointValue toView:toView];
  898. // Box them up and add to the array
  899. [translatedPoints addObject:[NSValue valueWithCGPoint:translatedPoint]];
  900. }
  901. return [translatedPoints copy];
  902. }
  903. @end