2
0

LBXScanViewController.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. //
  2. //
  3. //
  4. //
  5. // Created by lbxia on 15/10/21.
  6. // Copyright © 2015年 lbxia. All rights reserved.
  7. //
  8. #import "LBXScanViewController.h"
  9. #import <AssetsLibrary/AssetsLibrary.h>
  10. #import <Photos/Photos.h>
  11. #import "LBXScanTypes.h"
  12. #import "LBXScanView.h"
  13. #import "LBXScanNative.h" //原生扫码封装
  14. @interface LBXScanViewController ()
  15. @end
  16. @implementation LBXScanViewController
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. // Do any additional setup after loading the view.
  20. if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
  21. self.edgesForExtendedLayout = UIRectEdgeNone;
  22. }
  23. self.view.backgroundColor = [UIColor blackColor];
  24. self.title = @"二维码/条形码";
  25. }
  26. - (void)viewDidAppear:(BOOL)animated
  27. {
  28. [super viewDidAppear:animated];
  29. [self drawScanView];
  30. [self requestCameraPemissionWithResult:^(BOOL granted) {
  31. if (granted) {
  32. //不延时,可能会导致界面黑屏并卡住一会
  33. [self performSelector:@selector(startScan) withObject:nil afterDelay:0.3];
  34. }else{
  35. [_qRScanView stopDeviceReadying];
  36. }
  37. }];
  38. }
  39. //绘制扫描区域
  40. - (void)drawScanView
  41. {
  42. if (!_qRScanView)
  43. {
  44. CGRect rect = self.view.frame;
  45. rect.origin = CGPointMake(0, 0);
  46. self.qRScanView = [[LBXScanView alloc]initWithFrame:rect style:_style];
  47. [self.view addSubview:_qRScanView];
  48. }
  49. if (!_cameraInvokeMsg) {
  50. // _cameraInvokeMsg = NSLocalizedString(@"wating...", nil);
  51. }
  52. [_qRScanView startDeviceReadyingWithText:_cameraInvokeMsg];
  53. }
  54. - (void)reStartDevice
  55. {
  56. switch (_libraryType) {
  57. case SLT_Native:
  58. {
  59. [_scanObj startScan];
  60. }
  61. break;
  62. default:
  63. break;
  64. }
  65. }
  66. //启动设备
  67. - (void)startScan
  68. {
  69. UIView *videoView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
  70. videoView.backgroundColor = [UIColor clearColor];
  71. [self.view insertSubview:videoView atIndex:0];
  72. __weak __typeof(self) weakSelf = self;
  73. switch (_libraryType) {
  74. case SLT_Native:
  75. {
  76. if (!_scanObj )
  77. {
  78. CGRect cropRect = CGRectZero;
  79. if (_isOpenInterestRect) {
  80. //设置只识别框内区域
  81. cropRect = [LBXScanView getScanRectWithPreView:self.view style:_style];
  82. }
  83. NSString *strCode = AVMetadataObjectTypeQRCode;
  84. if (_scanCodeType != SCT_BarCodeITF ) {
  85. strCode = [self nativeCodeWithType:_scanCodeType];
  86. }
  87. //AVMetadataObjectTypeITF14Code 扫码效果不行,另外只能输入一个码制,虽然接口是可以输入多个码制
  88. self.scanObj = [[LBXScanNative alloc]initWithPreView:videoView ObjectType:@[strCode] cropRect:cropRect success:^(NSArray<LBXScanResult *> *array) {
  89. [weakSelf scanResultWithArray:array];
  90. }];
  91. [_scanObj setNeedCaptureImage:_isNeedScanImage];
  92. }
  93. [_scanObj startScan];
  94. }
  95. break;
  96. default:
  97. break;
  98. }
  99. [_qRScanView stopDeviceReadying];
  100. [_qRScanView startScanAnimation];
  101. self.view.backgroundColor = [UIColor clearColor];
  102. }
  103. - (void)viewWillDisappear:(BOOL)animated
  104. {
  105. [super viewWillDisappear:animated];
  106. [NSObject cancelPreviousPerformRequestsWithTarget:self];
  107. [self stopScan];
  108. [_qRScanView stopScanAnimation];
  109. }
  110. - (void)stopScan
  111. {
  112. switch (_libraryType) {
  113. case SLT_Native:
  114. {
  115. [_scanObj stopScan];
  116. }
  117. break;
  118. default:
  119. break;
  120. }
  121. }
  122. #pragma mark -扫码结果处理
  123. - (void)scanResultWithArray:(NSArray<LBXScanResult*>*)array
  124. {
  125. //设置了委托的处理
  126. if (_delegate) {
  127. [_delegate scanResultWithArray:array];
  128. }
  129. //也可以通过继承LBXScanViewController,重写本方法即可
  130. }
  131. //开关闪光灯
  132. - (void)openOrCloseFlash
  133. {
  134. switch (_libraryType) {
  135. case SLT_Native:
  136. {
  137. [_scanObj changeTorch];
  138. }
  139. break;
  140. default:
  141. break;
  142. }
  143. self.isOpenFlash =!self.isOpenFlash;
  144. }
  145. #pragma mark --打开相册并识别图片
  146. /*!
  147. * 打开本地照片,选择图片识别
  148. */
  149. - (void)openLocalPhoto:(BOOL)allowsEditing
  150. {
  151. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  152. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  153. picker.delegate = self;
  154. //部分机型有问题
  155. picker.allowsEditing = allowsEditing;
  156. [self presentViewController:picker animated:YES completion:nil];
  157. }
  158. //当选择一张图片后进入这里
  159. -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  160. {
  161. [picker dismissViewControllerAnimated:YES completion:nil];
  162. __block UIImage* image = [info objectForKey:UIImagePickerControllerEditedImage];
  163. if (!image){
  164. image = [info objectForKey:UIImagePickerControllerOriginalImage];
  165. }
  166. __weak __typeof(self) weakSelf = self;
  167. switch (_libraryType) {
  168. case SLT_Native:
  169. {
  170. if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 8.0)
  171. {
  172. [LBXScanNative recognizeImage:image success:^(NSArray<LBXScanResult *> *array) {
  173. [weakSelf scanResultWithArray:array];
  174. }];
  175. }
  176. else
  177. {
  178. [self showError:@"native低于ios8.0系统不支持识别图片条码"];
  179. }
  180. }
  181. break;
  182. case SLT_ZXing:
  183. {
  184. }
  185. break;
  186. case SLT_ZBar:
  187. {
  188. }
  189. break;
  190. default:
  191. break;
  192. }
  193. }
  194. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  195. {
  196. NSLog(@"cancel");
  197. [picker dismissViewControllerAnimated:YES completion:nil];
  198. }
  199. - (NSString*)nativeCodeWithType:(SCANCODETYPE)type
  200. {
  201. switch (type) {
  202. case SCT_QRCode:
  203. return AVMetadataObjectTypeQRCode;
  204. break;
  205. case SCT_BarCode93:
  206. return AVMetadataObjectTypeCode93Code;
  207. break;
  208. case SCT_BarCode128:
  209. return AVMetadataObjectTypeCode128Code;
  210. break;
  211. case SCT_BarCodeITF:
  212. return @"ITF条码:only ZXing支持";
  213. break;
  214. case SCT_BarEAN13:
  215. return AVMetadataObjectTypeEAN13Code;
  216. break;
  217. default:
  218. return AVMetadataObjectTypeQRCode;
  219. break;
  220. }
  221. }
  222. - (void)showError:(NSString*)str
  223. {
  224. }
  225. - (void)requestCameraPemissionWithResult:(void(^)( BOOL granted))completion
  226. {
  227. if ([AVCaptureDevice respondsToSelector:@selector(authorizationStatusForMediaType:)])
  228. {
  229. AVAuthorizationStatus permission =
  230. [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  231. switch (permission) {
  232. case AVAuthorizationStatusAuthorized:
  233. completion(YES);
  234. break;
  235. case AVAuthorizationStatusDenied:
  236. case AVAuthorizationStatusRestricted:
  237. completion(NO);
  238. break;
  239. case AVAuthorizationStatusNotDetermined:
  240. {
  241. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
  242. completionHandler:^(BOOL granted) {
  243. dispatch_async(dispatch_get_main_queue(), ^{
  244. if (granted) {
  245. completion(true);
  246. } else {
  247. completion(false);
  248. }
  249. });
  250. }];
  251. }
  252. break;
  253. }
  254. }
  255. }
  256. + (BOOL)photoPermission
  257. {
  258. if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)
  259. {
  260. ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
  261. if ( author == ALAuthorizationStatusDenied ) {
  262. return NO;
  263. }
  264. return YES;
  265. }
  266. PHAuthorizationStatus authorStatus = [PHPhotoLibrary authorizationStatus];
  267. if ( authorStatus == PHAuthorizationStatusDenied ) {
  268. return NO;
  269. }
  270. return YES;
  271. }
  272. @end