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 = [RAUtils query_orientation:self];//= [[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. #if TARGET_IPHONE_SIMULATOR//模拟器
  206. // if(RASingleton.sharedInstance.enable_OfflineOrder)
  207. {
  208. // 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\"]]";
  209. self.scan_val =@"[\"1689-96*5\",\"normal+\",\"special\",\"special+\"]";// @"[\"1002A70BK\"]";//
  210. [self addtocart];
  211. }
  212. return;
  213. #else
  214. #ifdef SCANNER_ORDER
  215. self.auto_style = true;
  216. #else
  217. self.auto_style = false;
  218. #endif
  219. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  220. style:UIBarButtonItemStylePlain
  221. target:self
  222. action:@selector( onBackClick:)];
  223. #ifdef SCANNER_ORDER
  224. closeButton.tintColor = [UIColor whiteColor];
  225. #endif
  226. // closeButton.tintColor = UIColorFromRGB(0x996633);
  227. self.navigationItem.leftBarButtonItem = closeButton;
  228. // return;
  229. // [self.backgroundView removeFromSuperview];
  230. //self.back = (ScannerControllerView*)self.view;
  231. if(self.auto_style)
  232. {
  233. // cgrectoff
  234. self.focusZone.frame = self.view.frame;
  235. self.backgroundView.auto_style = self.auto_style;
  236. self.backgroundView.highLightRect = self.view.frame;
  237. self.scanButton.hidden = true;
  238. self.handelOutput = true;
  239. }
  240. else
  241. {
  242. self.backgroundView.highLightRect = CGRectInset(self.focusZone.frame,6,6);
  243. self.handelOutput = false;
  244. }
  245. // self.backgroundView.autoresizingMask=0xff;
  246. #ifdef SCANNER_ORDER
  247. self.targetButton.hidden = true;
  248. #endif
  249. // Do any additional setup after loading the view, typically from a nib.
  250. // Create a new AVCaptureSession
  251. AVCaptureSession *session = [[AVCaptureSession alloc] init];
  252. session.sessionPreset = AVCaptureSessionPresetHigh;
  253. AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  254. // float factor = device.videoZoomFactor;
  255. // float up = device.activeFormat.videoZoomFactorUpscaleThreshold;
  256. // [device lockForConfiguration:nil];
  257. // device.videoZoomFactor = device.activeFormat.videoZoomFactorUpscaleThreshold;
  258. // [device unlockForConfiguration];
  259. NSError *error = nil;
  260. // Want the normal device
  261. AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
  262. if(input) {
  263. // Add the input to the session
  264. [session addInput:input];
  265. } else {
  266. DebugLog(@"error: %@", error);
  267. return;
  268. }
  269. AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
  270. // Have to add the output before setting metadata types
  271. [session addOutput:output];
  272. // What different things can we register to recognise?
  273. DebugLog(@"%@", [output availableMetadataObjectTypes]);
  274. // We're only interested in QR Codes
  275. [output setMetadataObjectTypes:@[AVMetadataObjectTypeUPCECode,AVMetadataObjectTypeCode39Code,AVMetadataObjectTypeCode39Mod43Code,AVMetadataObjectTypeEAN13Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeCode93Code,AVMetadataObjectTypeCode128Code,AVMetadataObjectTypePDF417Code,AVMetadataObjectTypeQRCode,AVMetadataObjectTypeAztecCode]];
  276. DebugLog(@"%@", [output metadataObjectTypes]);
  277. // NSArray* supporttype=output.availableMetadataObjectTypes;
  278. // [output setMetadataObjectTypes:output.availableMetadataObjectTypes];
  279. // This VC is the delegate. Please call us on the main queue
  280. [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
  281. //
  282. // CGRect rt1 = self.ScannerControl.scannerZone.frame;
  283. // Display on screen
  284. _previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
  285. _previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
  286. // _previewLayer.orientation= AVCaptureVideoOrientationLandscapeRight;
  287. UIInterfaceOrientation orientation = [RAUtils query_orientation:self];//= [[UIApplication sharedApplication]statusBarOrientation];
  288. switch (orientation) {
  289. // UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
  290. // UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
  291. // UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
  292. // UIInterfaceOrientationLandscapeRight
  293. // AVCaptureVideoOrientationPortrait = 1,
  294. // AVCaptureVideoOrientationPortraitUpsideDown = 2,
  295. // AVCaptureVideoOrientationLandscapeRight = 3,
  296. // AVCaptureVideoOrientationLandscapeLeft = 4,
  297. case UIInterfaceOrientationPortrait:
  298. _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationPortrait;
  299. break;
  300. case UIInterfaceOrientationPortraitUpsideDown:
  301. _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationPortraitUpsideDown;
  302. break;
  303. case UIInterfaceOrientationLandscapeLeft:
  304. _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationLandscapeLeft;
  305. break;
  306. case UIInterfaceOrientationLandscapeRight:
  307. _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationLandscapeRight;
  308. break;
  309. default:
  310. break;
  311. }
  312. CGRect rect=self.view.bounds;
  313. // rect.origin.y=rect.origin.y+40;
  314. // rect.size.height = rect.size.height-40;
  315. _previewLayer.bounds = rect;
  316. _previewLayer.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
  317. [self.view.layer addSublayer:_previewLayer];
  318. [self.view addSubview:self.backgroundView];
  319. // [self.view addSubview:self.ScannerControl.view];
  320. // CGRect rt = self.ScannerControl.scannerZone.bounds;
  321. // CGRect rt1 =output.rectOfInterest;
  322. // output.rectOfInterest = CGRectMake(0, 0, 300, 300); //self.ScannerControl.scannerZone.frame;
  323. // ScannerControllerView* view =[[ ScannerControllerView alloc] initWithFrame:self.view.frame];
  324. // view.backgroundColor = [UIColor clearColor];
  325. // ScannerLayer * layer = [[ScannerLayer alloc] init];
  326. // [self.view.layer addSublayer:layer];
  327. // Add the view to draw the bounding box for the UIView
  328. _boundingBox = [[SCShapeView alloc] initWithFrame:self.view.bounds];
  329. _boundingBox.backgroundColor = [UIColor clearColor];
  330. _boundingBox.hidden = YES;
  331. [self.view addSubview:_boundingBox];
  332. // Add a label to display the resultant message
  333. _decodedMessage = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds) - 75, CGRectGetWidth(self.view.bounds), 75)];
  334. _decodedMessage.numberOfLines = 0;
  335. _decodedMessage.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.9];
  336. _decodedMessage.textColor = [UIColor darkGrayColor];
  337. _decodedMessage.textAlignment = NSTextAlignmentCenter;
  338. [self.view addSubview:_decodedMessage];
  339. #ifdef SCANNER_ORDER
  340. _decodedMessage.hidden = TRUE;
  341. #endif
  342. self.scanButton.layer.shadowColor = [UIColor blackColor].CGColor;
  343. self.scanButton.layer.shadowOffset = CGSizeMake(0, 0);
  344. self.scanButton.layer.shadowOpacity = 0.5;
  345. self.scanButton.layer.shadowRadius = 2.0;
  346. self.scanButton.layer.borderColor = [[UIColor darkGrayColor] CGColor];
  347. self.scanButton.layer.borderWidth = 15;
  348. // Start the AVSession running
  349. [session startRunning];
  350. #ifdef BUILD_UWAVER
  351. [self.targetButton setTitle:@"Target: Model Detail" forState:UIControlStateNormal];
  352. self.target = SCANNER_TARGET_DETAIL;
  353. #endif
  354. #ifdef BUILD_CONTRAST
  355. self.typeButton.hidden = false;
  356. [self.typeButton setTitle:@"Type: UPC Code" forState:UIControlStateNormal];
  357. self.type = SCANNER_TYPE_UPCCODE;
  358. #else
  359. self.typeButton.hidden = true;
  360. self.type = SCANNER_TYPE_MODELNAME;
  361. #endif
  362. #endif
  363. }
  364. //#pragma mark - ScannerControllerDelegate
  365. //-(void)BeginScan:(bool)begin
  366. //{
  367. // self.handelOutput = begin;
  368. //}
  369. #pragma mark - AVCaptureMetadataOutputObjectsDelegate
  370. - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
  371. {
  372. if(!self.handelOutput)
  373. return;
  374. // if(!self.auto_style)
  375. {
  376. self.handelOutput = false;
  377. [self StopScan];
  378. }
  379. AVMetadataObject * cadedate = nil;
  380. CGPoint centerzone =CGPointMake(CGRectGetMidX(self.focusZone.frame), CGRectGetMidY(self.focusZone.frame));
  381. float distance = MAXFLOAT;
  382. for (AVMetadataObject *metadata in metadataObjects) {
  383. DebugLog(@"%@",metadata);
  384. // if ([metadata.type isEqualToString:AVMetadataObjectTypeQRCode])
  385. {
  386. // Transform the meta-data coordinates to screen coords
  387. AVMetadataMachineReadableCodeObject *transformed = (AVMetadataMachineReadableCodeObject *)[_previewLayer transformedMetadataObjectForMetadataObject:metadata];
  388. // Update the frame on the _boundingBox view, and show it
  389. CGRect rt = transformed.bounds;
  390. CGRectGetMidX(rt);
  391. CGPoint centermeta =CGPointMake(CGRectGetMidX(rt), CGRectGetMidY(rt));
  392. if(CGRectContainsPoint(self.focusZone.frame, centermeta))
  393. {
  394. if(cadedate == nil)
  395. cadedate = metadata;
  396. float distancemeta = (centermeta.x-centerzone.x)*(centermeta.x-centerzone.x)+(centermeta.y-centerzone.y)*(centermeta.y-centerzone.y);
  397. if(distancemeta<distance)
  398. {
  399. distance = distancemeta;
  400. cadedate = metadata;
  401. }
  402. }
  403. }
  404. }
  405. if(cadedate==nil)
  406. {
  407. if (self.auto_style)
  408. self.handelOutput = true;
  409. return;
  410. }
  411. AVMetadataMachineReadableCodeObject *transformed = (AVMetadataMachineReadableCodeObject *)[_previewLayer transformedMetadataObjectForMetadataObject:cadedate];
  412. // Update the frame on the _boundingBox view, and show it
  413. // CGRect rt = transformed.bounds;
  414. _boundingBox.frame = transformed.bounds;
  415. // _boundingBox.hidden = NO;
  416. // Now convert the corners array into CGPoints in the coordinate system
  417. // of the bounding box itself
  418. // NSArray * corners = transformed.corners;
  419. NSArray *translatedCorners = [self translatePoints:transformed.corners
  420. fromView:self.view
  421. toView:_boundingBox];
  422. // Set the corners array
  423. _boundingBox.corners = translatedCorners;
  424. // Update the view with the decoded text
  425. // Start the timer which will hide the overlay
  426. [self startOverlayHideTimer];
  427. [self playSound];
  428. self.scan_val = [transformed stringValue];
  429. DebugLog(@"scan result:%@",self.scan_val);
  430. #ifdef SCANNER_ORDER
  431. // if(RASingleton.sharedInstance.enable_OfflineOrder)
  432. {
  433. [self addtocart];
  434. // [CartUtils add_to_cart:self selectorholder:self selector:@selector(addtocart)];
  435. return;
  436. }
  437. #endif
  438. NSString *action = nil;
  439. switch (self.target) {
  440. case SCANNER_TARGET_DETAIL:
  441. {
  442. DetailViewController* dvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Model" bundle:nil] instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  443. // dvc
  444. action = @"Open A Model";
  445. [ERPUtils googleAnalyticsSendRequestString:nil WithScreen:ScreenCodeCamScan Action:action Extra:nil];
  446. if(self.type== SCANNER_TYPE_MODELNAME)
  447. dvc.use_model_name = true;
  448. else
  449. {
  450. dvc.use_upc_code = true;
  451. dvc.upc_code =[transformed stringValue];
  452. }
  453. dvc.model_name=[transformed stringValue];
  454. dvc.category_id=nil;
  455. dvc.ispush = true;
  456. #ifdef BUILD_UWAVER
  457. if(!(transformed.type==AVMetadataObjectTypeQRCode))
  458. dvc.groupName = [transformed stringValue];
  459. #endif
  460. //
  461. [dvc reload];
  462. [self.navigationController pushViewController:dvc animated:true];
  463. }
  464. break;
  465. case SCANNER_TARGET_CART:
  466. {
  467. [CartUtils add_to_cart:self selectorholder:self selector:@selector(addtocart)];
  468. // return;
  469. // [self add_toCart];
  470. // DetailViewController* dvc = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Model" bundle:nil] instantiateViewControllerWithIdentifier:@"DetailViewController" ];
  471. // // dvc
  472. //
  473. //
  474. // dvc.use_model_name = true;
  475. // dvc.model_name=[transformed stringValue];
  476. // dvc.category_id=nil;
  477. // dvc.ispush = true;
  478. //
  479. // [dvc reload];
  480. // [self.navigationController pushViewController:dvc animated:true];
  481. }
  482. break;
  483. default:
  484. break;
  485. }
  486. }
  487. //
  488. //
  489. //-(void)add_toCart
  490. //{
  491. //
  492. // [CartUtils add_to_cart:self selectorholder:self selector:@selector(addtocart)];
  493. // return;
  494. //
  495. //// UIApplication * app = [UIApplication sharedApplication];
  496. //// AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  497. //// MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  498. //// if(appDelegate.bLogin==false)
  499. //// {
  500. //// LoginViewController * loginvc =[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginViewController"];
  501. ////
  502. ////
  503. //// loginvc.returnValue = ^(bool blogin){
  504. ////
  505. ////
  506. ////
  507. ////
  508. ////
  509. //// if(blogin)
  510. //// {
  511. //// if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE*/ true)
  512. //// {
  513. ////
  514. ////#ifdef RA_NOTIFICATION
  515. //// [ActiveViewController Notify:@"CategoryViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  516. ////#else
  517. ////
  518. //// [((MainViewController*)appDelegate.main_vc) reloadCategory:true immediately:false];
  519. ////#endif
  520. ////
  521. ////
  522. ////
  523. //// [main_vc checklogin:false];
  524. ////
  525. ////
  526. ////
  527. //// UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Add to cart" message:@"Do you want to add to pending a order or a new order" preferredStyle:UIAlertControllerStyleAlert];
  528. ////
  529. ////
  530. //// UIAlertAction *action_1 = [UIAlertAction actionWithTitle:@"Add to pending order" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  531. ////
  532. ////
  533. ////
  534. //// }];
  535. ////
  536. //// UIAlertAction *action_2 = [UIAlertAction actionWithTitle:@"Add to new order" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  537. ////
  538. ////
  539. ////
  540. //// }];
  541. //// UIAlertAction *action_3 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  542. ////
  543. ////
  544. ////
  545. //// }];
  546. ////
  547. //// [alertController addAction:action_1];
  548. //// [alertController addAction:action_2];
  549. //// [alertController addAction:action_3];
  550. ////
  551. ////
  552. //// [self presentViewController:alertController animated:YES completion:nil];
  553. ////
  554. ////// UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"Do you want to add to pending a order or a new order", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil),NSLocalizedString(@"Add to new order", nil), nil];
  555. //////
  556. //////
  557. //////
  558. ////// // alert.
  559. ////// [alert show];
  560. //// }
  561. //// else
  562. //// {
  563. //// [main_vc checklogin:true];
  564. //// }
  565. //// }
  566. ////
  567. ////
  568. ////
  569. //// };
  570. ////
  571. //// UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  572. ////
  573. ////
  574. ////
  575. ////
  576. ////
  577. //// // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  578. ////
  579. //// navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  580. //// [self presentViewController:navi animated:YES completion:^{
  581. ////
  582. //// // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  583. ////
  584. //// DebugLog(@"LoginViewController present.........");
  585. ////
  586. //// // self.btop = false;
  587. //// // <#code#>
  588. //// }];
  589. //// }
  590. //// else
  591. //// {
  592. ////
  593. //// //if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE&&/*appDelegate.contact_id==nil&&*/appDelegate.order_code==nil)
  594. //// if(appDelegate.order_code==nil)
  595. //// {
  596. //// [main_vc checklogin:false];
  597. ////
  598. //// if(appDelegate.can_create_order)
  599. //// {
  600. //// NSString* msg =@"";
  601. //// if(appDelegate.contact_id.length>0)
  602. //// {
  603. //// msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  604. //// msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  605. ////
  606. //// }
  607. //// UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil),NSLocalizedString(@"Add to new order", nil), nil];
  608. ////
  609. //// // alert.
  610. //// [alert show];
  611. //// }
  612. //// else
  613. //// {
  614. //// UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Add to pending order", nil), nil];
  615. ////
  616. //// // alert.
  617. //// [alert show];
  618. //// }
  619. //// }
  620. //// else
  621. //// {
  622. ////
  623. ////
  624. //// if(appDelegate.order_code==nil)
  625. //// [ self neworder];
  626. //// else
  627. //// [self addtocart];
  628. ////
  629. ////
  630. //// }
  631. //// }
  632. ////
  633. ////
  634. ////
  635. ////
  636. ////
  637. ////
  638. ////
  639. ////
  640. //}
  641. -(void) neworder
  642. {
  643. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Create Order" completion:^{
  644. PopWaitAlert* pop = [RAUtils waiting_pop:@"Create Order" completion:nil];
  645. [RANetwork request_create_order:^(NSMutableDictionary *result) {
  646. NSMutableDictionary* return_json = result;
  647. // [waitalert dismissViewControllerAnimated:YES completion:^{
  648. [pop hide];
  649. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  650. if([[return_json valueForKey:@"result"] intValue]==2)
  651. {
  652. int result=[[return_json valueForKey:@"result"] intValue];
  653. if(result==2)
  654. {
  655. //successed.
  656. NSString* order_code = [return_json valueForKey:@"orderCode"];
  657. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  658. appDelegate.order_code = order_code;
  659. appDelegate.order_status = [[return_json valueForKey:@"orderStatus"] intValue];
  660. [self addtocart];
  661. }
  662. }
  663. else
  664. {
  665. [RAUtils message_box:@"Add To Cart" message:[return_json valueForKey:@"err_msg"] completion:nil];
  666. }
  667. // }];
  668. }];
  669. // }];
  670. }
  671. -(void) addtocart
  672. {
  673. #ifdef SCANNER_ORDER
  674. // if(RASingleton.sharedInstance.enable_OfflineOrder)
  675. {
  676. NSString* key =@"usai2010";
  677. //
  678. // 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\"]]";
  679. //
  680. // NSString* str2 = [AESCrypt encrypt:self.scan_val password:key];
  681. // NSString * cart_item = [AESCrypt decrypt:self.scan_val password:key];
  682. //#if TARGET_IPHONE_SIMULATOR
  683. // cart_item = self.scan_val;
  684. //#endif
  685. NSString * cart_item = self.scan_val;
  686. if(cart_item == nil)
  687. {
  688. //二维码不是scan order的格式
  689. [RAUtils message_box:@"Add To Cart" message:@"QR CODE incorrect." completion:^{
  690. self.handelOutput = true;
  691. }];
  692. return;
  693. }
  694. // NSMutableArray* item = [[RAConvertor string2dict:cart_item] mutableCopy];
  695. NSMutableArray* item = [[RADataProvider request_scan_model_by_names:cart_item] mutableCopy];
  696. self.resultvc.scan_val = item;
  697. self.resultvc.modalPresentationStyle = UIModalPresentationFullScreen;//有三种状态,自己看看是哪种
  698. __weak typeof(self) weakself = self;
  699. __block NSMutableArray* modellist;
  700. self.resultvc.returnValue = ^(id _Nonnull val) {
  701. modellist = (NSMutableArray*)val;
  702. weakself.handelOutput=true;
  703. if(modellist==nil)
  704. return;
  705. // 加购物车
  706. if(RASingleton.sharedInstance.scan_cart ==nil)
  707. {
  708. // NSData* json =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:URL_SO_CART ofType:@"json" ]];
  709. NSMutableDictionary* cartTemplate=[OLDataProvider loadScanTemplate:@"scan_cart.json"];
  710. RASingleton.sharedInstance.scan_cart=cartTemplate;//[[RAConvertor data2dict:json] mutableCopy];
  711. }
  712. // 初始化为 whse;
  713. if(RASingleton.sharedInstance.scan_cart[@"price_type"] ==nil)
  714. {
  715. RASingleton.sharedInstance.scan_cart[@"price_type"] = @1;
  716. RASingleton.sharedInstance.price_type = 1;
  717. }
  718. NSMutableDictionary* section =[RASingleton.sharedInstance.scan_cart[@"section_0"] mutableCopy];
  719. int count =[section[@"count"] intValue];
  720. for(int j=0;j<modellist.count;j++)
  721. {
  722. if([modellist[j][@"count"] intValue]==0)
  723. continue;
  724. NSMutableDictionary* jitem = nil;
  725. jitem = [modellist[j] mutableCopy];
  726. bool newitem = true;
  727. for(int i=0;i<count;i++)
  728. {
  729. NSMutableDictionary* litem = [section[[NSString stringWithFormat:@"item_%i",i]] mutableCopy];
  730. if([litem[@"product_id"] isEqualToString:jitem[@"product_id"]])
  731. {
  732. // int oldcount = [litem[@"stockUom"] intValue];
  733. newitem = false;
  734. litem[@"count"]=@([litem[@"count"] intValue] +[jitem[@"count"] intValue]);
  735. litem[@"cuft"]=@([litem[@"count"] intValue] * [litem[@"unit_cuft"] doubleValue]);
  736. section[[NSString stringWithFormat:@"item_%i",i]] = litem;
  737. break;
  738. }
  739. }
  740. if(newitem)
  741. {
  742. jitem[@"cart_item_id"]=[NSUUID UUID].UUIDString;
  743. section[[NSString stringWithFormat:@"item_%i",count]] = jitem;
  744. section[@"count"]= @(count+1);
  745. count++;
  746. }
  747. }
  748. RASingleton.sharedInstance.scan_cart[@"section_0"] = section;
  749. //加list
  750. #ifdef RA_NOTIFICATION
  751. [ActiveViewController Notify:@"CartViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  752. #else
  753. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  754. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  755. #endif
  756. [OLDataProvider saveScanCart:RASingleton.sharedInstance.scan_cart];
  757. return;
  758. };
  759. [self presentViewController:self.resultvc animated:YES completion:nil];
  760. return;
  761. }
  762. #endif
  763. if(self.type == SCANNER_TYPE_UPCCODE)
  764. {
  765. [RANetwork request_addto_cart_byupc:self.scan_val withScreen:ScreenCodeCamScan completionHandler:^(NSMutableDictionary *result) {
  766. NSDictionary* return_json = result;
  767. if([[return_json valueForKey:@"result"] intValue]==2)
  768. {
  769. #ifdef RA_NOTIFICATION
  770. [ActiveViewController Notify:@"CartViewController,OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  771. #else
  772. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  773. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  774. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  775. #endif
  776. }
  777. else
  778. {
  779. [RAUtils message_box:@"Add To Cart" message:[return_json valueForKey:@"err_msg"] completion:nil];
  780. }
  781. }];
  782. }
  783. else
  784. {
  785. [RANetwork request_addto_cart_byname:self.scan_val withScreen:ScreenCodeCamScan completionHandler:^(NSMutableDictionary *result) {
  786. NSDictionary* return_json = result;
  787. if([[return_json valueForKey:@"result"] intValue]==2)
  788. {
  789. #ifdef RA_NOTIFICATION
  790. [ActiveViewController Notify:@"CartViewController,OrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  791. #else
  792. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  793. [((MainViewController*)appDelegate.main_vc) reloadOrder:true immediately:false];
  794. [((MainViewController*)appDelegate.main_vc) reloadCart:true immediately:false];
  795. #endif
  796. }
  797. else
  798. {
  799. [RAUtils message_box:@"Add To Cart" message:[return_json valueForKey:@"err_msg"] completion:nil];
  800. }
  801. }];
  802. }
  803. }
  804. - (IBAction)TypeButtonClick:(id)sender {
  805. BOOL shouldChangeTarget = NO;
  806. #ifdef BUILD_CONTRAST
  807. shouldChangeTarget = YES;
  808. #endif
  809. if (!shouldChangeTarget) {
  810. return;
  811. }
  812. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Change code type to" message:nil preferredStyle:UIAlertControllerStyleAlert];
  813. //block代码块取代了delegate
  814. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Model Name" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  815. [self.typeButton setTitle:@"Type: Model Name" forState:UIControlStateNormal];
  816. self.type = SCANNER_TYPE_MODELNAME;
  817. }];
  818. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"UPC CODE" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  819. [self.typeButton setTitle:@"Type: UPC Code" forState:UIControlStateNormal];
  820. self.type = SCANNER_TYPE_UPCCODE;
  821. DebugLog(@"No");
  822. }];
  823. [alertControl addAction:actionOne];
  824. [alertControl addAction:alertthree];
  825. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  826. [self presentViewController:alertControl animated:YES completion:nil];
  827. }
  828. - (IBAction)TargetButtonClick:(id)sender {
  829. BOOL shouldChangeTarget = YES;
  830. #ifdef BUILD_UWAVER
  831. shouldChangeTarget = NO;
  832. #endif
  833. if (!shouldChangeTarget) {
  834. return;
  835. }
  836. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Change scanner target to" message:nil preferredStyle:UIAlertControllerStyleAlert];
  837. //block代码块取代了delegate
  838. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Model Detail" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  839. [self.targetButton setTitle:@"Target: Model Detail" forState:UIControlStateNormal];
  840. self.target = SCANNER_TARGET_DETAIL;
  841. }];
  842. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cart" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  843. [self.targetButton setTitle:@"Target: Cart" forState:UIControlStateNormal];
  844. self.target = SCANNER_TARGET_CART;
  845. DebugLog(@"No");
  846. }];
  847. [alertControl addAction:actionOne];
  848. [alertControl addAction:alertthree];
  849. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  850. [self presentViewController:alertControl animated:YES completion:nil];
  851. }
  852. - (IBAction)ScanButtonClick:(id)sender {
  853. self.scanButton.backgroundColor = [UIColor greenColor];
  854. [self.scanButton setTitle:@"Scanning" forState:UIControlStateNormal];
  855. // self.scanButton.layer.borderWidth = 1;
  856. self.handelOutput = true;
  857. // if (self.Scannerdelegate && [self.Scannerdelegate respondsToSelector:@selector(BeginScan:)]) {
  858. // [self.Scannerdelegate BeginScan:true];
  859. // }
  860. }
  861. -(void) StopScan
  862. {
  863. if(self.auto_style)
  864. return;
  865. self.scanButton.backgroundColor = [UIColor redColor];
  866. [self.scanButton setTitle:@"Tap" forState:UIControlStateNormal];
  867. }
  868. #pragma mark - Utility Methods
  869. - (void)startOverlayHideTimer
  870. {
  871. // Cancel it if we're already running
  872. if(_boxHideTimer) {
  873. [_boxHideTimer invalidate];
  874. }
  875. // Restart it to hide the overlay when it fires
  876. _boxHideTimer = [NSTimer scheduledTimerWithTimeInterval:0.2
  877. target:self
  878. selector:@selector(removeBoundingBox:)
  879. userInfo:nil
  880. repeats:NO];
  881. }
  882. - (void)removeBoundingBox:(id)sender
  883. {
  884. // Hide the box and remove the decoded text
  885. _boundingBox.hidden = YES;
  886. // _decodedMessage.text = @"";
  887. }
  888. - (NSArray *)translatePoints:(NSArray *)points fromView:(UIView *)fromView toView:(UIView *)toView
  889. {
  890. NSMutableArray *translatedPoints = [NSMutableArray new];
  891. // The points are provided in a dictionary with keys X and Y
  892. for (NSDictionary *point in points) {
  893. // Let's turn them into CGPoints
  894. CGPoint pointValue = CGPointMake([point[@"X"] floatValue], [point[@"Y"] floatValue]);
  895. // Now translate from one view to the other
  896. CGPoint translatedPoint = [fromView convertPoint:pointValue toView:toView];
  897. // Box them up and add to the array
  898. [translatedPoints addObject:[NSValue valueWithCGPoint:translatedPoint]];
  899. }
  900. return [translatedPoints copy];
  901. }
  902. @end