QQLBXScanViewController.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. //
  3. //
  4. //
  5. // Created by lbxia on 15/10/21.
  6. // Copyright © 2015年 lbxia. All rights reserved.
  7. //
  8. #import "QQLBXScanViewController.h"
  9. #import "CreateBarCodeViewController.h"
  10. #import "LBXScanVideoZoomView.h"
  11. #import "LBXPermission.h"
  12. #import "LBXPermissionSetting.h"
  13. #import <WFChatClient/WFCChatClient.h>
  14. #import <WFChatUIKit/WFChatUIKit.h>
  15. #import "LBXAlertAction.h"
  16. #import "LBXScanTypes.h"
  17. #import "LBXScanNative.h"
  18. @interface QQLBXScanViewController ()
  19. @property (nonatomic, strong) LBXScanVideoZoomView *zoomView;
  20. @end
  21. @implementation QQLBXScanViewController
  22. - (void)viewDidLoad
  23. {
  24. [super viewDidLoad];
  25. // Do any additional setup after loading the view.
  26. if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
  27. self.edgesForExtendedLayout = UIRectEdgeNone;
  28. }
  29. self.view.backgroundColor = [UIColor blackColor];
  30. //设置扫码后需要扫码图像
  31. self.isNeedScanImage = YES;
  32. }
  33. - (void)viewDidAppear:(BOOL)animated
  34. {
  35. [super viewDidAppear:animated];
  36. [self drawBottomItems];
  37. [self drawTitle];
  38. [self.view bringSubviewToFront:_topTitle];
  39. }
  40. //绘制扫描区域
  41. - (void)drawTitle
  42. {
  43. if (!_topTitle)
  44. {
  45. self.topTitle = [[UILabel alloc]init];
  46. _topTitle.bounds = CGRectMake(0, 0, 145, 60);
  47. _topTitle.center = CGPointMake(CGRectGetWidth(self.view.frame)/2, 50);
  48. //3.5inch iphone
  49. if ([UIScreen mainScreen].bounds.size.height <= 568 )
  50. {
  51. _topTitle.center = CGPointMake(CGRectGetWidth(self.view.frame)/2, 38);
  52. _topTitle.font = [UIFont systemFontOfSize:14];
  53. }
  54. _topTitle.textAlignment = NSTextAlignmentCenter;
  55. _topTitle.numberOfLines = 0;
  56. _topTitle.text = WFCString(@"TargetToScanQRCode");
  57. _topTitle.textColor = [UIColor whiteColor];
  58. [self.view addSubview:_topTitle];
  59. }
  60. }
  61. - (void)cameraInitOver
  62. {
  63. if (self.isVideoZoom) {
  64. [self zoomView];
  65. }
  66. }
  67. - (LBXScanVideoZoomView*)zoomView
  68. {
  69. if (!_zoomView)
  70. {
  71. CGRect frame = self.view.frame;
  72. int XRetangleLeft = self.style.xScanRetangleOffset;
  73. CGSize sizeRetangle = CGSizeMake(frame.size.width - XRetangleLeft*2, frame.size.width - XRetangleLeft*2);
  74. if (self.style.whRatio != 1)
  75. {
  76. CGFloat w = sizeRetangle.width;
  77. CGFloat h = w / self.style.whRatio;
  78. NSInteger hInt = (NSInteger)h;
  79. h = hInt;
  80. sizeRetangle = CGSizeMake(w, h);
  81. }
  82. CGFloat videoMaxScale = [self.scanObj getVideoMaxScale];
  83. //扫码区域Y轴最小坐标
  84. CGFloat YMinRetangle = frame.size.height / 2.0 - sizeRetangle.height/2.0 - self.style.centerUpOffset;
  85. CGFloat YMaxRetangle = YMinRetangle + sizeRetangle.height;
  86. CGFloat zoomw = sizeRetangle.width + 40;
  87. _zoomView = [[LBXScanVideoZoomView alloc]initWithFrame:CGRectMake((CGRectGetWidth(self.view.frame)-zoomw)/2, YMaxRetangle + 40, zoomw, 18)];
  88. [_zoomView setMaximunValue:videoMaxScale/4];
  89. __weak __typeof(self) weakSelf = self;
  90. _zoomView.block= ^(float value)
  91. {
  92. [weakSelf.scanObj setVideoScale:value];
  93. };
  94. [self.view addSubview:_zoomView];
  95. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
  96. [self.view addGestureRecognizer:tap];
  97. }
  98. return _zoomView;
  99. }
  100. - (void)tap
  101. {
  102. _zoomView.hidden = !_zoomView.hidden;
  103. }
  104. - (void)drawBottomItems
  105. {
  106. if (_bottomItemsView) {
  107. return;
  108. }
  109. self.bottomItemsView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.view.bounds)-100 - [WFCUUtilities wf_safeDistanceBottom],
  110. CGRectGetWidth(self.view.frame), 100)];
  111. _bottomItemsView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
  112. [self.view addSubview:_bottomItemsView];
  113. CGSize size = CGSizeMake(65, 87);
  114. self.btnFlash = [[UIButton alloc]init];
  115. _btnFlash.bounds = CGRectMake(0, 0, size.width, size.height);
  116. _btnFlash.center = CGPointMake(CGRectGetWidth(_bottomItemsView.frame)/2, CGRectGetHeight(_bottomItemsView.frame)/2);
  117. [_btnFlash setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_flash_nor"] forState:UIControlStateNormal];
  118. [_btnFlash addTarget:self action:@selector(openOrCloseFlash) forControlEvents:UIControlEventTouchUpInside];
  119. self.btnPhoto = [[UIButton alloc]init];
  120. _btnPhoto.bounds = _btnFlash.bounds;
  121. _btnPhoto.center = CGPointMake(CGRectGetWidth(_bottomItemsView.frame)/4, CGRectGetHeight(_bottomItemsView.frame)/2);
  122. [_btnPhoto setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_photo_nor"] forState:UIControlStateNormal];
  123. [_btnPhoto setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_photo_down"] forState:UIControlStateHighlighted];
  124. [_btnPhoto addTarget:self action:@selector(openPhoto) forControlEvents:UIControlEventTouchUpInside];
  125. self.btnMyQR = [[UIButton alloc]init];
  126. _btnMyQR.bounds = _btnFlash.bounds;
  127. _btnMyQR.center = CGPointMake(CGRectGetWidth(_bottomItemsView.frame) * 3/4, CGRectGetHeight(_bottomItemsView.frame)/2);
  128. [_btnMyQR setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_myqrcode_nor"] forState:UIControlStateNormal];
  129. [_btnMyQR setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_myqrcode_down"] forState:UIControlStateHighlighted];
  130. [_btnMyQR addTarget:self action:@selector(myQRCode) forControlEvents:UIControlEventTouchUpInside];
  131. [_bottomItemsView addSubview:_btnFlash];
  132. [_bottomItemsView addSubview:_btnPhoto];
  133. [_bottomItemsView addSubview:_btnMyQR];
  134. }
  135. - (void)showError:(NSString*)str
  136. {
  137. [LBXAlertAction showAlertWithTitle:@"提示" msg:str buttonsStatement:@[@"知道了"] chooseBlock:nil];
  138. }
  139. - (void)scanResultWithArray:(NSArray<LBXScanResult*>*)array
  140. {
  141. if (array.count < 1)
  142. {
  143. [self popAlertMsgWithScanResult:nil];
  144. return;
  145. }
  146. //经测试,可以同时识别2个二维码,不能同时识别二维码和条形码
  147. for (LBXScanResult *result in array) {
  148. NSLog(@"scanResult:%@",result.strScanned);
  149. }
  150. LBXScanResult *scanResult = array[0];
  151. NSString*strResult = scanResult.strScanned;
  152. self.scanImage = scanResult.imgScanned;
  153. if (!strResult) {
  154. [self popAlertMsgWithScanResult:nil];
  155. return;
  156. }
  157. //震动提醒
  158. // [LBXScanWrapper systemVibrate];
  159. //声音提醒
  160. //[LBXScanWrapper systemSound];
  161. [self showNextVCWithScanResult:scanResult];
  162. }
  163. - (void)popAlertMsgWithScanResult:(NSString*)strResult
  164. {
  165. if (!strResult) {
  166. strResult = @"识别失败";
  167. }
  168. __weak __typeof(self) weakSelf = self;
  169. [LBXAlertAction showAlertWithTitle:@"扫码内容" msg:strResult buttonsStatement:@[@"知道了"] chooseBlock:^(NSInteger buttonIdx) {
  170. [weakSelf reStartDevice];
  171. }];
  172. }
  173. - (void)showNextVCWithScanResult:(LBXScanResult*)strResult
  174. {
  175. [self.navigationController popViewControllerAnimated:NO];
  176. self.scanResult(strResult.strScanned);
  177. }
  178. #pragma mark -底部功能项
  179. //打开相册
  180. - (void)openPhoto
  181. {
  182. __weak __typeof(self) weakSelf = self;
  183. [LBXPermission authorizeWithType:LBXPermissionType_Photos completion:^(BOOL granted, BOOL firstTime) {
  184. if (granted) {
  185. [weakSelf openLocalPhoto:NO];
  186. }
  187. else if (!firstTime )
  188. {
  189. [LBXPermissionSetting showAlertToDislayPrivacySettingWithTitle:@"提示" msg:@"没有相册权限,是否前往设置" cancel:@"取消" setting:@"设置"];
  190. }
  191. }];
  192. }
  193. //开关闪光灯
  194. - (void)openOrCloseFlash
  195. {
  196. [super openOrCloseFlash];
  197. if (self.isOpenFlash)
  198. {
  199. [_btnFlash setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_flash_down"] forState:UIControlStateNormal];
  200. }
  201. else
  202. [_btnFlash setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_flash_nor"] forState:UIControlStateNormal];
  203. }
  204. #pragma mark -底部功能项
  205. - (void)myQRCode
  206. {
  207. CreateBarCodeViewController *vc = [CreateBarCodeViewController new];
  208. vc.qrType = QRType_User;
  209. vc.target = [WFCCNetworkService sharedInstance].userId;
  210. [self.navigationController pushViewController:vc animated:YES];
  211. }
  212. @end