// // RQQRCodeViewController.m // jiaPei // // Created by 张嵘 on 2021/1/21. // Copyright © 2021 JCZ. All rights reserved. // #import "RQQRCodeViewController.h" @interface RQQRCodeViewController () @property (nonatomic, readwrite, strong) SGScanCode *scanCode; @property (nonatomic, readwrite, strong) SGScanView *scanView; @property (nonatomic, readwrite, strong) UILabel *promptLabel; @property (nonatomic, readwrite, strong) UIButton *closeBtn; @property (nonatomic, readwrite, strong) UIButton *flashlightBtn; @property (nonatomic, readwrite, copy) RQQRCodeScanResultBlock rqQRCodeScanResultBlock; // 在接口声明中添加一个布尔变量,用于跟踪是否已经处理了扫描结果 @property (nonatomic, assign) BOOL isProcessingScanResult; @end @implementation RQQRCodeViewController #pragma mark - SystemMethods - (void)viewDidLoad { [super viewDidLoad]; self.isProcessingScanResult = NO; [self rq_setUp]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self start]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } - (void)dealloc { [self stop]; } - (void)closePage { [self stop]; } #pragma mark - PublicMethods - (void)initBlock:(RQQRCodeScanResultBlock)block { self.rqQRCodeScanResultBlock = block; } #pragma mark - PrivateMethods - (void)rq_setUp { self.view.backgroundColor = [UIColor blackColor]; [self configureUI]; [self configScanCode]; } - (void)configureUI { [self.view addSubview:self.scanView]; [self.view addSubview:self.promptLabel]; [self.view addSubview:self.closeBtn]; [self.view addSubview:self.flashlightBtn]; } - (void)start { [self.scanCode startRunning]; [self.scanView startScanning]; } - (void)stop { [self.scanCode stopRunning]; [self.scanView stopScanning]; self.scanCode.delegate = nil; self.scanCode.sampleBufferDelegate = nil; self.scanCode = nil; self.scanView = nil; self.rqQRCodeScanResultBlock = nil; } - (void)configScanCode { BOOL isCameraDeviceRearAvailable = [self.scanCode checkCameraDeviceRearAvailable]; if (isCameraDeviceRearAvailable == NO) { [RQ_SHARE_FUNCTION showAlertWithTitle:@"温馨提示" message:@"请在iPhone的“设置”-“隐私”-“相机”功能中,找到“极速驾培”打开相机访问权限" alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] otherButtonStyles:nil showInWindow:NO completion:^(NSUInteger selectedOtherButtonIndex) { if (selectedOtherButtonIndex == 0) { NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([[UIApplication sharedApplication] canOpenURL:url]) { [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil]; } } }]; return; } } - (void)flashlightBtn_action:(UIButton *)button { if (button.selected == NO) { button.selected = YES; [SGTorch turnOnTorch]; } else { [SGTorch turnOffTorch]; button.selected = NO; } } #pragma mark - SGScanCodeDelegate - (void)scanCode:(SGScanCode *)scanCode result:(NSString *)result { [self stop]; // 检查是否已经在处理扫描结果 if (self.isProcessingScanResult) { return; // 如果是,则退出方法,避免重复处理 } // 将 isProcessingScanResult 设置为 YES,表示正在处理扫描结果 self.isProcessingScanResult = YES; // 延时处理扫描结果 __weak typeof(self) weakSelf = self; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // 在这里执行你的扫描结果处理逻辑 NSLog(@"SGScanCodeDelegate-----"); [scanCode playSoundEffect:@"SGQRCode.bundle/scan_end_sound.caf"]; if (result == nil) { NSLog(@"暂未识别出二维码"); if (weakSelf.rqQRCodeScanResultBlock) { weakSelf.rqQRCodeScanResultBlock(@"暂未识别出二维码", NO); } } else { if (weakSelf.rqQRCodeScanResultBlock) { [weakSelf dismissViewControllerAnimated:YES completion:^{ weakSelf.rqQRCodeScanResultBlock(result, YES); }]; } } // 处理完毕后将 isProcessingScanResult 设置回 NO,以便下一次扫描结果的处理 weakSelf.isProcessingScanResult = NO; }); } #pragma mark - SGScanCodeSampleBufferDelegate - (void)scanCode:(SGScanCode *)scanCode brightness:(CGFloat)brightness { // if (brightness < - 1.5) { // self.flashlightBtn.hidden = NO; // } else { // self.flashlightBtn.hidden = YES; // [SGTorch turnOffTorch]; // self.flashlightBtn.selected = NO; // } } #pragma mark - LazyLoad - (SGScanCode *)scanCode { if (!_scanCode) { _scanCode = [SGScanCode scanCode]; // 预览视图,必须设置 __weak typeof(self) weakSelf = self; _scanCode.preview = weakSelf.view; _scanCode.delegate = weakSelf; _scanCode.sampleBufferDelegate = weakSelf; } return _scanCode; } - (SGScanView *)scanView { if (!_scanView) { SGScanViewConfigure *configure = [[SGScanViewConfigure alloc] init]; configure.isShowBorder = YES; configure.borderColor = [UIColor clearColor]; configure.cornerColor = [UIColor whiteColor]; configure.cornerWidth = 3; configure.cornerLength = 15; configure.isFromTop = YES; configure.scanline = @"SGQRCode.bundle/scan_scanline_qq"; configure.color = [UIColor clearColor]; CGFloat x = 0; CGFloat y = 0; CGFloat w = self.view.frame.size.width; CGFloat h = self.view.frame.size.height; _scanView = [[SGScanView alloc] initWithFrame:CGRectMake(x, y, w, h) configure:configure]; [_scanView startScanning]; _scanView.scanFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); } return _scanView; } - (UILabel *)promptLabel { if (!_promptLabel) { _promptLabel = [[UILabel alloc] init]; _promptLabel.backgroundColor = [UIColor clearColor]; CGFloat promptLabelX = 0; CGFloat promptLabelY = 0.73 * self.view.frame.size.height; CGFloat promptLabelW = self.view.frame.size.width; CGFloat promptLabelH = 25; _promptLabel.frame = CGRectMake(promptLabelX, promptLabelY, promptLabelW, promptLabelH); _promptLabel.textAlignment = NSTextAlignmentCenter; _promptLabel.font = [UIFont boldSystemFontOfSize:13.0]; _promptLabel.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.6]; _promptLabel.text = @"将二维码/条码放入框内, 即可自动扫描"; } return _promptLabel; } - (UIButton *)closeBtn { if (!_closeBtn) { _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom]; float height = 0.039 * RQ_SCREEN_HEIGHT; _closeBtn.frame = CGRectMake(RQ_SCREEN_WIDTH - 16 - height, kNavBarHeight, height, height); [_closeBtn setImage:[UIImage imageNamed:@"closewhite"] forState:UIControlStateNormal]; __weak typeof(self) weakSelf = self; [_closeBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) { [weakSelf dismissViewControllerAnimated:YES completion:nil]; }]; } return _closeBtn; } - (UIButton *)flashlightBtn { if (!_flashlightBtn) { // 添加闪光灯按钮 _flashlightBtn = [UIButton buttonWithType:(UIButtonTypeCustom)]; CGFloat flashlightBtnW = 50; CGFloat flashlightBtnH = 70; CGFloat flashlightBtnX = 0.5 * (self.view.frame.size.width - flashlightBtnW); CGFloat flashlightBtnY = 0.80 * self.view.frame.size.height; _flashlightBtn.frame = CGRectMake(flashlightBtnX, flashlightBtnY, flashlightBtnW, flashlightBtnH); [_flashlightBtn setImage:[UIImage imageNamed:@"wc_scan_torch"] withTitle:@"轻触照亮" textColor:UIColorWhite Font:15 fotState:UIControlStateNormal]; [_flashlightBtn setImage:[UIImage imageNamed:@"wc_scan_torch_hl"] withTitle:@"轻触关闭" textColor:UIColorWhite Font:15 fotState:UIControlStateSelected]; [_flashlightBtn addTarget:self action:@selector(flashlightBtn_action:) forControlEvents:UIControlEventTouchUpInside]; } return _flashlightBtn; } @end