KSScanViewController.m 7.1 KB

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