SWQRCodeManager.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // SWQRCodeManager.m
  3. // SWQRCode_Objc
  4. //
  5. // Created by zhuku on 2018/4/4.
  6. // Copyright © 2018年 selwyn. All rights reserved.
  7. //
  8. #import "SWQRCodeManager.h"
  9. #import <Photos/PHPhotoLibrary.h>
  10. @implementation SWQRCodeManager
  11. /** 校验是否有相机权限 */
  12. + (void)sw_checkCameraAuthorizationStatusWithGrand:(void(^)(BOOL granted))permissionGranted
  13. {
  14. AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  15. switch (videoAuthStatus) {
  16. // 已授权
  17. case AVAuthorizationStatusAuthorized:
  18. {
  19. permissionGranted(YES);
  20. }
  21. break;
  22. // 未询问用户是否授权
  23. case AVAuthorizationStatusNotDetermined:
  24. {
  25. // 提示用户授权
  26. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
  27. permissionGranted(granted);
  28. }];
  29. }
  30. break;
  31. // 用户拒绝授权或权限受限
  32. case AVAuthorizationStatusRestricted:
  33. case AVAuthorizationStatusDenied:
  34. {
  35. UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"请在”设置-隐私-相机”选项中,允许访问你的相机" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
  36. [alert show];
  37. permissionGranted(NO);
  38. }
  39. break;
  40. default:
  41. break;
  42. }
  43. }
  44. /** 校验是否有相册权限 */
  45. + (void)sw_checkAlbumAuthorizationStatusWithGrand:(void(^)(BOOL granted))permissionGranted {
  46. PHAuthorizationStatus photoAuthStatus = [PHPhotoLibrary authorizationStatus];
  47. switch (photoAuthStatus) {
  48. // 已授权
  49. case PHAuthorizationStatusAuthorized:
  50. {
  51. permissionGranted(YES);
  52. }
  53. break;
  54. // 未询问用户是否授权
  55. case PHAuthorizationStatusNotDetermined:
  56. {
  57. [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
  58. permissionGranted(status == PHAuthorizationStatusAuthorized);
  59. }];
  60. }
  61. break;
  62. // 用户拒绝授权或权限受限
  63. case PHAuthorizationStatusRestricted:
  64. case PHAuthorizationStatusDenied:
  65. {
  66. UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"请在”设置-隐私-相片”选项中,允许访问你的相册" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
  67. [alert show];
  68. permissionGranted(NO);
  69. }
  70. break;
  71. default:
  72. break;
  73. }
  74. }
  75. /** 根据扫描器类型配置支持编码格式 */
  76. + (NSArray *)sw_metadataObjectTypesWithType:(SWScannerType)scannerType {
  77. switch (scannerType) {
  78. case SWScannerTypeQRCode:
  79. {
  80. return @[AVMetadataObjectTypeQRCode];
  81. }
  82. break;
  83. case SWScannerTypeBarCode:
  84. {
  85. return @[AVMetadataObjectTypeEAN13Code,
  86. AVMetadataObjectTypeEAN8Code,
  87. AVMetadataObjectTypeUPCECode,
  88. AVMetadataObjectTypeCode39Code,
  89. AVMetadataObjectTypeCode39Mod43Code,
  90. AVMetadataObjectTypeCode93Code,
  91. AVMetadataObjectTypeCode128Code,
  92. AVMetadataObjectTypePDF417Code];
  93. }
  94. break;
  95. case SWScannerTypeBoth:
  96. {
  97. return @[AVMetadataObjectTypeQRCode,
  98. AVMetadataObjectTypeEAN13Code,
  99. AVMetadataObjectTypeEAN8Code,
  100. AVMetadataObjectTypeUPCECode,
  101. AVMetadataObjectTypeCode39Code,
  102. AVMetadataObjectTypeCode39Mod43Code,
  103. AVMetadataObjectTypeCode93Code,
  104. AVMetadataObjectTypeCode128Code,
  105. AVMetadataObjectTypePDF417Code];
  106. }
  107. break;
  108. default:
  109. break;
  110. }
  111. }
  112. /** 根据扫描器类型配置导航栏标题 */
  113. + (NSString *)sw_navigationItemTitleWithType:(SWScannerType)scannerType {
  114. switch (scannerType) {
  115. case SWScannerTypeQRCode:
  116. {
  117. return @"二维码";
  118. }
  119. break;
  120. case SWScannerTypeBarCode:
  121. {
  122. return @"条码";
  123. }
  124. break;
  125. case SWScannerTypeBoth:
  126. {
  127. return @"二维码/条码";
  128. }
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134. /** 手电筒开关 */
  135. + (void)sw_FlashlightOn:(BOOL)on {
  136. AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  137. if ([captureDevice hasTorch] && [captureDevice hasFlash]) {
  138. [captureDevice lockForConfiguration:nil];
  139. if (on) {
  140. [captureDevice setTorchMode:AVCaptureTorchModeOn];
  141. [captureDevice setFlashMode:AVCaptureFlashModeOn];
  142. }else
  143. {
  144. [captureDevice setTorchMode:AVCaptureTorchModeOff];
  145. [captureDevice setFlashMode:AVCaptureFlashModeOff];
  146. }
  147. [captureDevice unlockForConfiguration];
  148. }
  149. }
  150. @end