image_util.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import 'dart:io';
  2. import 'dart:typed_data';
  3. import 'package:clean/model/asset_info.dart';
  4. import 'package:intl/intl.dart';
  5. import 'package:wechat_assets_picker/wechat_assets_picker.dart';
  6. import 'file_utils.dart';
  7. import 'package:exif/exif.dart';
  8. class ImageUtil {
  9. // 生成月份 key (用于内部存储)
  10. static String getMonthKey(DateTime date) {
  11. return '${date.year}-${date.month.toString().padLeft(2, '0')}';
  12. }
  13. // 格式化月份显示 (例如: Jan 2025)
  14. static String formatMonthKey(String monthKey) {
  15. final parts = monthKey.split('-');
  16. if (parts.length == 2) {
  17. final date = DateTime(int.parse(parts[0]), int.parse(parts[1]));
  18. return DateFormat('MMM yyyy').format(date);
  19. }
  20. return monthKey;
  21. }
  22. // 获取指定索引的月份显示文本
  23. static String getMonthText(Map<String, List<AssetInfo>> assets, int index) {
  24. final monthKeys = assets.keys.toList()
  25. ..sort((a, b) => b.compareTo(a)); // 最新的月份在前
  26. if (index < monthKeys.length) {
  27. return formatMonthKey(monthKeys[index]);
  28. }
  29. return '';
  30. }
  31. // 获取指定月份的图片
  32. static List<AssetInfo> getMonthAssets(Map<String, List<AssetInfo>> assets, int index) {
  33. final monthKeys = assets.keys.toList()
  34. ..sort((a, b) => b.compareTo(a)); // 最新的月份在前
  35. if (index < monthKeys.length) {
  36. return assets[monthKeys[index]] ?? [];
  37. }
  38. return [];
  39. }
  40. // 获取缩略图数据
  41. static Future<Uint8List?> getImageThumbnail(FileType type, AssetInfo asset) async {
  42. try {
  43. // 先尝试从本地读取缩略图
  44. final localThumb = await FileUtils.getThumbData(type, asset.id.substring(0, 36));
  45. if (localThumb != null) {
  46. return localThumb;
  47. }
  48. // 如果本地没有,则从 AssetEntity 获取
  49. return await asset.thumbnailData;
  50. } catch (e) {
  51. print('获取缩略图失败: $e');
  52. return null;
  53. }
  54. }
  55. // 获取原始图片文件
  56. static Future<File?> getImageFile(FileType type, AssetInfo asset) async {
  57. try {
  58. // 先尝试从本地读取
  59. final assetPath = await FileUtils.getAssetPath(type);
  60. final localFile = File('$assetPath/${asset.id.substring(0, 36)}.jpg');
  61. if (await localFile.exists()) {
  62. return localFile;
  63. }
  64. // 如果本地没有,则从 AssetEntity 获取
  65. return asset.file;
  66. } catch (e) {
  67. print('获取图片文件失败: $e');
  68. return null;
  69. }
  70. }
  71. static Future<Map<String, dynamic>> getPhotoDetails(AssetEntity asset) async {
  72. try {
  73. final Map<String, dynamic> details = {};
  74. // 基本信息
  75. details['fileName'] = asset.title;
  76. details['createDate'] = asset.createDateTime;
  77. details['modifiedDate'] = asset.modifiedDateTime;
  78. details['width'] = asset.width;
  79. details['height'] = asset.height;
  80. details['size'] = asset.size;
  81. // 获取文件
  82. final file = await asset.file;
  83. if (file != null) {
  84. // 读取 EXIF 数据
  85. final bytes = await file.readAsBytes();
  86. final exifData = await readExifFromBytes(bytes);
  87. if (exifData.isNotEmpty) {
  88. // 相机信息
  89. details['make'] = exifData['Image Make']?.printable;
  90. details['model'] = exifData['Image Model']?.printable;
  91. // 拍摄参数
  92. details['aperture'] = exifData['EXIF ApertureValue']?.printable;
  93. details['exposureTime'] = exifData['EXIF ExposureTime']?.printable;
  94. details['iso'] = exifData['EXIF ISOSpeedRatings']?.printable;
  95. details['focalLength'] = exifData['EXIF FocalLength']?.printable;
  96. // GPS 信息
  97. if (exifData.containsKey('GPS GPSLatitude') &&
  98. exifData.containsKey('GPS GPSLongitude')) {
  99. details['latitude'] = exifData['GPS GPSLatitude']?.printable;
  100. details['longitude'] = exifData['GPS GPSLongitude']?.printable;
  101. }
  102. }
  103. }
  104. print(details);
  105. return details;
  106. } catch (e) {
  107. print('获取照片详情失败: $e');
  108. return {};
  109. }
  110. }
  111. /// 格式化图片尺寸
  112. static String formatResolution(int width, int height) {
  113. final megapixels = (width * height) / 1000000.0;
  114. if (megapixels >= 1) {
  115. return '${megapixels.toStringAsFixed(1)} MP ($width × $height)';
  116. } else {
  117. return '$width × $height';
  118. }
  119. }
  120. /// 格式化文件大小
  121. static String formatFileSize(int bytes) {
  122. if (bytes < 1024) return '$bytes B';
  123. if (bytes < 1024 * 1024) return '${(bytes / 1024).toStringAsFixed(1)} KB';
  124. if (bytes < 1024 * 1024 * 1024) {
  125. return '${(bytes / (1024 * 1024)).toStringAsFixed(1)} MB';
  126. }
  127. return '${(bytes / (1024 * 1024 * 1024)).toStringAsFixed(1)} GB';
  128. }
  129. /// 格式化光圈值
  130. static String formatAperture(String? value) {
  131. if (value == null) return 'Unknown';
  132. return 'f/$value';
  133. }
  134. /// 格式化曝光时间
  135. static String formatExposureTime(String? value) {
  136. if (value == null) return 'Unknown';
  137. // 将分数转换为更易读的格式
  138. if (value.contains('/')) {
  139. final parts = value.split('/');
  140. if (parts.length == 2) {
  141. final numerator = int.parse(parts[0]);
  142. final denominator = int.parse(parts[1]);
  143. if (numerator == 1) {
  144. return '1/${denominator}s';
  145. }
  146. }
  147. }
  148. return '${value}s';
  149. }
  150. }