2021-07-13 06:32:05 +02:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
2021-05-06 22:11:31 +02:00
|
|
|
import 'package:exifdart/exifdart.dart' as exifdart;
|
2021-07-13 06:32:05 +02:00
|
|
|
import 'package:exifdart/exifdart_memory.dart';
|
2021-04-14 23:09:31 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:image_size_getter/image_size_getter.dart';
|
2021-04-10 06:28:12 +02:00
|
|
|
import 'package:nc_photos/account.dart';
|
|
|
|
import 'package:nc_photos/entity/file.dart';
|
2021-05-06 22:11:31 +02:00
|
|
|
import 'package:nc_photos/entity/file_util.dart' as file_util;
|
2021-07-13 06:32:05 +02:00
|
|
|
import 'package:nc_photos/image_size_getter_util.dart';
|
2021-04-10 06:28:12 +02:00
|
|
|
|
2021-07-13 06:32:05 +02:00
|
|
|
class LoadMetadata {
|
|
|
|
/// Load metadata of [binary], which is the content of [file]
|
|
|
|
Future<Map<String, dynamic>> call(
|
|
|
|
Account account, File file, Uint8List binary) {
|
|
|
|
return _loadMetadata(
|
|
|
|
file: file,
|
|
|
|
exifdartReaderBuilder: () => MemoryBlobReader(binary),
|
|
|
|
imageSizeGetterInputBuilder: () => AsyncMemoryInput(binary),
|
|
|
|
);
|
|
|
|
}
|
2021-04-14 23:09:31 +02:00
|
|
|
|
2021-07-13 06:32:05 +02:00
|
|
|
Future<Map<String, dynamic>> _loadMetadata({
|
2021-05-06 22:11:31 +02:00
|
|
|
@required File file,
|
|
|
|
exifdart.AbstractBlobReader Function() exifdartReaderBuilder,
|
2021-04-14 23:09:31 +02:00
|
|
|
AsyncImageInput Function() imageSizeGetterInputBuilder,
|
|
|
|
}) async {
|
2021-05-06 22:11:31 +02:00
|
|
|
exifdart.Metadata metadata;
|
|
|
|
if (file_util.isMetadataSupportedFormat(file)) {
|
|
|
|
metadata = await exifdart.readMetadata(exifdartReaderBuilder());
|
|
|
|
} else {
|
|
|
|
metadata = exifdart.Metadata();
|
|
|
|
}
|
2021-04-14 23:09:31 +02:00
|
|
|
int imageWidth, imageHeight;
|
|
|
|
if (metadata.imageWidth == null || metadata.imageHeight == null) {
|
|
|
|
final resolution =
|
|
|
|
await AsyncImageSizeGetter.getSize(imageSizeGetterInputBuilder());
|
2021-04-15 00:57:23 +02:00
|
|
|
// image size getter doesn't handle exif orientation
|
|
|
|
if (metadata.exif?.containsKey("Orientation") == true &&
|
|
|
|
metadata.exif["Orientation"] >= 5 &&
|
|
|
|
metadata.exif["Orientation"] <= 8) {
|
|
|
|
// 90 deg CW/CCW
|
|
|
|
imageWidth = resolution.height;
|
|
|
|
imageHeight = resolution.width;
|
|
|
|
} else {
|
|
|
|
imageWidth = resolution.width;
|
|
|
|
imageHeight = resolution.height;
|
|
|
|
}
|
2021-04-14 23:09:31 +02:00
|
|
|
} else {
|
|
|
|
if (metadata.rotateAngleCcw != null &&
|
|
|
|
metadata.rotateAngleCcw % 180 != 0) {
|
|
|
|
imageWidth = metadata.imageHeight;
|
|
|
|
imageHeight = metadata.imageWidth;
|
|
|
|
} else {
|
|
|
|
imageWidth = metadata.imageWidth;
|
|
|
|
imageHeight = metadata.imageHeight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
if (metadata.exif != null) "exif": metadata.exif,
|
|
|
|
if (imageWidth > 0 && imageHeight > 0)
|
|
|
|
"resolution": {
|
|
|
|
"width": imageWidth,
|
|
|
|
"height": imageHeight,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|