省钱达人

SubLBXScanViewController.m 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. //
  3. //
  4. //
  5. // Created by lbxia on 15/10/21.
  6. // Copyright © 2015年 lbxia. All rights reserved.
  7. //
  8. #import "SubLBXScanViewController.h"
  9. #import "MyQRViewController.h"
  10. #import "ScanResultViewController.h"
  11. #import "LBXScanResult.h"
  12. #import "LBXScanWrapper.h"
  13. @interface SubLBXScanViewController ()
  14. @end
  15. @implementation SubLBXScanViewController
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. // Do any additional setup after loading the view.
  19. if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
  20. self.edgesForExtendedLayout = UIRectEdgeNone;
  21. }
  22. self.view.backgroundColor = [UIColor blackColor];
  23. }
  24. - (void)viewDidAppear:(BOOL)animated
  25. {
  26. [super viewDidAppear:animated];
  27. if (_isQQSimulator) {
  28. [self drawBottomItems];
  29. [self drawTitle];
  30. [self.view bringSubviewToFront:_topTitle];
  31. }
  32. else
  33. _topTitle.hidden = YES;
  34. }
  35. //绘制扫描区域
  36. - (void)drawTitle
  37. {
  38. if (!_topTitle)
  39. {
  40. self.topTitle = [[UILabel alloc]init];
  41. _topTitle.bounds = CGRectMake(0, 0, 145, 60);
  42. _topTitle.center = CGPointMake(CGRectGetWidth(self.view.frame)/2, 50);
  43. //3.5inch iphone
  44. if ([UIScreen mainScreen].bounds.size.height <= 568 )
  45. {
  46. _topTitle.center = CGPointMake(CGRectGetWidth(self.view.frame)/2, 38);
  47. _topTitle.font = [UIFont systemFontOfSize:14];
  48. }
  49. _topTitle.textAlignment = NSTextAlignmentCenter;
  50. _topTitle.numberOfLines = 0;
  51. _topTitle.text = @"将取景框对准二维码即可自动扫描";
  52. _topTitle.textColor = [UIColor whiteColor];
  53. [self.view addSubview:_topTitle];
  54. }
  55. }
  56. - (void)drawBottomItems
  57. {
  58. if (_bottomItemsView) {
  59. return;
  60. }
  61. self.bottomItemsView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.view.frame)-164,
  62. CGRectGetWidth(self.view.frame), 100)];
  63. _bottomItemsView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
  64. [self.view addSubview:_bottomItemsView];
  65. CGSize size = CGSizeMake(65, 87);
  66. self.btnFlash = [[UIButton alloc]init];
  67. _btnFlash.bounds = CGRectMake(0, 0, size.width, size.height);
  68. _btnFlash.center = CGPointMake(CGRectGetWidth(_bottomItemsView.frame)/2, CGRectGetHeight(_bottomItemsView.frame)/2);
  69. [_btnFlash setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_flash_nor"] forState:UIControlStateNormal];
  70. [_btnFlash addTarget:self action:@selector(openOrCloseFlash) forControlEvents:UIControlEventTouchUpInside];
  71. self.btnPhoto = [[UIButton alloc]init];
  72. _btnPhoto.bounds = _btnFlash.bounds;
  73. _btnPhoto.center = CGPointMake(CGRectGetWidth(_bottomItemsView.frame)/4, CGRectGetHeight(_bottomItemsView.frame)/2);
  74. [_btnPhoto setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_photo_nor"] forState:UIControlStateNormal];
  75. [_btnPhoto setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_photo_down"] forState:UIControlStateHighlighted];
  76. [_btnPhoto addTarget:self action:@selector(openPhoto) forControlEvents:UIControlEventTouchUpInside];
  77. self.btnMyQR = [[UIButton alloc]init];
  78. _btnMyQR.bounds = _btnFlash.bounds;
  79. _btnMyQR.center = CGPointMake(CGRectGetWidth(_bottomItemsView.frame) * 3/4, CGRectGetHeight(_bottomItemsView.frame)/2);
  80. [_btnMyQR setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_myqrcode_nor"] forState:UIControlStateNormal];
  81. [_btnMyQR setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_myqrcode_down"] forState:UIControlStateHighlighted];
  82. [_btnMyQR addTarget:self action:@selector(myQRCode) forControlEvents:UIControlEventTouchUpInside];
  83. [_bottomItemsView addSubview:_btnFlash];
  84. [_bottomItemsView addSubview:_btnPhoto];
  85. [_bottomItemsView addSubview:_btnMyQR];
  86. }
  87. - (void)showError:(NSString*)str
  88. {
  89. [LBXAlertAction showAlertWithTitle:@"提示" msg:str chooseBlock:nil buttonsStatement:@"知道了",nil];
  90. }
  91. - (void)scanResultWithArray:(NSArray<LBXScanResult*>*)array
  92. {
  93. if (array.count < 1)
  94. {
  95. [self popAlertMsgWithScanResult:nil];
  96. return;
  97. }
  98. //经测试,可以同时识别2个二维码,不能同时识别二维码和条形码
  99. for (LBXScanResult *result in array) {
  100. NSLog(@"scanResult:%@",result.strScanned);
  101. }
  102. LBXScanResult *scanResult = array[0];
  103. NSString*strResult = scanResult.strScanned;
  104. self.scanImage = scanResult.imgScanned;
  105. if (!strResult) {
  106. [self popAlertMsgWithScanResult:nil];
  107. return;
  108. }
  109. //震动提醒
  110. [LBXScanWrapper systemVibrate];
  111. //声音提醒
  112. [LBXScanWrapper systemSound];
  113. //[self popAlertMsgWithScanResult:strResult];
  114. [self showNextVCWithScanResult:scanResult];
  115. }
  116. - (void)popAlertMsgWithScanResult:(NSString*)strResult
  117. {
  118. if (!strResult) {
  119. strResult = @"识别失败";
  120. }
  121. __weak __typeof(self) weakSelf = self;
  122. [LBXAlertAction showAlertWithTitle:@"扫码内容" msg:strResult chooseBlock:^(NSInteger buttonIdx) {
  123. //点击完,继续扫码
  124. [weakSelf reStartDevice];
  125. } buttonsStatement:@"知道了",nil];
  126. }
  127. - (void)showNextVCWithScanResult:(LBXScanResult*)strResult
  128. {
  129. ScanResultViewController *vc = [ScanResultViewController new];
  130. vc.imgScan = strResult.imgScanned;
  131. vc.strScan = strResult.strScanned;
  132. vc.strCodeType = strResult.strBarCodeType;
  133. [self.navigationController pushViewController:vc animated:YES];
  134. }
  135. #pragma mark -底部功能项
  136. //打开相册
  137. - (void)openPhoto
  138. {
  139. if ([LBXScanWrapper isGetPhotoPermission])
  140. [self openLocalPhoto];
  141. else
  142. {
  143. [self showError:@" 请到设置->隐私中开启本程序相册权限 "];
  144. }
  145. }
  146. //开关闪光灯
  147. - (void)openOrCloseFlash
  148. {
  149. [super openOrCloseFlash];
  150. if (self.isOpenFlash)
  151. {
  152. [_btnFlash setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_flash_down"] forState:UIControlStateNormal];
  153. }
  154. else
  155. [_btnFlash setImage:[UIImage imageNamed:@"CodeScan.bundle/qrcode_scan_btn_flash_nor"] forState:UIControlStateNormal];
  156. }
  157. #pragma mark -底部功能项
  158. - (void)myQRCode
  159. {
  160. MyQRViewController *vc = [MyQRViewController new];
  161. [self.navigationController pushViewController:vc animated:YES];
  162. }
  163. @end