123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- //
- // RQQRCodeViewController.m
- // jiaPei
- //
- // Created by 张嵘 on 2021/1/21.
- // Copyright © 2021 JCZ. All rights reserved.
- //
- #import "RQQRCodeViewController.h"
- @interface RQQRCodeViewController () <SGScanCodeDelegate, SGScanCodeSampleBufferDelegate>
- @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;
- @end
- @implementation RQQRCodeViewController
- #pragma mark - SystemMethods
- - (void)viewDidLoad {
- [super viewDidLoad];
- [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];
- self.scanView = nil;
- self.scanCode.delegate = nil;
- self.scanCode.sampleBufferDelegate = nil;
- self.scanCode = nil;
- self.rqQRCodeScanResultBlock = nil;
- }
- #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];
- }
- - (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];
- [scanCode playSoundEffect:@"SGQRCode.bundle/scan_end_sound.caf"];
-
- if (result == nil) {
- NSLog(@"暂未识别出二维码");
- if (self.rqQRCodeScanResultBlock) {
- self.rqQRCodeScanResultBlock(@"暂未识别出二维码", NO);
- }
- } else {
- if (self.rqQRCodeScanResultBlock) {
- __weak typeof(self) weakSelf = self;
- [self dismissViewControllerAnimated:YES completion:^{
- weakSelf.rqQRCodeScanResultBlock(result, YES);
- }];
- }
- }
- }
- #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 = self.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
|