Build and Release FLUX (Multi-Platform) / build-web (push) Successful in 1m55s
Build and Release FLUX (Multi-Platform) / build-linux (push) Successful in 1m29s
Build and Release FLUX (Multi-Platform) / build-macos (push) Successful in 2m43s
Build and Release FLUX (Multi-Platform) / build-windows (push) Successful in 8m15s
79 lines
2.7 KiB
Dart
79 lines
2.7 KiB
Dart
import 'dart:typed_data';
|
|
import 'package:image/image.dart' as img;
|
|
|
|
/// Utilità per la gestione delle immagini e normalizzazione dell'orientamento EXIF.
|
|
class ImageUtils {
|
|
/// Verifica se l'estensione corrisponde a un'immagine supportata.
|
|
static bool isImageExtension(String? extension) {
|
|
if (extension == null) return false;
|
|
final ext = extension.toLowerCase().replaceAll('.', '');
|
|
return ['jpg', 'jpeg', 'png', 'webp', 'bmp'].contains(ext);
|
|
}
|
|
|
|
/// Verifica se un nome file o percorso è un'immagine supportata.
|
|
static bool isImagePath(String path) {
|
|
final dotIndex = path.lastIndexOf('.');
|
|
if (dotIndex == -1 || dotIndex == path.length - 1) return false;
|
|
return isImageExtension(path.substring(dotIndex + 1));
|
|
}
|
|
|
|
/// Ruota fisicamente i pixel dell'immagine se contiene metadati EXIF di orientamento
|
|
/// (tipico delle fotocamere degli smartphone), resettando l'orientamento a 1 (normale).
|
|
/// Se l'immagine è già dritta o non possiede metadati EXIF, restituisce i byte originali.
|
|
static Uint8List normalizeOrientation(Uint8List bytes, {String? extension}) {
|
|
try {
|
|
final image = img.decodeImage(bytes);
|
|
if (image == null) return bytes;
|
|
|
|
// Se non ha tag di orientamento o è già orientata dritta (1)
|
|
if (!image.exif.imageIfd.hasOrientation || image.exif.imageIfd.orientation == 1) {
|
|
return bytes;
|
|
}
|
|
|
|
// Applica la rotazione fisica dei pixel e resetta il tag EXIF
|
|
final baked = img.bakeOrientation(image);
|
|
|
|
final ext = extension?.toLowerCase().replaceAll('.', '');
|
|
final isPng = ext == 'png' || (ext == null && image.hasAlpha);
|
|
|
|
if (isPng) {
|
|
return Uint8List.fromList(img.encodePng(baked));
|
|
} else {
|
|
return Uint8List.fromList(img.encodeJpg(baked, quality: 92));
|
|
}
|
|
} catch (_) {
|
|
// In caso di errore di parsing/decodifica, restituisce i byte inalterati
|
|
return bytes;
|
|
}
|
|
}
|
|
|
|
/// Ruota fisicamente i byte di un'immagine di un numero di quarti di giro (90° * quarterTurns).
|
|
static Uint8List rotateBytes(
|
|
Uint8List bytes, {
|
|
required int quarterTurns,
|
|
String? extension,
|
|
}) {
|
|
final turns = quarterTurns % 4;
|
|
if (turns == 0) return bytes;
|
|
|
|
try {
|
|
final image = img.decodeImage(bytes);
|
|
if (image == null) return bytes;
|
|
|
|
final angle = turns * 90;
|
|
final rotated = img.copyRotate(image, angle: angle);
|
|
|
|
final ext = extension?.toLowerCase().replaceAll('.', '');
|
|
final isPng = ext == 'png' || (ext == null && image.hasAlpha);
|
|
|
|
if (isPng) {
|
|
return Uint8List.fromList(img.encodePng(rotated));
|
|
} else {
|
|
return Uint8List.fromList(img.encodeJpg(rotated, quality: 92));
|
|
}
|
|
} catch (_) {
|
|
return bytes;
|
|
}
|
|
}
|
|
}
|