process static method
Future<String>
process({
- required XFile shot,
- required DeviceOrientation orientation,
- required bool fourThree,
Implementation
static Future<String> process({
required XFile shot,
required DeviceOrientation orientation,
required bool fourThree,
}) async {
debugPrint('📸 iOS PhotoProcessor: orientation = $orientation');
final bytes = await shot.readAsBytes();
final decoded = img.decodeImage(bytes);
if (decoded == null) return shot.path;
img.Image imgOut = decoded;
debugPrint('📸 iOS: original size = ${decoded.width}x${decoded.height}');
switch (orientation) {
case DeviceOrientation.landscapeLeft:
debugPrint('📸 iOS: rotating 90° (landscapeLeft)');
imgOut = img.copyRotate(imgOut, angle: 90);
break;
case DeviceOrientation.landscapeRight:
debugPrint('📸 iOS: rotating 270° (landscapeRight)');
imgOut = img.copyRotate(imgOut, angle: 270);
break;
case DeviceOrientation.portraitDown:
debugPrint('📸 iOS: rotating 180° (portraitDown)');
imgOut = img.copyRotate(imgOut, angle: 180);
break;
default:
debugPrint('📸 iOS: no rotation (portraitUp or unknown)');
break;
}
debugPrint('📸 iOS: after rotation size = ${imgOut.width}x${imgOut.height}');
// Landscape orientations need an extra rotation: iOS reports orientation
// before the camera sensor rotation is applied, so we have to flip back.
if (orientation == DeviceOrientation.landscapeLeft) {
imgOut = img.copyRotate(imgOut, angle: 270);
debugPrint('📸 iOS: after correction size = ${imgOut.width}x${imgOut.height}');
} else if (orientation == DeviceOrientation.landscapeRight) {
imgOut = img.copyRotate(imgOut, angle: 90);
debugPrint('📸 iOS: after correction size = ${imgOut.width}x${imgOut.height}');
}
final w = imgOut.width, h = imgOut.height;
final isPortrait = h > w;
final wantRatio = fourThree
? (isPortrait ? 3 / 4 : 4 / 3)
: (isPortrait ? 9 / 16 : 16 / 9);
debugPrint('📸 iOS: isPortrait=$isPortrait, wantRatio=$wantRatio');
final curRatio = w / h;
const eps = 0.01;
if ((curRatio - wantRatio).abs() > eps) {
int cropW, cropH;
if (curRatio > wantRatio) {
cropH = h;
cropW = (h * wantRatio).round();
} else {
cropW = w;
cropH = (w / wantRatio).round();
}
final x = ((w - cropW) / 2).round();
final y = ((h - cropH) / 2).round();
imgOut = img.copyCrop(imgOut, x: x, y: y, width: cropW, height: cropH);
}
await File(shot.path).writeAsBytes(img.encodeJpg(imgOut));
return shot.path;
}