123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- //
- // KSScanViewController.m
- // KulexiuForTeacher
- //
- // Created by 王智 on 2022/6/30.
- //
- #import "KSScanViewController.h"
- #import "GRScanManager.h"
- #import "ScanNavView.h"
- #import "KSBaseWKWebViewController.h"
- #import "KSMediaManager.h"
- #import "KSPremissionAlert.h"
- #import "RecordCheckManager.h"
- @interface KSScanViewController ()
- @property (nonatomic, strong) KSMediaManager *mediaManager;
- @property (nonatomic, strong) NSMutableArray *imageArray; // 图片数组
- @property (nonatomic, strong) NSMutableArray *imageAsset; // 图片 asset
- @property (nonatomic, strong) GRScanManager *scanManager;
- @property (nonatomic, strong) ScanNavView *navView;
- @property (nonatomic, strong) UIImageView *scanView;
- @property (nonatomic, strong) UIView *maskView;
- @end
- @implementation KSScanViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- self.ks_prefersNavigationBarHidden = YES;
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- [self configUI];
- }
- - (void)configUI {
- UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((KPortraitWidth - 255) / 2.0f, (KPortraitHeight - 255) / 2.0f, 255, 255)];
- CGRect qrViweFrame = imageView.frame;
- imageView.image = [UIImage imageNamed:@"ScanView"];
- [self.view addSubview:imageView];
-
- self.scanView = imageView;
-
- CAShapeLayer *shperLayer = [[CAShapeLayer alloc]init];
-
- CGMutablePathRef path = CGPathCreateMutable();
- // 保留顶部
- CGPathAddRect(path, nil, CGRectMake(0, 0, KPortraitWidth, CGRectGetMinY(qrViweFrame)));
- // 保留底部
- CGPathAddRect(path, nil, CGRectMake(0,CGRectGetMaxY(qrViweFrame), KPortraitWidth,KPortraitHeight - CGRectGetMaxY(qrViweFrame)));
- //保留左边
- CGPathAddRect(path, nil, CGRectMake(0,CGRectGetMinY(qrViweFrame), CGRectGetMinX(qrViweFrame),300));
- //保留右边
- CGPathAddRect(path, nil, CGRectMake(CGRectGetMaxX(qrViweFrame),CGRectGetMinY(qrViweFrame),KPortraitWidth - CGRectGetMaxX(qrViweFrame),300));
- shperLayer.path = path;
- CGPathRelease(path);
-
- UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
- view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
- [self.view addSubview:view];
- self.maskView = view;
- view.layer.mask = shperLayer;
-
- [self.view addSubview:self.navView];
- [self.navView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.top.mas_equalTo(self.view);
- make.height.mas_equalTo(kNaviBarHeight);
- }];
-
- UILabel *msg = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(imageView.frame) + 15, KPortraitWidth - 60, 22)];
- msg.backgroundColor = [UIColor clearColor];
- msg.textColor = HexRGB(0xffffff);
- msg.textAlignment = NSTextAlignmentCenter;
- msg.font = [UIFont systemFontOfSize:16];
- msg.text = @"请对准需要识别的二维码。";
- [self.view addSubview:msg];
- MJWeakSelf;
- [self.scanManager startScanningQRCodeWithInView:self.view scanView:self.scanView resultCallback:^(NSString *result) {
- [weakSelf.scanManager stopScanning];
- [weakSelf dealWithCheckSource:result];
- NSLog(@"%@",result);
- }];
- }
- - (void)dealWithCheckSource:(NSString *)result {
- if (result) {
- if ([result containsString:hostURL]) {
- KSBaseWKWebViewController *webCtrl = [[KSBaseWKWebViewController alloc] init];
- webCtrl.url = result;
- [self.navigationController pushViewController:webCtrl animated:YES];
- }
- else {
- [self backPreView];
- }
- }
- }
- - (void)backPreView {
- MJWeakSelf;
- [self KSShowMsg:@"请扫描酷乐秀二维码" promptCompletion:^{
- [weakSelf.navigationController popViewControllerAnimated:YES];
- }];
- }
- - (GRScanManager *)scanManager {
- if (!_scanManager) {
- _scanManager = [[GRScanManager alloc] init];
- _scanManager.supportBarCode = YES;
- _scanManager.supportQRCode = YES;
- }
- return _scanManager;
- }
- - (ScanNavView *)navView {
- if (!_navView) {
- _navView = [ScanNavView shareInstance];
- MJWeakSelf;
- [_navView scanActionCallback:^(SCANNAV_ACTION action) {
- [weakSelf topNavAction:action];
- }];
- }
- return _navView;
- }
- - (void)topNavAction:(SCANNAV_ACTION)action {
- if (action == SCANNAV_ACTION_BACK) {
- [self.navigationController popViewControllerAnimated:YES];
- }
- else { // 选择相册
- // 判断是否有权限
- PREMISSIONTYPE albumEnable = [RecordCheckManager checkPhotoLibraryPremissionAvaiable:NO showInView:nil];
- if (albumEnable == PREMISSIONTYPE_YES) { // 如果有权限
- [self choosePhoneScan];
- }
- else {
- if (albumEnable == PREMISSIONTYPE_NO) {
- [self showAlertWithMessage:@"请开启相册访问权限" type:CHECKDEVICETYPE_CAMREA];
- }
- }
-
- }
- }
- - (void)showAlertWithMessage:(NSString *)message type:(CHECKDEVICETYPE)deviceType {
- [KSPremissionAlert shareInstanceDisplayImage:deviceType message:message showInView:self.view cancel:^{
-
- } confirm:^{
- [self openSettingView];
- }];
-
- }
- - (void)openSettingView {
- if (@available(iOS 10, *)) {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
- } else {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
- }
- }
- - (void)choosePhoneScan {
- // 调用相册
- self.mediaManager = [[KSMediaManager alloc] init];
- self.mediaManager.mediaType = MEDIATYPE_PHOTO;
- self.mediaManager.maxPhotoNumber = 1;
- self.mediaManager.baseCtrl = self;
- self.mediaManager.imageArray = [self.imageArray mutableCopy];
- self.mediaManager.imageAsset = [self.imageAsset mutableCopy];
- self.mediaManager.needCropImage = NO;
- MJWeakSelf;
- [self.mediaManager noAlertCallback:^(NSString * _Nullable videoUrl, NSMutableArray * _Nullable imageArray, NSMutableArray * _Nullable imageAsset) {
- [weakSelf scanImage:[imageArray lastObject]];
- }];
- [self.mediaManager pushImagePickerController];
- }
- - (void)scanImage:(UIImage *)image {
- CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{ CIDetectorAccuracy : CIDetectorAccuracyHigh }];
-
- NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]];
- if (features.count > 0) {
- CIQRCodeFeature *feature = [features objectAtIndex:0];
- NSString *scannedResult = feature.messageString;
- NSLog(@"result:%@",scannedResult);
- [self dealWithCheckSource:scannedResult];
- }
- else {
- [self MBPShow:@"无法识别"];
- }
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
|