image_picker_util.dart 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import 'dart:io';
  2. import 'dart:math';
  3. import 'package:clean/model/asset_group.dart';
  4. import 'package:get/get.dart';
  5. import 'package:photo_manager/photo_manager.dart';
  6. class ImagePickerUtil {
  7. ImagePickerUtil._();
  8. static const RequestType permissionType = RequestType.image;
  9. // 全局存储不同类型的照片
  10. // 截图图片
  11. static final RxList<AssetEntity> screenshotPhotos = <AssetEntity>[].obs;
  12. // 相似图片
  13. static final RxList<List<AssetEntity>> similarPhotos =
  14. <List<AssetEntity>>[].obs;
  15. // 地点图片
  16. static final RxMap<String, List<AssetEntity>> locationPhotos =
  17. <String, List<AssetEntity>>{}.obs;
  18. // 人物图片
  19. static final RxList<AssetEntity> peoplePhotos = <AssetEntity>[].obs;
  20. // 添加大小信息的变量
  21. static final Rx<int> screenshotsSize = 0.obs;
  22. static final Rx<int> locationsSize = 0.obs;
  23. static final Rx<int> peopleSize = 0.obs;
  24. static final Rx<int> similarPhotosSize = 0.obs;
  25. // 清除所有照片数据
  26. static void clearAllPhotos() {
  27. screenshotPhotos.clear();
  28. similarPhotos.clear();
  29. locationPhotos.clear();
  30. peoplePhotos.clear();
  31. }
  32. // 更新照片数据
  33. static Future<void> updatePhotos(
  34. List<Map<String, dynamic>> photoGroups) async {
  35. clearAllPhotos();
  36. for (var group in photoGroups) {
  37. String type = group['type'] as String;
  38. Map<dynamic, dynamic> groupData = group['group'] as Map<dynamic, dynamic>;
  39. List<dynamic> photos = groupData['photos'] as List<dynamic>;
  40. int totalSize = groupData['totalSize'] as int;
  41. int count = groupData['count'] as int;
  42. switch (type) {
  43. case 'screenshots':
  44. screenshotPhotos.value = await _convertToAssetEntities(photos);
  45. screenshotsSize.value = totalSize;
  46. break;
  47. case 'similar':
  48. similarPhotos.add(await _convertToAssetEntities(photos));
  49. similarPhotosSize.value = totalSize;
  50. break;
  51. case 'location':
  52. String location = group['name'] as String;
  53. locationPhotos[location] = await _convertToAssetEntities(photos);
  54. locationsSize.value = totalSize;
  55. break;
  56. case 'people':
  57. peoplePhotos.value = await _convertToAssetEntities(photos);
  58. peopleSize.value = totalSize;
  59. break;
  60. }
  61. }
  62. }
  63. // 将原始照片数据转换为 AssetEntity 列表
  64. static Future<List<AssetEntity>> _convertToAssetEntities(
  65. List<dynamic> photos) async {
  66. List<AssetEntity> entities = [];
  67. for (var photo in photos) {
  68. final entity = await AssetEntity.fromId(photo['id'] as String);
  69. if (entity != null) {
  70. entities.add(entity);
  71. }
  72. }
  73. return entities;
  74. }
  75. //申请权限
  76. static Future<bool> requestPermissionExtend() async {
  77. final PermissionState ps = await PhotoManager.requestPermissionExtend(
  78. requestOption: const PermissionRequestOption(
  79. androidPermission: AndroidPermission(
  80. type: permissionType,
  81. mediaLocation: false,
  82. )));
  83. return ps.hasAccess;
  84. }
  85. //判断是否有权限
  86. static Future<bool> hasPermission() async {
  87. final PermissionState ps = await PhotoManager.getPermissionState(
  88. requestOption: const PermissionRequestOption(
  89. androidPermission: AndroidPermission(
  90. type: permissionType,
  91. mediaLocation: false,
  92. )));
  93. return ps.hasAccess;
  94. }
  95. static String formatFileSize(int bytes, {int decimals = 2}) {
  96. if (bytes <= 0) return '0 B';
  97. const suffixes = ['B', 'KB', 'MB', 'GB', 'TB'];
  98. var i = (log(bytes) / log(1024)).floor();
  99. // 确保不超过数组范围
  100. i = i < suffixes.length ? i : suffixes.length - 1;
  101. // 计算实际大小
  102. final size = bytes / pow(1024, i);
  103. // 格式化数字,处理小数点位数
  104. String sizeStr;
  105. if (size >= 100) {
  106. // 大于100的只保留整数
  107. sizeStr = size.round().toString();
  108. } else {
  109. // 小于100的保留指定小数位
  110. sizeStr = size.toStringAsFixed(decimals);
  111. }
  112. // 移除末尾的0和不必要的小数点
  113. sizeStr = sizeStr.replaceAll(RegExp(r'\.?0+$'), '');
  114. return '$sizeStr ${suffixes[i]}';
  115. }
  116. /// 直接转换为 GB 单位
  117. static String formatToGB(int bytes, {int decimals = 2}) {
  118. if (bytes <= 0) return '0 GB';
  119. final gb = bytes / pow(1024, 3); // 直接转换为 GB
  120. // 格式化数字,处理小数点位数
  121. String sizeStr;
  122. if (gb >= 100) {
  123. // 大于100的只保留整数
  124. sizeStr = gb.round().toString();
  125. } else {
  126. // 小于100的保留指定小数位
  127. sizeStr = gb.toStringAsFixed(decimals);
  128. }
  129. // 移除末尾的0和不必要的小数点
  130. sizeStr = sizeStr.replaceAll(RegExp(r'\.?0+$'), '');
  131. return '$sizeStr GB';
  132. }
  133. }