KSScanViewController.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // KSScanViewController.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by 王智 on 2022/6/30.
  6. //
  7. #import "KSScanViewController.h"
  8. #import "GRScanManager.h"
  9. #import "ScanNavView.h"
  10. #import "KSBaseWKWebViewController.h"
  11. #import "KSMediaManager.h"
  12. #import "KSPremissionAlert.h"
  13. #import "RecordCheckManager.h"
  14. @interface KSScanViewController ()
  15. @property (nonatomic, strong) KSMediaManager *mediaManager;
  16. @property (nonatomic, strong) NSMutableArray *imageArray; // 图片数组
  17. @property (nonatomic, strong) NSMutableArray *imageAsset; // 图片 asset
  18. @property (nonatomic, strong) GRScanManager *scanManager;
  19. @property (nonatomic, strong) ScanNavView *navView;
  20. @property (nonatomic, strong) UIImageView *scanView;
  21. @property (nonatomic, strong) UIView *maskView;
  22. @end
  23. @implementation KSScanViewController
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. // Do any additional setup after loading the view.
  27. self.ks_prefersNavigationBarHidden = YES;
  28. self.view.backgroundColor = HexRGB(0x000000);
  29. [self configUI];
  30. }
  31. - (void)viewWillAppear:(BOOL)animated {
  32. [super viewWillAppear:animated];
  33. [self startScan];
  34. }
  35. - (void)configUI {
  36. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((KPortraitWidth - 255) / 2.0f, (KPortraitHeight - 255) / 2.0f, 255, 255)];
  37. CGRect qrViweFrame = imageView.frame;
  38. imageView.image = [UIImage imageNamed:@"ScanView"];
  39. [self.view addSubview:imageView];
  40. self.scanView = imageView;
  41. CAShapeLayer *shperLayer = [[CAShapeLayer alloc]init];
  42. CGMutablePathRef path = CGPathCreateMutable();
  43. // 保留顶部
  44. CGPathAddRect(path, nil, CGRectMake(0, 0, KPortraitWidth, CGRectGetMinY(qrViweFrame)));
  45. // 保留底部
  46. CGPathAddRect(path, nil, CGRectMake(0,CGRectGetMaxY(qrViweFrame), KPortraitWidth,KPortraitHeight - CGRectGetMaxY(qrViweFrame)));
  47. //保留左边
  48. CGPathAddRect(path, nil, CGRectMake(0,CGRectGetMinY(qrViweFrame), CGRectGetMinX(qrViweFrame),300));
  49. //保留右边
  50. CGPathAddRect(path, nil, CGRectMake(CGRectGetMaxX(qrViweFrame),CGRectGetMinY(qrViweFrame),KPortraitWidth - CGRectGetMaxX(qrViweFrame),300));
  51. shperLayer.path = path;
  52. CGPathRelease(path);
  53. UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
  54. view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
  55. [self.view addSubview:view];
  56. self.maskView = view;
  57. view.layer.mask = shperLayer;
  58. [self.view addSubview:self.navView];
  59. [self.navView mas_makeConstraints:^(MASConstraintMaker *make) {
  60. make.left.right.top.mas_equalTo(self.view);
  61. make.height.mas_equalTo(kNaviBarHeight);
  62. }];
  63. UILabel *msg = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(imageView.frame) + 15, KPortraitWidth - 60, 22)];
  64. msg.backgroundColor = [UIColor clearColor];
  65. msg.textColor = HexRGB(0xffffff);
  66. msg.textAlignment = NSTextAlignmentCenter;
  67. msg.font = [UIFont systemFontOfSize:16];
  68. msg.text = @"请对准需要识别的二维码。";
  69. [self.view addSubview:msg];
  70. }
  71. - (void)startScan {
  72. MJWeakSelf;
  73. [self.scanManager startScanningQRCodeWithInView:self.view scanView:self.scanView resultCallback:^(NSString *result) {
  74. [weakSelf.scanManager stopScanning];
  75. [weakSelf dealWithCheckSource:result];
  76. NSLog(@"%@",result);
  77. }];
  78. }
  79. - (void)dealWithCheckSource:(NSString *)result {
  80. if (result) {
  81. if ([result containsString:hostURL]) {
  82. KSBaseWKWebViewController *webCtrl = [[KSBaseWKWebViewController alloc] init];
  83. webCtrl.url = result;
  84. [self.navigationController pushViewController:webCtrl animated:YES];
  85. }
  86. else {
  87. [self backPreView];
  88. }
  89. }
  90. }
  91. - (void)backPreView {
  92. [LOADING_MANAGER KSShowMsg:@"请扫描酷乐秀二维码" promptCompletion:^{
  93. [self.navigationController popViewControllerAnimated:YES];
  94. }];
  95. }
  96. - (GRScanManager *)scanManager {
  97. if (!_scanManager) {
  98. _scanManager = [[GRScanManager alloc] init];
  99. _scanManager.supportBarCode = YES;
  100. _scanManager.supportQRCode = YES;
  101. }
  102. return _scanManager;
  103. }
  104. - (ScanNavView *)navView {
  105. if (!_navView) {
  106. _navView = [ScanNavView shareInstance];
  107. MJWeakSelf;
  108. [_navView scanActionCallback:^(SCANNAV_ACTION action) {
  109. [weakSelf topNavAction:action];
  110. }];
  111. }
  112. return _navView;
  113. }
  114. - (void)topNavAction:(SCANNAV_ACTION)action {
  115. if (action == SCANNAV_ACTION_BACK) {
  116. [self.navigationController popViewControllerAnimated:YES];
  117. }
  118. else { // 选择相册
  119. // 判断是否有权限
  120. PREMISSIONTYPE albumEnable = [RecordCheckManager checkPhotoLibraryPremissionAvaiable:NO showInView:nil];
  121. if (albumEnable == PREMISSIONTYPE_YES) { // 如果有权限
  122. [self choosePhoneScan];
  123. }
  124. else {
  125. if (albumEnable == PREMISSIONTYPE_NO) {
  126. [self showAlertWithMessage:@"请开启相册访问权限" type:CHECKDEVICETYPE_CAMREA];
  127. }
  128. }
  129. }
  130. }
  131. - (void)showAlertWithMessage:(NSString *)message type:(CHECKDEVICETYPE)deviceType {
  132. [KSPremissionAlert shareInstanceDisplayImage:deviceType message:message showInView:self.view cancel:^{
  133. } confirm:^{
  134. [self openSettingView];
  135. }];
  136. }
  137. - (void)openSettingView {
  138. if (@available(iOS 10, *)) {
  139. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
  140. } else {
  141. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
  142. }
  143. }
  144. - (void)choosePhoneScan {
  145. // 调用相册
  146. self.mediaManager = [[KSMediaManager alloc] init];
  147. self.mediaManager.mediaType = MEDIATYPE_PHOTO;
  148. self.mediaManager.maxPhotoNumber = 1;
  149. self.mediaManager.baseCtrl = self;
  150. self.mediaManager.imageArray = [self.imageArray mutableCopy];
  151. self.mediaManager.imageAsset = [self.imageAsset mutableCopy];
  152. self.mediaManager.needCropImage = NO;
  153. MJWeakSelf;
  154. [self.mediaManager noAlertCallback:^(NSString * _Nullable videoUrl, NSMutableArray * _Nullable imageArray, NSMutableArray * _Nullable imageAsset) {
  155. dispatch_main_async_safe(^{
  156. [weakSelf scanImage:[imageArray lastObject]];
  157. });
  158. }];
  159. [self.mediaManager pushImagePickerController];
  160. }
  161. - (void)scanImage:(UIImage *)image {
  162. CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{ CIDetectorAccuracy : CIDetectorAccuracyHigh }];
  163. NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]];
  164. if (features.count > 0) {
  165. CIQRCodeFeature *feature = [features objectAtIndex:0];
  166. NSString *scannedResult = feature.messageString;
  167. NSLog(@"result:%@",scannedResult);
  168. [self dealWithCheckSource:scannedResult];
  169. }
  170. else {
  171. [LOADING_MANAGER MBShowAUTOHidingInWindow:@"无法识别"];
  172. }
  173. }
  174. /*
  175. #pragma mark - Navigation
  176. // In a storyboard-based application, you will often want to do a little preparation before navigation
  177. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  178. // Get the new view controller using [segue destinationViewController].
  179. // Pass the selected object to the new view controller.
  180. }
  181. */
  182. @end