RQQRCodeViewController.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. // 在接口声明中添加一个布尔变量,用于跟踪是否已经处理了扫描结果
  17. @property (nonatomic, assign) BOOL isProcessingScanResult;
  18. @end
  19. @implementation RQQRCodeViewController
  20. #pragma mark - SystemMethods
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. self.isProcessingScanResult = NO;
  24. [self rq_setUp];
  25. }
  26. - (void)viewWillAppear:(BOOL)animated {
  27. [super viewWillAppear:animated];
  28. [self start];
  29. }
  30. - (void)viewWillDisappear:(BOOL)animated {
  31. [super viewWillDisappear:animated];
  32. }
  33. - (void)dealloc {
  34. [self stop];
  35. self.scanCode.delegate = nil;
  36. self.scanCode.sampleBufferDelegate = nil;
  37. self.scanCode = nil;
  38. self.scanView = nil;
  39. self.rqQRCodeScanResultBlock = nil;
  40. }
  41. - (void)closePage {
  42. [self stop];
  43. }
  44. #pragma mark - PublicMethods
  45. - (void)initBlock:(RQQRCodeScanResultBlock)block {
  46. self.rqQRCodeScanResultBlock = block;
  47. }
  48. #pragma mark - PrivateMethods
  49. - (void)rq_setUp {
  50. self.view.backgroundColor = [UIColor blackColor];
  51. [self configureUI];
  52. [self configScanCode];
  53. }
  54. - (void)configureUI {
  55. [self.view addSubview:self.scanView];
  56. [self.view addSubview:self.promptLabel];
  57. [self.view addSubview:self.closeBtn];
  58. [self.view addSubview:self.flashlightBtn];
  59. }
  60. - (void)start {
  61. [self.scanCode startRunning];
  62. [self.scanView startScanning];
  63. }
  64. - (void)stop {
  65. [self.scanCode stopRunning];
  66. [self.scanView stopScanning];
  67. }
  68. - (void)configScanCode {
  69. BOOL isCameraDeviceRearAvailable = [self.scanCode checkCameraDeviceRearAvailable];
  70. if (isCameraDeviceRearAvailable == NO) {
  71. [RQ_SHARE_FUNCTION showAlertWithTitle:@"温馨提示" message:@"请在iPhone的“设置”-“隐私”-“相机”功能中,找到“极速驾培”打开相机访问权限" alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] otherButtonStyles:nil showInWindow:NO completion:^(NSUInteger selectedOtherButtonIndex) {
  72. if (selectedOtherButtonIndex == 0) {
  73. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  74. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  75. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  76. }
  77. }
  78. }];
  79. return;
  80. }
  81. }
  82. - (void)flashlightBtn_action:(UIButton *)button {
  83. if (button.selected == NO) {
  84. button.selected = YES;
  85. [SGTorch turnOnTorch];
  86. } else {
  87. [SGTorch turnOffTorch];
  88. button.selected = NO;
  89. }
  90. }
  91. #pragma mark - SGScanCodeDelegate
  92. - (void)scanCode:(SGScanCode *)scanCode result:(NSString *)result {
  93. [self stop];
  94. // 检查是否已经在处理扫描结果
  95. if (self.isProcessingScanResult) {
  96. return; // 如果是,则退出方法,避免重复处理
  97. }
  98. // 将 isProcessingScanResult 设置为 YES,表示正在处理扫描结果
  99. self.isProcessingScanResult = YES;
  100. // 延时处理扫描结果
  101. __weak typeof(self) weakSelf = self;
  102. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  103. // 在这里执行你的扫描结果处理逻辑
  104. NSLog(@"SGScanCodeDelegate-----");
  105. [scanCode playSoundEffect:@"SGQRCode.bundle/scan_end_sound.caf"];
  106. if (result == nil) {
  107. NSLog(@"暂未识别出二维码");
  108. if (weakSelf.rqQRCodeScanResultBlock) {
  109. weakSelf.rqQRCodeScanResultBlock(@"暂未识别出二维码", NO);
  110. }
  111. } else {
  112. if (weakSelf.rqQRCodeScanResultBlock) {
  113. [weakSelf dismissViewControllerAnimated:YES completion:^{
  114. weakSelf.rqQRCodeScanResultBlock(result, YES);
  115. }];
  116. }
  117. }
  118. // 处理完毕后将 isProcessingScanResult 设置回 NO,以便下一次扫描结果的处理
  119. weakSelf.isProcessingScanResult = NO;
  120. });
  121. }
  122. #pragma mark - SGScanCodeSampleBufferDelegate
  123. - (void)scanCode:(SGScanCode *)scanCode brightness:(CGFloat)brightness {
  124. // if (brightness < - 1.5) {
  125. // self.flashlightBtn.hidden = NO;
  126. // } else {
  127. // self.flashlightBtn.hidden = YES;
  128. // [SGTorch turnOffTorch];
  129. // self.flashlightBtn.selected = NO;
  130. // }
  131. }
  132. #pragma mark - LazyLoad
  133. - (SGScanCode *)scanCode {
  134. if (!_scanCode) {
  135. _scanCode = [SGScanCode scanCode];
  136. // 预览视图,必须设置
  137. // __weak typeof(self) weakSelf = self;
  138. _scanCode.preview = self.view;
  139. _scanCode.delegate = self;
  140. _scanCode.sampleBufferDelegate = self;
  141. }
  142. return _scanCode;
  143. }
  144. - (SGScanView *)scanView {
  145. if (!_scanView) {
  146. SGScanViewConfigure *configure = [[SGScanViewConfigure alloc] init];
  147. configure.isShowBorder = YES;
  148. configure.borderColor = [UIColor clearColor];
  149. configure.cornerColor = [UIColor whiteColor];
  150. configure.cornerWidth = 3;
  151. configure.cornerLength = 15;
  152. configure.isFromTop = YES;
  153. configure.scanline = @"SGQRCode.bundle/scan_scanline_qq";
  154. configure.color = [UIColor clearColor];
  155. CGFloat x = 0;
  156. CGFloat y = 0;
  157. CGFloat w = self.view.frame.size.width;
  158. CGFloat h = self.view.frame.size.height;
  159. _scanView = [[SGScanView alloc] initWithFrame:CGRectMake(x, y, w, h) configure:configure];
  160. [_scanView startScanning];
  161. _scanView.scanFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
  162. }
  163. return _scanView;
  164. }
  165. - (UILabel *)promptLabel {
  166. if (!_promptLabel) {
  167. _promptLabel = [[UILabel alloc] init];
  168. _promptLabel.backgroundColor = [UIColor clearColor];
  169. CGFloat promptLabelX = 0;
  170. CGFloat promptLabelY = 0.73 * self.view.frame.size.height;
  171. CGFloat promptLabelW = self.view.frame.size.width;
  172. CGFloat promptLabelH = 25;
  173. _promptLabel.frame = CGRectMake(promptLabelX, promptLabelY, promptLabelW, promptLabelH);
  174. _promptLabel.textAlignment = NSTextAlignmentCenter;
  175. _promptLabel.font = [UIFont boldSystemFontOfSize:13.0];
  176. _promptLabel.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.6];
  177. _promptLabel.text = @"将二维码/条码放入框内, 即可自动扫描";
  178. }
  179. return _promptLabel;
  180. }
  181. - (UIButton *)closeBtn {
  182. if (!_closeBtn) {
  183. _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  184. float height = 0.039 * RQ_SCREEN_HEIGHT;
  185. _closeBtn.frame = CGRectMake(RQ_SCREEN_WIDTH - 16 - height, kNavBarHeight, height, height);
  186. [_closeBtn setImage:[UIImage imageNamed:@"closewhite"] forState:UIControlStateNormal];
  187. __weak typeof(self) weakSelf = self;
  188. [_closeBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) {
  189. [weakSelf dismissViewControllerAnimated:YES completion:nil];
  190. }];
  191. }
  192. return _closeBtn;
  193. }
  194. - (UIButton *)flashlightBtn {
  195. if (!_flashlightBtn) {
  196. // 添加闪光灯按钮
  197. _flashlightBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
  198. CGFloat flashlightBtnW = 50;
  199. CGFloat flashlightBtnH = 70;
  200. CGFloat flashlightBtnX = 0.5 * (self.view.frame.size.width - flashlightBtnW);
  201. CGFloat flashlightBtnY = 0.80 * self.view.frame.size.height;
  202. _flashlightBtn.frame = CGRectMake(flashlightBtnX, flashlightBtnY, flashlightBtnW, flashlightBtnH);
  203. [_flashlightBtn setImage:[UIImage imageNamed:@"wc_scan_torch"] withTitle:@"轻触照亮" textColor:UIColorWhite Font:15 fotState:UIControlStateNormal];
  204. [_flashlightBtn setImage:[UIImage imageNamed:@"wc_scan_torch_hl"] withTitle:@"轻触关闭" textColor:UIColorWhite Font:15 fotState:UIControlStateSelected];
  205. [_flashlightBtn addTarget:self action:@selector(flashlightBtn_action:) forControlEvents:UIControlEventTouchUpInside];
  206. }
  207. return _flashlightBtn;
  208. }
  209. @end