I'm experiencing image quality issues after implementing image rotation in my Flutter app (the camera code). Previously, my app produced high-quality images (~11MB), but after adding orientation handling and rotation, the image quality has significantly worsened (~1MB) with visible pixelation when zooming.
I would like some tips if I can keep the photo rotation functionality.
Previous Implementation:
final fileAsBytes = await image.readAsBytes();
final base64 = base64Encode(fileAsBytes);
FFAppState().RawPhotoList!.add({
'photo': base64,
'latitude': position.latitude.toString(),
'longitude': position.longitude.toString(),
});
}
But I added device orientation tracking and rotating:
_controller = CameraController(
cameras.first,
ResolutionPreset.max,
enableAudio: false,
);
// Image processing with rotation
Future<void> _processImageInBackground(XFile image) async {
final Uint8List fileAsBytes = await image.readAsBytes();
// Added rotation processing in isolate
final rotatedImageBytes = await compute(
rotateImageImpl,
{
'imageBytes': fileAsBytes,
'orientation': _deviceOrientation,
},
);
final base64Image = base64Encode(rotatedImageBytes);
// Store in state...
}
// Rotation implementation
Uint8List rotateImageImpl(Map<String, dynamic> data) {
final decodedImage = img.decodeImage(imageBytes);
rotatedImage = img.copyRotate(
decodedImage,
angle: rotationAngle,
interpolation: img.Interpolation.cubic,
);
final encodedImage = img.encodeJpg(rotatedImage, quality: 100);
return Uint8List.fromList(encodedImage);
}
The main changes are:
- Added device orientation tracking using accelerometer
- Process images through decode → rotate → re-encode cycle
- Is there a way to maintain the original image quality while still implementing rotation?
- Im pretty sure the decode → rotate → re-encode is causing quality loss. Is there a better library to do this? Or a way to do this while keeping high quality? Photos now are under 1mb instead of the original 11mb (and you can also see this)
- Should I consider a different image format or processing library?
- Maybe moving on from jpeg can help me keep higher quality?
Device is S22 ultra if that helps. I store the image in Azure Blob Service later on.