RQQRCodeViewController.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // RQQRCodeViewController.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2021/1/21.
  6. // Copyright © 2021 JCZ. All rights reserved.
  7. //
  8. #import "RQQRCodeViewController.h"
  9. @interface RQQRCodeViewController () <SGScanCodeDelegate, SGScanCodeSampleBufferDelegate>
  10. @property (nonatomic, readwrite, strong) SGScanCode *scanCode;
  11. @property (nonatomic, readwrite, strong) SGScanView *scanView;
  12. @property (nonatomic, readwrite, strong) UILabel *promptLabel;
  13. @property (nonatomic, readwrite, strong) UIButton *closeBtn;
  14. @property (nonatomic, readwrite, strong) UIButton *flashlightBtn;
  15. @property (nonatomic, readwrite, copy) RQQRCodeScanResultBlock rqQRCodeScanResultBlock;
  16. @end
  17. @implementation RQQRCodeViewController
  18. #pragma mark - SystemMethods
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. [self rq_setUp];
  22. }
  23. - (void)viewWillAppear:(BOOL)animated {
  24. [super viewWillAppear:animated];
  25. [self start];
  26. }
  27. - (void)viewWillDisappear:(BOOL)animated {
  28. [super viewWillDisappear:animated];
  29. }
  30. - (void)dealloc {
  31. [self stop];
  32. }
  33. - (void)closePage {
  34. [self stop];
  35. self.scanView = nil;
  36. self.scanCode.delegate = nil;
  37. self.scanCode.sampleBufferDelegate = nil;
  38. self.scanCode = nil;
  39. self.rqQRCodeScanResultBlock = nil;
  40. }
  41. #pragma mark - PublicMethods
  42. - (void)initBlock:(RQQRCodeScanResultBlock)block {
  43. self.rqQRCodeScanResultBlock = block;
  44. }
  45. #pragma mark - PrivateMethods
  46. - (void)rq_setUp {
  47. self.view.backgroundColor = [UIColor blackColor];
  48. [self configureUI];
  49. [self configScanCode];
  50. }
  51. - (void)configureUI {
  52. [self.view addSubview:self.scanView];
  53. [self.view addSubview:self.promptLabel];
  54. [self.view addSubview:self.closeBtn];
  55. [self.view addSubview:self.flashlightBtn];
  56. }
  57. - (void)start {
  58. [self.scanCode startRunning];
  59. [self.scanView startScanning];
  60. }
  61. - (void)stop {
  62. [self.scanCode stopRunning];
  63. [self.scanView stopScanning];
  64. }
  65. - (void)configScanCode {
  66. BOOL isCameraDeviceRearAvailable = [self.scanCode checkCameraDeviceRearAvailable];
  67. if (isCameraDeviceRearAvailable == NO) {
  68. [RQ_SHARE_FUNCTION showAlertWithTitle:@"温馨提示" message:@"请在iPhone的“设置”-“隐私”-“相机”功能中,找到“极速驾培”打开相机访问权限" alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] otherButtonStyles:nil showInWindow:NO completion:^(NSUInteger selectedOtherButtonIndex) {
  69. if (selectedOtherButtonIndex == 0) {
  70. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  71. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  72. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  73. }
  74. }
  75. }];
  76. return;
  77. }
  78. }
  79. - (void)flashlightBtn_action:(UIButton *)button {
  80. if (button.selected == NO) {
  81. button.selected = YES;
  82. [SGTorch turnOnTorch];
  83. } else {
  84. [SGTorch turnOffTorch];
  85. button.selected = NO;
  86. }
  87. }
  88. #pragma mark - SGScanCodeDelegate
  89. - (void)scanCode:(SGScanCode *)scanCode result:(NSString *)result {
  90. [self stop];
  91. [scanCode playSoundEffect:@"SGQRCode.bundle/scan_end_sound.caf"];
  92. if (result == nil) {
  93. NSLog(@"暂未识别出二维码");
  94. if (self.rqQRCodeScanResultBlock) {
  95. self.rqQRCodeScanResultBlock(@"暂未识别出二维码", NO);
  96. }
  97. } else {
  98. if (self.rqQRCodeScanResultBlock) {
  99. __weak typeof(self) weakSelf = self;
  100. [self dismissViewControllerAnimated:YES completion:^{
  101. weakSelf.rqQRCodeScanResultBlock(result, YES);
  102. }];
  103. }
  104. }
  105. }
  106. #pragma mark - SGScanCodeSampleBufferDelegate
  107. - (void)scanCode:(SGScanCode *)scanCode brightness:(CGFloat)brightness {
  108. // if (brightness < - 1.5) {
  109. // self.flashlightBtn.hidden = NO;
  110. // } else {
  111. // self.flashlightBtn.hidden = YES;
  112. // [SGTorch turnOffTorch];
  113. // self.flashlightBtn.selected = NO;
  114. // }
  115. }
  116. #pragma mark - LazyLoad
  117. - (SGScanCode *)scanCode {
  118. if (!_scanCode) {
  119. _scanCode = [SGScanCode scanCode];
  120. // 预览视图,必须设置
  121. __weak typeof(self) weakSelf = self;
  122. _scanCode.preview = self.view;
  123. _scanCode.delegate = weakSelf;
  124. _scanCode.sampleBufferDelegate = weakSelf;
  125. }
  126. return _scanCode;
  127. }
  128. - (SGScanView *)scanView {
  129. if (!_scanView) {
  130. SGScanViewConfigure *configure = [[SGScanViewConfigure alloc] init];
  131. configure.isShowBorder = YES;
  132. configure.borderColor = [UIColor clearColor];
  133. configure.cornerColor = [UIColor whiteColor];
  134. configure.cornerWidth = 3;
  135. configure.cornerLength = 15;
  136. configure.isFromTop = YES;
  137. configure.scanline = @"SGQRCode.bundle/scan_scanline_qq";
  138. configure.color = [UIColor clearColor];
  139. CGFloat x = 0;
  140. CGFloat y = 0;
  141. CGFloat w = self.view.frame.size.width;
  142. CGFloat h = self.view.frame.size.height;
  143. _scanView = [[SGScanView alloc] initWithFrame:CGRectMake(x, y, w, h) configure:configure];
  144. [_scanView startScanning];
  145. _scanView.scanFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
  146. }
  147. return _scanView;
  148. }
  149. - (UILabel *)promptLabel {
  150. if (!_promptLabel) {
  151. _promptLabel = [[UILabel alloc] init];
  152. _promptLabel.backgroundColor = [UIColor clearColor];
  153. CGFloat promptLabelX = 0;
  154. CGFloat promptLabelY = 0.73 * self.view.frame.size.height;
  155. CGFloat promptLabelW = self.view.frame.size.width;
  156. CGFloat promptLabelH = 25;
  157. _promptLabel.frame = CGRectMake(promptLabelX, promptLabelY, promptLabelW, promptLabelH);
  158. _promptLabel.textAlignment = NSTextAlignmentCenter;
  159. _promptLabel.font = [UIFont boldSystemFontOfSize:13.0];
  160. _promptLabel.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.6];
  161. _promptLabel.text = @"将二维码/条码放入框内, 即可自动扫描";
  162. }
  163. return _promptLabel;
  164. }
  165. - (UIButton *)closeBtn {
  166. if (!_closeBtn) {
  167. _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  168. float height = 0.039 * RQ_SCREEN_HEIGHT;
  169. _closeBtn.frame = CGRectMake(RQ_SCREEN_WIDTH - 16 - height, kNavBarHeight, height, height);
  170. [_closeBtn setImage:[UIImage imageNamed:@"closewhite"] forState:UIControlStateNormal];
  171. __weak typeof(self) weakSelf = self;
  172. [_closeBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) {
  173. [weakSelf dismissViewControllerAnimated:YES completion:nil];
  174. }];
  175. }
  176. return _closeBtn;
  177. }
  178. - (UIButton *)flashlightBtn {
  179. if (!_flashlightBtn) {
  180. // 添加闪光灯按钮
  181. _flashlightBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
  182. CGFloat flashlightBtnW = 50;
  183. CGFloat flashlightBtnH = 70;
  184. CGFloat flashlightBtnX = 0.5 * (self.view.frame.size.width - flashlightBtnW);
  185. CGFloat flashlightBtnY = 0.80 * self.view.frame.size.height;
  186. _flashlightBtn.frame = CGRectMake(flashlightBtnX, flashlightBtnY, flashlightBtnW, flashlightBtnH);
  187. [_flashlightBtn setImage:[UIImage imageNamed:@"wc_scan_torch"] withTitle:@"轻触照亮" textColor:UIColorWhite Font:15 fotState:UIControlStateNormal];
  188. [_flashlightBtn setImage:[UIImage imageNamed:@"wc_scan_torch_hl"] withTitle:@"轻触关闭" textColor:UIColorWhite Font:15 fotState:UIControlStateSelected];
  189. [_flashlightBtn addTarget:self action:@selector(flashlightBtn_action:) forControlEvents:UIControlEventTouchUpInside];
  190. }
  191. return _flashlightBtn;
  192. }
  193. @end