WCQRCodeVC.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // WCQRCodeVC.m
  3. // SGQRCodeExample
  4. //
  5. // Created by kingsic on 17/3/20.
  6. // Copyright © 2017年 kingsic. All rights reserved.
  7. //
  8. #import "WCQRCodeVC.h"
  9. #import "SGQRCode.h"
  10. #import "ScanSuccessJumpVC.h"
  11. #import "MBProgressHUD+SGQRCode.h"
  12. @interface WCQRCodeVC () {
  13. SGQRCodeObtain *obtain;
  14. }
  15. @property (nonatomic, strong) SGQRCodeScanView *scanView;
  16. @property (nonatomic, strong) UIButton *flashlightBtn;
  17. @property (nonatomic, strong) UILabel *promptLabel;
  18. @property (nonatomic, assign) BOOL isSelectedFlashlightBtn;
  19. @property (nonatomic, strong) UIView *bottomView;
  20. @property(nonatomic,strong)UILabel *titleLab;
  21. @end
  22. @implementation WCQRCodeVC
  23. - (void)viewWillAppear:(BOOL)animated {
  24. [super viewWillAppear:animated];
  25. /// 二维码开启方法
  26. [obtain startRunningWithBefore:nil completion:nil];
  27. }
  28. - (void)viewDidAppear:(BOOL)animated {
  29. [super viewDidAppear:animated];
  30. [self.scanView addTimer];
  31. }
  32. - (void)viewWillDisappear:(BOOL)animated {
  33. [super viewWillDisappear:animated];
  34. [self.scanView removeTimer];
  35. [self removeFlashlightBtn];
  36. [obtain stopRunning];
  37. }
  38. - (void)dealloc {
  39. NSLog(@"WCQRCodeVC - dealloc");
  40. [self removeScanningView];
  41. }
  42. - (void)viewDidLoad {
  43. [super viewDidLoad];
  44. // Do any additional setup after loading the view from its nib.
  45. self.view.backgroundColor = [UIColor blackColor];
  46. [self.navigationView removeAllLeftButton];
  47. _titleLab = [[UILabel alloc]init];
  48. _titleLab.frame = CGRectMake(0,0, kGScreenWidth-60, 30);
  49. _titleLab.text = @"扫码签到";
  50. [self.navigationView addTitleView:_titleLab];
  51. kWeakSelf(self);
  52. [self.navigationView addLeftButtonWithImage:[UIImage imageNamed:@"btn_back"] clickCallBack:^(UIView *view) {
  53. [weakself.navigationController popViewControllerAnimated:YES];
  54. }];
  55. obtain = [SGQRCodeObtain QRCodeObtain];
  56. [self setupQRCodeScan];
  57. [self setupNavigationBar];
  58. [self.view addSubview:self.scanView];
  59. [self.view addSubview:self.promptLabel];
  60. /// 为了 UI 效果
  61. [self.view addSubview:self.bottomView];
  62. }
  63. - (void)setupQRCodeScan {
  64. __weak typeof(self) weakSelf = self;
  65. SGQRCodeObtainConfigure *configure = [SGQRCodeObtainConfigure QRCodeObtainConfigure];
  66. configure.sampleBufferDelegate = YES;
  67. [obtain establishQRCodeObtainScanWithController:self configure:configure];
  68. [obtain setBlockWithQRCodeObtainScanResult:^(SGQRCodeObtain *obtain, NSString *result) {
  69. if (result) {
  70. [MBProgressHUD SG_showMBProgressHUDWithModifyStyleMessage:@"正在处理..." toView:weakSelf.view];
  71. [obtain stopRunning];
  72. [obtain playSoundName:@"SGQRCode.bundle/sound.caf"];
  73. ScanSuccessJumpVC *jumpVC = [[ScanSuccessJumpVC alloc] init];
  74. jumpVC.comeFromVC = ScanSuccessJumpComeFromWC;
  75. jumpVC.jump_URL = result;
  76. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  77. [MBProgressHUD SG_hideHUDForView:weakSelf.view];
  78. [weakSelf.navigationController pushViewController:jumpVC animated:YES];
  79. });
  80. }
  81. }];
  82. [obtain setBlockWithQRCodeObtainScanBrightness:^(SGQRCodeObtain *obtain, CGFloat brightness) {
  83. if (brightness < - 1) {
  84. [weakSelf.view addSubview:weakSelf.flashlightBtn];
  85. } else {
  86. if (weakSelf.isSelectedFlashlightBtn == NO) {
  87. [weakSelf removeFlashlightBtn];
  88. }
  89. }
  90. }];
  91. }
  92. - (void)setupNavigationBar {
  93. self.navigationItem.title = @"扫一扫";
  94. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"相册" style:(UIBarButtonItemStyleDone) target:self action:@selector(rightBarButtonItenAction)];
  95. }
  96. - (void)rightBarButtonItenAction {
  97. __weak typeof(self) weakSelf = self;
  98. [obtain establishAuthorizationQRCodeObtainAlbumWithController:nil];
  99. if (obtain.isPHAuthorization == YES) {
  100. [self.scanView removeTimer];
  101. }
  102. [obtain setBlockWithQRCodeObtainAlbumDidCancelImagePickerController:^(SGQRCodeObtain *obtain) {
  103. [weakSelf.view addSubview:weakSelf.scanView];
  104. }];
  105. [obtain setBlockWithQRCodeObtainAlbumResult:^(SGQRCodeObtain *obtain, NSString *result) {
  106. [MBProgressHUD SG_showMBProgressHUDWithModifyStyleMessage:@"正在处理..." toView:weakSelf.view];
  107. if (result == nil) {
  108. NSLog(@"暂未识别出二维码");
  109. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  110. [MBProgressHUD SG_hideHUDForView:weakSelf.view];
  111. [MBProgressHUD SG_showMBProgressHUDWithOnlyMessage:@"未发现二维码/条形码" delayTime:1.0];
  112. });
  113. } else {
  114. ScanSuccessJumpVC *jumpVC = [[ScanSuccessJumpVC alloc] init];
  115. jumpVC.comeFromVC = ScanSuccessJumpComeFromWC;
  116. if ([result hasPrefix:@"http"]) {
  117. jumpVC.jump_URL = result;
  118. } else {
  119. jumpVC.jump_bar_code = result;
  120. }
  121. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  122. [MBProgressHUD SG_hideHUDForView:weakSelf.view];
  123. [weakSelf.navigationController pushViewController:jumpVC animated:YES];
  124. });
  125. }
  126. }];
  127. }
  128. - (SGQRCodeScanView *)scanView {
  129. if (!_scanView) {
  130. _scanView = [[SGQRCodeScanView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.9 * self.view.frame.size.height)];
  131. }
  132. return _scanView;
  133. }
  134. - (void)removeScanningView {
  135. [self.scanView removeTimer];
  136. [self.scanView removeFromSuperview];
  137. self.scanView = nil;
  138. }
  139. - (UILabel *)promptLabel {
  140. if (!_promptLabel) {
  141. _promptLabel = [[UILabel alloc] init];
  142. _promptLabel.backgroundColor = [UIColor clearColor];
  143. CGFloat promptLabelX = 0;
  144. CGFloat promptLabelY = 0.73 * self.view.frame.size.height;
  145. CGFloat promptLabelW = self.view.frame.size.width;
  146. CGFloat promptLabelH = 25;
  147. _promptLabel.frame = CGRectMake(promptLabelX, promptLabelY, promptLabelW, promptLabelH);
  148. _promptLabel.textAlignment = NSTextAlignmentCenter;
  149. _promptLabel.font = [UIFont boldSystemFontOfSize:13.0];
  150. _promptLabel.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.6];
  151. _promptLabel.text = @"将二维码/条码放入框内, 即可自动扫描";
  152. }
  153. return _promptLabel;
  154. }
  155. - (UIView *)bottomView {
  156. if (!_bottomView) {
  157. _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.scanView.frame), self.view.frame.size.width, self.view.frame.size.height - CGRectGetMaxY(self.scanView.frame))];
  158. _bottomView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
  159. }
  160. return _bottomView;
  161. }
  162. #pragma mark - - - 闪光灯按钮
  163. - (UIButton *)flashlightBtn {
  164. if (!_flashlightBtn) {
  165. // 添加闪光灯按钮
  166. _flashlightBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
  167. CGFloat flashlightBtnW = 30;
  168. CGFloat flashlightBtnH = 30;
  169. CGFloat flashlightBtnX = 0.5 * (self.view.frame.size.width - flashlightBtnW);
  170. CGFloat flashlightBtnY = 0.55 * self.view.frame.size.height;
  171. _flashlightBtn.frame = CGRectMake(flashlightBtnX, flashlightBtnY, flashlightBtnW, flashlightBtnH);
  172. [_flashlightBtn setBackgroundImage:[UIImage imageNamed:@"SGQRCodeFlashlightOpenImage"] forState:(UIControlStateNormal)];
  173. [_flashlightBtn setBackgroundImage:[UIImage imageNamed:@"SGQRCodeFlashlightCloseImage"] forState:(UIControlStateSelected)];
  174. [_flashlightBtn addTarget:self action:@selector(flashlightBtn_action:) forControlEvents:UIControlEventTouchUpInside];
  175. }
  176. return _flashlightBtn;
  177. }
  178. - (void)flashlightBtn_action:(UIButton *)button {
  179. if (button.selected == NO) {
  180. [obtain openFlashlight];
  181. self.isSelectedFlashlightBtn = YES;
  182. button.selected = YES;
  183. } else {
  184. [self removeFlashlightBtn];
  185. }
  186. }
  187. - (void)removeFlashlightBtn {
  188. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  189. [obtain closeFlashlight];
  190. self.isSelectedFlashlightBtn = NO;
  191. self.flashlightBtn.selected = NO;
  192. [self.flashlightBtn removeFromSuperview];
  193. });
  194. }
  195. @end