美文网首页
好用的二维码库

好用的二维码库

作者: 白色天空729 | 来源:发表于2018-11-07 15:44 被阅读1次

    https://github.com/kingsic/SGQRCode

    https://pan.baidu.com/s/16Q5qsP7s4dfgvBQ8L3emdw

    #import "QRCodeCtrl.h"
    #import "MBProgressHUD+SGQRCode.h"
    #import "SGQRCode.h"
    #import "TrainingVC.h"
    #import "Singleton.h"
    
    @interface QRCodeCtrl () {
        SGQRCodeObtain *_obtain;
    }
    
    @property (nonatomic, strong) UIButton *flashlightBtn;
    @property (nonatomic, assign) BOOL isSelectedFlashlightBtn;
    @property (nonatomic, strong) SGQRCodeScanView *scanView;
    @property (nonatomic, strong) UILabel *promptLabel;
    @property (nonatomic, strong) UIView *bottomView;
    
    @end
    
    @implementation QRCodeCtrl
    
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        
        [_obtain startRunningWithBefore:nil completion:nil];
    
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.`
        
        [self setNavTitle:@"扫一扫"];
        
        /// 二维码开启方法
        self.view.backgroundColor = [UIColor blackColor];
        _obtain = [SGQRCodeObtain QRCodeObtain];
        
        /// 检查是否有相机访问权限
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            if (granted) {
                dispatch_sync(dispatch_get_main_queue(), ^{
                    /// 二维码开启方法
                    [self setupQRCodeScan];
                });
                DEBUGLOG(@"用户第一次同意了访问相机权限 - - %@", [NSThread currentThread]);
            } else {
                UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请去-> [设置 - 隐私 - 相机 - 福建省立OA] 打开访问开关" preferredStyle:(UIAlertControllerStyleAlert)];
                UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                    return;
                }];
                
                [alertC addAction:alertA];
                [self presentViewController:alertC animated:YES completion:nil];
                DEBUGLOG(@"用户第一次拒绝了访问相机权限 - - %@", [NSThread currentThread]);
            }
        }];
        
        
        [self.view addSubview:self.scanView];
        [self.view addSubview:self.promptLabel];
        /// 为了 UI 效果
        [self.view addSubview:self.bottomView];
    }
    
    - (void)setupQRCodeScan {
        __weak typeof(self) weakSelf = self;
        
        SGQRCodeObtainConfigure *configure = [SGQRCodeObtainConfigure QRCodeObtainConfigure];
        configure.sampleBufferDelegate = YES;
        [_obtain establishQRCodeObtainScanWithController:self configure:configure];
        [_obtain setBlockWithQRCodeObtainScanResult:^(SGQRCodeObtain *obtain, NSString *result) {
            
            DEBUGLOG(@"%@",result);
            
            if (result) {
                [MBProgressHUD SG_showMBProgressHUDWithModifyStyleMessage:@"正在处理..." toView:weakSelf.view];
                [obtain stopRunning];
                [obtain playSoundName:@"SGQRCode.bundle/sound.caf"];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [MBProgressHUD SG_hideHUDForView:weakSelf.view];
                    [Singleton shareInstance].qrcodeStr = result;
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                });
            }
        }];
        [_obtain setBlockWithQRCodeObtainScanBrightness:^(SGQRCodeObtain *obtain, CGFloat brightness) {
            if (brightness < - 1) {
                [weakSelf.view addSubview:weakSelf.flashlightBtn];
            } else {
                if (weakSelf.isSelectedFlashlightBtn == NO) {
                    [weakSelf removeFlashlightBtn];
                }
            }
        }];
    }
    
    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        [self.scanView addTimer];
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        [self.scanView removeTimer];
        [self removeFlashlightBtn];
        [_obtain stopRunning];
    }
    
    - (void)dealloc {
        NSLog(@"WCQRCodeVC - dealloc");
        [self removeScanningView];
    }
    
    - (SGQRCodeScanView *)scanView {
        if (!_scanView) {
            _scanView = [[SGQRCodeScanView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.9 * self.view.frame.size.height)];
        }
        return _scanView;
    }
    - (void)removeScanningView {
        [self.scanView removeTimer];
        [self.scanView removeFromSuperview];
        self.scanView = nil;
    }
    
    - (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;
    }
    
    - (UIView *)bottomView {
        if (!_bottomView) {
            _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.scanView.frame), self.view.frame.size.width, self.view.frame.size.height - CGRectGetMaxY(self.scanView.frame))];
            _bottomView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        }
        return _bottomView;
    }
    
    #pragma mark - - - 闪光灯按钮
    - (UIButton *)flashlightBtn {
        if (!_flashlightBtn) {
            // 添加闪光灯按钮
            _flashlightBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
            CGFloat flashlightBtnW = 30;
            CGFloat flashlightBtnH = 30;
            CGFloat flashlightBtnX = 0.5 * (self.view.frame.size.width - flashlightBtnW);
            CGFloat flashlightBtnY = 0.55 * self.view.frame.size.height;
            _flashlightBtn.frame = CGRectMake(flashlightBtnX, flashlightBtnY, flashlightBtnW, flashlightBtnH);
            [_flashlightBtn setBackgroundImage:[UIImage imageNamed:@"SGQRCodeFlashlightOpenImage"] forState:(UIControlStateNormal)];
            [_flashlightBtn setBackgroundImage:[UIImage imageNamed:@"SGQRCodeFlashlightCloseImage"] forState:(UIControlStateSelected)];
            [_flashlightBtn addTarget:self action:@selector(flashlightBtn_action:) forControlEvents:UIControlEventTouchUpInside];
        }
        return _flashlightBtn;
    }
    
    - (void)flashlightBtn_action:(UIButton *)button {
        if (button.selected == NO) {
            [_obtain openFlashlight];
            self.isSelectedFlashlightBtn = YES;
            button.selected = YES;
        } else {
            [self removeFlashlightBtn];
        }
    }
    
    - (void)removeFlashlightBtn {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [_obtain closeFlashlight];
            self.isSelectedFlashlightBtn = NO;
            self.flashlightBtn.selected = NO;
            [self.flashlightBtn removeFromSuperview];
        });
    }
    
    
    @end
    
    

    相关文章

      网友评论

          本文标题:好用的二维码库

          本文链接:https://www.haomeiwen.com/subject/kcyvxqtx.html