normalizeCameraCapture function
Future<File>
normalizeCameraCapture(
- File jpeg, {
- required DeviceOrientation physical,
- required int sensorOrientation,
- required bool isFrontCamera,
Re-encodes a captured jpeg as an upright WebP file in the temporary
directory, from the device's physical orientation at capture, the camera's
sensorOrientation and whether it isFrontCamera.
Implementation
Future<io.File> normalizeCameraCapture(
io.File jpeg, {
required DeviceOrientation physical,
required int sensorOrientation,
required bool isFrontCamera,
}) async {
final original = await jpeg.readAsBytes();
final exif = readJpegExifOrientation(original);
final dims = readJpegRawDimensions(original);
// Some OEMs (notably Huawei EMUI) physically pre-rotate the JPEG pixels to
// match `targetRotation` and stamp EXIF=1; others (Pixel) leave pixels at
// sensor orientation and stamp a non-identity EXIF tag. Detect via dim swap
// (90/270° rotations swap dims) plus the EXIF=1 + non-zero CameraX-formula
// case (180° rotations leave dims alone, but EXIF=1 with a non-zero
// would-be-rotation only happens on pre-rotating OEMs — Pixel writes EXIF=3
// there).
final sensorIsLandscapeReadout =
sensorOrientation == 90 || sensorOrientation == 270;
final jpegIsLandscape = dims != null && dims.width >= dims.height;
final dimsSwapped =
dims != null && sensorIsLandscapeReadout != jpegIsLandscape;
final displayRotation = _flutterToDisplayRotationDegrees(physical);
final cameraxPreRotation = isFrontCamera
? (sensorOrientation + displayRotation) % 360
: (sensorOrientation - displayRotation + 360) % 360;
final preRotated = dimsSwapped || (exif == 1 && cameraxPreRotation != 0);
final pixelExpected = computeCaptureTransform(
physical: physical,
sensorOrientation: sensorOrientation,
isFrontCamera: isFrontCamera,
);
final Uint8List rgba;
final int width;
final int height;
final WebPOrientation outputOrientation;
if (preRotated) {
// CameraX pre-rotated by `cameraxPreRotation` (standard CameraX formula
// off `displayRotation`). Our target is `pixelExpected.rotationDegrees`
// (off `_surfaceDegrees`). The two diverge by 180° for landscapes because
// [_surfaceDegrees] and [_flutterToDisplayRotationDegrees] use complementary
// conventions for landscapeLeft/Right. Apply the residual.
final decoded = await _decodeJpegRgba(original);
rgba = decoded.rgba;
width = decoded.width;
height = decoded.height;
final residual =
(pixelExpected.rotationDegrees - cameraxPreRotation + 360) % 360;
outputOrientation = _webpOrientationFor((
rotationDegrees: residual,
mirror: pixelExpected.mirror,
));
} else {
final decoded = await _decodeJpegRgba(stripJpegApp1(original));
rgba = decoded.rgba;
width = decoded.width;
height = decoded.height;
outputOrientation = _webpOrientationFor(pixelExpected);
}
final webp = await compute(
encodeWebP,
WebPEncodeInput(
rgba: rgba,
width: width,
height: height,
orientation: outputOrientation,
),
);
if (webp == null) {
throw StateError('WebP encode failed');
}
final tmp = await getTemporaryDirectory();
final outPath =
'${tmp.path}/capture_${clock.now().microsecondsSinceEpoch}.webp';
final outFile = io.File(outPath);
await outFile.writeAsBytes(webp, flush: true);
return outFile;
}