How to fix flutter image picker orientation rotation fix

How to fix flutter camera image picker rotation issue?

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.

Muhammed Afsal Villan

Muhammed Afsal Villan is an experienced full-stack developer, specialized in desktop and mobile application development. He also regularly publishes quality tutorials on his YouTube channel named 'Genuine Coder'. He likes to contribute to open-source projects and is always enthusiastic about new technologies.

More From Author

Dart syntax highlight for Wordpress

Dart/Flutter code highlighting for WordPress

Microservices using Spring Boot

Building Real-World Microservices with Spring Boot 3: A Step-by-Step Guide

76 thoughts on “How to fix flutter camera image picker rotation issue?

  1. Pingback: บัตร psn
  2. Pingback: https://noonoo.org
  3. Pingback: site
  4. Pingback: HUAYDEE
  5. Pingback: lucabet88
  6. Pingback: Browning shotguns
  7. Pingback: Ashley
  8. Pingback: sa game login
  9. Pingback: Sylfirm
  10. Pingback: dark168
  11. Pingback: rca77
  12. Pingback: fox888
  13. Pingback: Tor
  14. Pingback: profibus connector
  15. Pingback: linkno789
  16. Pingback: rybelsus 3mg
  17. Pingback: cialis online
  18. Pingback: cialis coupon
  19. Pingback: 50mg viagra cost
  20. Pingback: cialis prices
  21. Pingback: get cialis
  22. Pingback: cialis 20mg
  23. Pingback: cialis in mexico
  24. Pingback: 80mg cialis
  25. Pingback: 50 mg sildenafil
  26. Pingback: propecia discount

Leave a Reply