teslamotors / react-native-camera-kit

A high performance, easy to use, rock solid camera library for React Native apps.
MIT License
2.42k stars 577 forks source link

After taking the photo and converting it to Base64, it rotates 90 degrees on some Android devices #643

Closed olcaneristi closed 4 months ago

olcaneristi commented 4 months ago

Describe the bug When I convert the photo I took on Android devices such as Oppo and Samsung to Base64 with react-native-fs, then send to Backend as base64, it rotates 90 degrees.

To Reproduce Steps to reproduce the behavior:

1- Setup the with props like ref, cameraType, 2- Configure the cameraRef.capture, 3- Convert uri to base64 with react-native-fs

  const takePicture = async () => {
    if (cameraRef) {
      const photo = await cameraRef.capture();

      /* output:
    "path": "/data/data/com.xxxxx/cache/ckcap5681401830148856910.jpg",
    "height": 1012,
    "name": "ckcap5681401830148856910.jpg",
    "width": 720,
    "id": "/data/data/com.xxxxxxx/cache/ckcap5681401830148856910.jpg",
    "uri": "file:///data/data/com.xxxxxx/cache/ckcap5681401830148856910.jpg"
      */

   const frontBase64 = await RNFS.readFile(uri, 'base64');
      let newUri = '';
      await ImageResizer.createResizedImage(`data:image/jpeg;base64,${frontBase64}`, 1600, 1600, 'JPEG', 90).then(
        (response) => { newUri = response?.uri; })
         .catch((err) => { console.log({ err }); });

      const frontBase64Resized = await RNFS.readFile(newUri, 'base64');
      setImage(frontBase64Resized);

      /* output:
       /9j/4QPaRXhpZgAATU0AKgAAAAgADAEbAAUAAAABAAAAngEaA....
       */
    }
  };

 <Camera
    ref={(ref) => (cameraRef = ref)}
    cameraType={CameraType.Back} // front/back(default)
    style={styles.preview}
    showFrame={false}
    scanBarcode={false}
    zoomMode="off"
/>

Expected behavior 1- Taking photo on Portrait mode, 2- Convert to base64 with react-native-fs, 3- Get Base64 version of taken photo with no rotation.

Screenshots Screenshot 2024-02-04 at 02 00 03

Screenshot 2024-02-04 at 02 03 31

Smartphone (please complete the followig information):

Additional context When I capture the photo and convert to base64 with react-native-fs, then send to Backend as a base64 format, it rotates to 90 degree. I add the screenshots above.

olcaneristi commented 4 months ago

Ah, I made a huge mistake. Instead of resizing the URI directly, I resized the base64 format with react-native-image-resizer. That's why the base64 format produced by RNFS rotates 90 degrees after resize and gets corrupted. There was no problem with react-native-camera-kit. It's entirely my fault. Please ignore this issue.