口袋优选

SubLBXScanViewController.m 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //
  2. //
  3. //
  4. //
  5. // Created by lbxia on 15/10/21.
  6. // Copyright © 2015年 lbxia. All rights reserved.
  7. //
  8. #import "SubLBXScanViewController.h"
  9. #import "LBXScanResult.h"
  10. #import "LBXScanWrapper.h"
  11. #import "MyQRViewController.h"
  12. #import "ScanResultViewController.h"
  13. @interface SubLBXScanViewController ()
  14. @end
  15. @implementation SubLBXScanViewController
  16. - (void)dealloc {
  17. }
  18. - (void)viewWillAppear:(BOOL)animated{
  19. self.tabBarController.tabBar.hidden = YES;
  20. [super viewWillAppear:animated];
  21. }
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. self.view.backgroundColor = [UIColor whiteColor];
  25. self.title = @"扫描二维码";
  26. LBXScanViewStyle *style = [[LBXScanViewStyle alloc]init];
  27. style.anmiationStyle = LBXScanViewAnimationStyle_LineMove;
  28. style.animationImage = [UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_light_green"];
  29. self.style = style;
  30. if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
  31. // self.edgesForExtendedLayout = UIRectEdgeNone;
  32. }
  33. self.view.backgroundColor = [UIColor blackColor];
  34. }
  35. - (void)viewDidAppear:(BOOL)animated
  36. {
  37. [super viewDidAppear:animated];
  38. [self drawBottomItems];
  39. [self drawTitle];
  40. [self.view bringSubviewToFront:_topTitle];
  41. }
  42. - (void)drawTitle
  43. {
  44. if (!_topTitle)
  45. {
  46. self.topTitle = [[UILabel alloc]init];
  47. _topTitle.bounds = CGRectMake(0, 0, 145, 60);
  48. _topTitle.center = CGPointMake(CGRectGetWidth(self.view.frame)/2, 50);
  49. //3.5inch iphone
  50. if ([UIScreen mainScreen].bounds.size.height <= 568 )
  51. {
  52. _topTitle.center = CGPointMake(CGRectGetWidth(self.view.frame)/2, 38);
  53. _topTitle.font = [UIFont systemFontOfSize:14];
  54. }
  55. _topTitle.textAlignment = NSTextAlignmentCenter;
  56. _topTitle.numberOfLines = 0;
  57. _topTitle.text = @"扫描二维码";
  58. _topTitle.textColor = [UIColor whiteColor];
  59. [self.view addSubview:_topTitle];
  60. }
  61. UIButton *closeButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 25, 30, 30)];
  62. [closeButton setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
  63. [closeButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
  64. [self.view addSubview:closeButton];
  65. }
  66. - (void)closeAction {
  67. [self dismissViewControllerAnimated:YES completion:nil];
  68. }
  69. - (void)drawBottomItems
  70. {
  71. if (_bottomItemsView) {
  72. return;
  73. }
  74. self.bottomItemsView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.view.frame)-164,CGRectGetWidth(self.view.frame), 100)];
  75. _bottomItemsView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
  76. [self.view addSubview:_bottomItemsView];
  77. CGSize size = CGSizeMake(65, 87);
  78. self.btnFlash = [[UIButton alloc]init];
  79. _btnFlash.bounds = CGRectMake(0, 0, size.width, size.height);
  80. _btnFlash.center = CGPointMake(CGRectGetWidth(_bottomItemsView.frame)/2, CGRectGetHeight(_bottomItemsView.frame)/2);
  81. [_btnFlash setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_flash_nor"] forState:UIControlStateNormal];
  82. [_btnFlash addTarget:self action:@selector(openOrCloseFlash) forControlEvents:UIControlEventTouchUpInside];
  83. self.btnPhoto = [[UIButton alloc]init];
  84. _btnPhoto.bounds = _btnFlash.bounds;
  85. _btnPhoto.center = CGPointMake(CGRectGetWidth(_bottomItemsView.frame)/4, CGRectGetHeight(_bottomItemsView.frame)/2);
  86. [_btnPhoto setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_photo_nor"] forState:UIControlStateNormal];
  87. [_btnPhoto setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_photo_down"] forState:UIControlStateHighlighted];
  88. [_btnPhoto addTarget:self action:@selector(openPhoto) forControlEvents:UIControlEventTouchUpInside];
  89. self.btnMyQR = [[UIButton alloc]init];
  90. _btnMyQR.bounds = _btnFlash.bounds;
  91. _btnMyQR.center = CGPointMake(CGRectGetWidth(_bottomItemsView.frame) * 3/4, CGRectGetHeight(_bottomItemsView.frame)/2);
  92. [_btnMyQR setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_myqrcode_nor"] forState:UIControlStateNormal];
  93. [_btnMyQR setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_myqrcode_down"] forState:UIControlStateHighlighted];
  94. [_btnMyQR addTarget:self action:@selector(myQRCode) forControlEvents:UIControlEventTouchUpInside];
  95. [_bottomItemsView addSubview:_btnFlash];
  96. [_bottomItemsView addSubview:_btnPhoto];
  97. // [_bottomItemsView addSubview:_btnMyQR];
  98. }
  99. - (void)scanResultWithArray:(NSArray<LBXScanResult*>*)array
  100. {
  101. if (array.count < 1)
  102. {
  103. [self popAlertMsgWithScanResult:nil];
  104. return;
  105. }
  106. //经测试,可以同时识别2个二维码,不能同时识别二维码和条形码
  107. for (LBXScanResult *result in array) {
  108. NSLog(@"scanResult:%@",result.strScanned);
  109. }
  110. LBXScanResult *scanResult = array[0];
  111. NSString*strResult = scanResult.strScanned;
  112. self.scanImage = scanResult.imgScanned;
  113. if (!strResult) {
  114. [self popAlertMsgWithScanResult:nil];
  115. return;
  116. }
  117. //震动提醒
  118. [LBXScanWrapper systemVibrate];
  119. //声音提醒
  120. [LBXScanWrapper systemSound];
  121. [self showNextVCWithScanResult:scanResult];
  122. }
  123. - (void)popAlertMsgWithScanResult:(NSString*)strResult
  124. {
  125. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"识别失败" preferredStyle:UIAlertControllerStyleAlert];
  126. __weak __typeof(self) weakSelf = self;
  127. UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  128. [weakSelf reStartDevice];
  129. }];
  130. [alert addAction:defaultAction];
  131. [self presentViewController:alert animated:YES completion:nil];
  132. }
  133. - (void)showNextVCWithScanResult:(LBXScanResult*)strResult
  134. {
  135. }
  136. #pragma mark -底部功能项
  137. //打开相册
  138. - (void)openPhoto
  139. {
  140. if ([LBXScanWrapper isGetPhotoPermission])
  141. [self openLocalPhoto];
  142. else{
  143. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请到设置->隐私中开启本程序相册权限" preferredStyle:UIAlertControllerStyleAlert];
  144. UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  145. }];
  146. [alert addAction:defaultAction];
  147. [self presentViewController:alert animated:YES completion:nil];
  148. }
  149. }
  150. //开关闪光灯
  151. - (void)openOrCloseFlash
  152. {
  153. [super openOrCloseFlash];
  154. if (self.isOpenFlash)
  155. {
  156. [_btnFlash setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_flash_down"] forState:UIControlStateNormal];
  157. }
  158. else
  159. [_btnFlash setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_flash_nor"] forState:UIControlStateNormal];
  160. }
  161. #pragma mark -底部功能项
  162. - (void)myQRCode
  163. {
  164. MyQRViewController *vc = [MyQRViewController new];
  165. [self.navigationController pushViewController:vc animated:YES];
  166. }
  167. #pragma mark - 错误提示
  168. - (void)showError:(NSString *)str {
  169. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
  170. UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  171. }];
  172. [alert addAction:defaultAction];
  173. [self presentViewController:alert animated:YES completion:nil];
  174. }
  175. @end