fix(camera): synchroniser le ratio de visée 3/4 avec le capteur natif et corriger le décalage du crop

This commit is contained in:
qguillaume
2026-05-27 17:44:57 +02:00
parent 730629a031
commit a5d0251545
3 changed files with 76 additions and 7 deletions

View File

@@ -154,4 +154,43 @@ class ImageCropService {
// Ignorer les erreurs
}
}
/// Coupe automatiquement une photo brute de l'appareil photo en carré
/// calé sur la position haute du viseur de l'écran.
/// Coupe une photo brute selon les proportions exactes du viseur de l'écran.
Future<String> cropRawCameraToSquare(String sourcePath) async {
final file = File(sourcePath);
final bytes = await file.readAsBytes();
final originalImage = img.decodeImage(bytes);
if (originalImage == null) return sourcePath;
// 1. Déterminer les dimensions réelles (gestion automatique de la rotation du capteur)
final int imgW = originalImage.width;
final int imgH = originalImage.height;
// 2. Ton cadre à l'écran fait 85% de la largeur du viewport (0.85).
// On calcule la taille du carré par rapport à la plus petite dimension.
final int cropSizePixels = (math.min(imgW, imgH) * 0.85).round();
// 3. Calcul du centre parfait pour le x et le y
final int x = ((imgW - cropSizePixels) / 2).round();
final int y = ((imgH - cropSizePixels) / 2).round();
// 4. Découpe physique au pixel près
final squareImage = img.copyCrop(
originalImage,
x: x.clamp(0, imgW - 1),
y: y.clamp(0, imgH - 1),
width: cropSizePixels,
height: cropSizePixels,
);
// 5. Sauvegarde
final tempDir = await getTemporaryDirectory();
final outputPath = '${tempDir.path}/camera_square_${DateTime.now().millisecondsSinceEpoch}.jpg';
await File(outputPath).writeAsBytes(img.encodeJpg(squareImage, quality: 90));
return outputPath;
}
}