Flutter camera image picker has a known issue of returning images with wrong unexpected rotation. The issue has been raised in the GitHub and marked as closed, but still occurs in the latest stable release. The issue can, however, be resolved easily with the help of exif-rotation plugin. This issue is known to be affecting both iOS and Android platforms.
Why the image is rotated
When we take an image from the camera, we could be holding the phone in any orientation. Sometimes we take photos in landscape mode, sometimes portait and so on. The camera app takes the photo and adds special metadata entries (EXIF data) to the file so that it can be rotated properly in image viewers.
Flutter image_picker has a bug where it doesn’t process the image with the given EXIF data which makes the image appear in the wrong rotation. However, we can fix it by considering the EXIF data and rotating the image manually as the metadata suggests.
How to fix the rotation?
To fix the unwanted image rotation, we need to rotate the image based on the metadata available. Luckily, a library named flutter_exif_rotation is available. Once the image is taken from the image-picker, give it to the FlutterExifRotation library. The library will rotate it, if needed, based on the available EXIF data.
Let’s have a look into the sample code.
Firstly, add the flutter_exif_rotation library to your pubspec.yaml file.
dependencies: ... flutter_exif_rotation: ^0.5.1 ...
Sample code for image pick and rotation fix
import 'dart:async'; import 'dart:io'; import 'package:flutter_exif_rotation/flutter_exif_rotation.dart'; //EXIF rotation library import 'package:image_picker/image_picker.dart'; //Image picker library class CameraService { Future<XFile?> pickImage() async { //Take a photo from camera final XFile? image = await ImagePicker().pickImage( source: ImageSource.camera, maxWidth: 1200, maxHeight: 1200, requestFullMetadata: true ); if (image != null) { // Rotate the image to fix the wrong rotation coming from ImagePicker File rotatedImage = await FlutterExifRotation.rotateImage(path: image.path); return XFile(rotatedImage.path); //Return the file } return image; } }
In the above code, The function FlutterExifRotation#rotateImage will fix the rotation of the image by considering the EXIF orientation tags. This solution has been proposed in the GitHub and many people supported this as the best way available at the moment.
If you have liked this article, you might also like other Fltuter tutorials I have written.
… [Trackback]
[…] Read More Info here to that Topic: genuinecoder.com/flutter-camera-image-orientation-rotation-issue-fix/ […]
… [Trackback]
[…] Here you will find 64163 additional Info on that Topic: genuinecoder.com/flutter-camera-image-orientation-rotation-issue-fix/ […]
… [Trackback]
[…] Here you will find 48503 more Information to that Topic: genuinecoder.com/flutter-camera-image-orientation-rotation-issue-fix/ […]
… [Trackback]
[…] Info to that Topic: genuinecoder.com/flutter-camera-image-orientation-rotation-issue-fix/ […]
… [Trackback]
[…] Read More Info here on that Topic: genuinecoder.com/flutter-camera-image-orientation-rotation-issue-fix/ […]
… [Trackback]
[…] Find More here to that Topic: genuinecoder.com/flutter-camera-image-orientation-rotation-issue-fix/ […]
… [Trackback]
[…] Read More to that Topic: genuinecoder.com/flutter-camera-image-orientation-rotation-issue-fix/ […]
… [Trackback]
[…] Read More on that Topic: genuinecoder.com/flutter-camera-image-orientation-rotation-issue-fix/ […]
… [Trackback]
[…] Find More here to that Topic: genuinecoder.com/flutter-camera-image-orientation-rotation-issue-fix/ […]
… [Trackback]
[…] Find More here on that Topic: genuinecoder.com/flutter-camera-image-orientation-rotation-issue-fix/ […]
… [Trackback]
[…] Find More Information here on that Topic: genuinecoder.com/flutter-camera-image-orientation-rotation-issue-fix/ […]
… [Trackback]
[…] Read More on that Topic: genuinecoder.com/flutter-camera-image-orientation-rotation-issue-fix/ […]
… [Trackback]
[…] Find More on to that Topic: genuinecoder.com/flutter-camera-image-orientation-rotation-issue-fix/ […]
… [Trackback]
[…] Information on that Topic: genuinecoder.com/flutter-camera-image-orientation-rotation-issue-fix/ […]
… [Trackback]
[…] Information to that Topic: genuinecoder.com/flutter-camera-image-orientation-rotation-issue-fix/ […]
… [Trackback]
[…] Read More here on that Topic: genuinecoder.com/flutter-camera-image-orientation-rotation-issue-fix/ […]
… [Trackback]
[…] Info to that Topic: genuinecoder.com/flutter-camera-image-orientation-rotation-issue-fix/ […]
… [Trackback]
[…] Information on that Topic: genuinecoder.com/flutter-camera-image-orientation-rotation-issue-fix/ […]
… [Trackback]
[…] Find More to that Topic: genuinecoder.com/flutter-camera-image-orientation-rotation-issue-fix/ […]