numandev1 / react-native-compressor

🗜️Compress Image, Video, and Audio same like Whatsapp 🚀✨
MIT License
938 stars 87 forks source link

Images taken from camera being rotated 90 degrees counter-clockwise #114

Closed luizaugustoventura closed 1 year ago

luizaugustoventura commented 2 years ago

Current behavior

Whenever I compress an image taken straight from the camera (in portrait mode) the image gets rotated 90 degrees counter-clockwise after compressed. This happens on the physical device (in my case, a Samsung Galaxy S10 Lite), but not on the Android Emulator.

I am using react-native-image-picker (version 4.7.3) to take the images and then compressing them using react-native-compressor before upload. Using react-native-image-picker itself does not rotate the image. The problem occurs specifically on android after I use the compressor to compress the image taken from react-native-image-picker. Here is the code:

import { Image as ImageCompressor } from 'react-native-compressor';
import { launchCamera } from 'react-native-image-picker';

addImageFromCamera = async () => {
    const result = await launchCamera({
        mediaType: 'photo',
        saveToPhotos: true,
    });

    if (result.didCancel) {
        return;
    }

    const compressedImage = await ImageCompressor.compress(result.assets[0].uri, { autoCompress: true });

    return compressedImage;
}

The output is great! The image's size is significantly reduced and the quality is still good. It is just the orientation issue bothering the users on Android(only).

Expected behavior

Compress the image keeping its original orientation.

Platform

React Native Version

0.61.5

React Native Compressor Version

0.5.17

Reproducible Steps And Demo

ezgif-4-c750eb23cd

github-actions[bot] commented 2 years ago

👋 @luizaugustoventura Thanks for opening your issue here! If you find this package useful hit the star🌟!

luizaugustoventura commented 2 years ago

Just noticed something! The issue seems to be related with Samsung cameras. Just tested on a Motorola and image did not get rotated.

Is there any configuration I can do to my phone's camera to get this right?

iphonic commented 2 years ago

Just noticed something! The issue seems to be related with Samsung cameras. Just tested on a Motorola and image did not get rotated.

Is there any configuration I can do to my phone's camera to get this right?

Don't think it's only Samsung, I am checking it on OnePlus, same. it rotates.

Nekuin commented 2 years ago

Check out the issues on react-native-image-picker regarding rotation stuff, I have a feeling this library doesn't do anything to the rotation. I've had struggles with this exact same issue, and it doesn't help that the image gallery shows the picture in the "correct rotation" while it's sideways somewhere else. You can also try to look into EXIF data of the image, which should contain information about the images orientation.

Hope any of this helps and good luck!

luizaugustoventura commented 2 years ago

Check out the issues on react-native-image-picker regarding rotation stuff, I have a feeling this library doesn't do anything to the rotation. I've had struggles with this exact same issue, and it doesn't help that the image gallery shows the picture in the "correct rotation" while it's sideways somewhere else. You can also try to look into EXIF data of the image, which should contain information about the images orientation.

Hope any of this helps and good luck!

Well, you know what's even more curious? I went to the Microsoft Teams website and tried to update my profile picture with a photo I've sent from my Samsung phone via Bluetooth. And guess what...... the photo got rotated there as well. Now I'm really starting to think it's something related to the picture's metadata or something of this nature.

How do you look into the EXIF data of the image? I don't know any means to do that, as it is in the native Java code in node_modules.

Nekuin commented 2 years ago

Check out the issues on react-native-image-picker regarding rotation stuff, I have a feeling this library doesn't do anything to the rotation. I've had struggles with this exact same issue, and it doesn't help that the image gallery shows the picture in the "correct rotation" while it's sideways somewhere else. You can also try to look into EXIF data of the image, which should contain information about the images orientation. Hope any of this helps and good luck!

Well, you know what's even more curious? I went to the Microsoft Teams website and tried to update my profile picture with a photo I've sent from my Samsung phone via Bluetooth. And guess what...... the photo got rotated there as well. Now I'm really starting to think it's something related to the picture's metadata or something of this nature.

How do you look into the EXIF data of the image? I don't know any means to do that, as it is in the native Java code in node_modules.

Looking at the react-native-image-picker options you can get the exif data with the image you take with the camera or select from image library by setting includeExtra as true. Copy pasting a bit of your code here as an example: const result = await launchCamera({ mediaType: 'photo', saveToPhotos: true, includeExtra: true });

sadeghhosseini commented 2 years ago

I believe the problem is with metadata of the image being deleted during the compression, I had the same problem with sharp library which got resolved using withMetadata() method, which reserved the metadata of the image. Does this library have a similar option for reserving metadata?

uniquejava commented 2 years ago

+1, rotation happens on my colleague's Xiaomi K40S but not on my K50.

puckj commented 2 years ago

I'm still facing the same issue. (tested on iPhone 6)

Marcuspo commented 2 years ago

+1

hotaryuzaki commented 2 years ago

i have same issue, and it's ordinary upload from file with portrait orientation. i try in 2 devices, Oneplus okay but Samsung. i think the problem from metadata of the image in the device.

any solution?

hb-webdev commented 1 year ago

Looking forward to fixing rotating images on Android (presumably caused by deleted metadata)

jeemercado commented 1 year ago

Looking forward to a fix. After investigation it's probably due to losing the "orientation" data after compression on Android.

fakfa19 commented 1 year ago

If someone use react-native-image-crop-picker please check this issue https://github.com/ivpusic/react-native-image-crop-picker/issues/1696#issuecomment-1183505899

luizaugustoventura commented 1 year ago

Check out the issues on react-native-image-picker regarding rotation stuff, I have a feeling this library doesn't do anything to the rotation. I've had struggles with this exact same issue, and it doesn't help that the image gallery shows the picture in the "correct rotation" while it's sideways somewhere else. You can also try to look into EXIF data of the image, which should contain information about the images orientation. Hope any of this helps and good luck!

Well, you know what's even more curious? I went to the Microsoft Teams website and tried to update my profile picture with a photo I've sent from my Samsung phone via Bluetooth. And guess what...... the photo got rotated there as well. Now I'm really starting to think it's something related to the picture's metadata or something of this nature. How do you look into the EXIF data of the image? I don't know any means to do that, as it is in the native Java code in node_modules.

Looking at the react-native-image-picker options you can get the exif data with the image you take with the camera or select from image library by setting includeExtra as true. Copy pasting a bit of your code here as an example: const result = await launchCamera({ mediaType: 'photo', saveToPhotos: true, includeExtra: true });

Perfect solution, working nicely on the aforementioned Samsung device 🤩️🤩️🤩️

I'm sorry for the late answer. I moved to another company and no longer had access to the code attached above. But I tested your solution in the following environment:

Platform

React Native Version

0.70.6

React Native Compressor Version

1.6.1

React Native Image Picker Version

5.0.1

Everything worked exactly as I expected, so thank you so much for your answer and once again sorry for being so late. As I said, I'm no longer working on that project.

The only problem I faced is a compatibility issue with some of the latest React Native versions. I had to follow this workaround on #132. Otherwise, a runtime error would happen.

numandev1 commented 1 year ago

🙏 sorry for a long ago I was not maintaining due to the huge office workload, hopefully, I am going to start fixing bugs on a weekend basis, I will fix at least 2 issues on the weekend, for priority bugs you can join the discord channel https://discord.gg/dtYzk8sp where we can discuss which bug is most important

numandev1 commented 1 year ago

@luizaugustoventura @jeemercado @fakfa19 @hb-webdev @iphonic do you have any device where it is reproducible i wanna fix it by testing it on your device

hb-webdev commented 1 year ago

@numandev1 I think it's reproducible on any android phone. I have one that I could test on. Should I use #170 and test?

numandev1 commented 1 year ago

@hb-webdev If it is reproducible on your side then we can test it on Zoom or Teamviewer and fix it as it is not reproducible on my side

numandev1 commented 1 year ago

is anyone having this issue on his mobile device, if yes then we can both debug this issue on Zoom?👀

hb-webdev commented 1 year ago

i'll try to reproduce soon on latest RN and let you know

numandev1 commented 1 year ago

@hb-webdev ?

hb-webdev commented 1 year ago

I'm unable to replicate the issue. The original and compressed images are both oriented the same on my Galaxy S9. They used to rotate 90 degrees on the same device after compression

I might have been using React Native Camera before, but now I am using React Native Vision Camera. Maybe that affects it...

I will try again with React Native Image Picker like OP used

hb-webdev commented 1 year ago

The rotation does not occur with RN 0.72 + RN Image Picker (5.0.2).

The rotation used to happen on my Galaxy S9 from this compression module, but no longer with RN Vision Camera or RN Image Picker on RN 0.72...

I think this might be able to be considered resolved for now

ezgif com-video-to-gif (1)

numandev1 commented 1 year ago

@hb-webdev thanks a lot ❤❤

luisresendiz0 commented 11 months ago

+1 "react-native-image-picker": "^4.10.3" "react-native-compressor": "^1.6.1"

numandev1 commented 11 months ago

@luisresendiz0 can you check the latest version?

luisresendiz0 commented 11 months ago

Yep, It's the first thing I thought of. They are already updated but the issue continues.

I'm currently on:

"react-native-compressor": "^1.8.21",
"react-native-image-picker": "^7.0.2",

And my code includes includeExtra: true prop:

try {
      const result = await launchCamera({
        mediaType: 'photo',
        presentationStyle: 'fullScreen',
        saveToPhotos: true,
        includeExtra: true
      });

      if (!result.assets) {
        throw new Error(
          result.errorMessage || 'No se obtuvieron imagenes de la camara',
        );
      }

      const asset = result.assets[0];

      console.log("asset", asset);

      const uri = await ImageCompressor.compress(`${asset.uri}`, {
        compressionMethod: 'auto'
      });

Issue:

https://github.com/numandev1/react-native-compressor/assets/80994311/f7e7d155-3633-4606-a46e-a7b0a894b464

@luisresendiz0 can you check the latest version?

numandev1 commented 11 months ago

@luisresendiz0 can you check 1.8.22 new version?

talaikis commented 9 months ago

+1

"react-native-compressor": "1.8.x-1,8.23", "react-native-image-picker": "7.1.0",

Smigi33 commented 9 months ago

I got the same issue when i tak picture via react-native-vision-camera and set compress to manual (on auto it's work normal). Pictures from image picker are normal ;/

Iphone 7 iOS 15.8 and any android device

react-native 0.71.8 "react-native-compressor": "^1.8.23", "react-native-vision-camera": "^2.15.4", "react-native-image-picker": "^5.6.0",