Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offset not correct in some Android devices #152

Closed
duongdinh98 opened this issue Apr 2, 2024 · 10 comments
Closed

Offset not correct in some Android devices #152

duongdinh98 opened this issue Apr 2, 2024 · 10 comments

Comments

@duongdinh98
Copy link

duongdinh98 commented Apr 2, 2024

Environment

Android (Galaxy Note 10+ is OK but Huawei P20 not correctly cropped)

Description

Offset not correct in some Android devices

Reproducible Demo

 // Crop square image
  let cropOffset = {
    x: 0,
     y: (imgSize.height - imgSize.width) / 2,
  };

  ImageEditor.cropImage(photo.path, {
    offset: cropOffset,
    size: {
      width: imgSize.width,
      height: imgSize.width,
    },
    displaySize: {
      width: imgSize.width,
      height: imgSize.width,
    },
    resizeMode: 'contain',
  }).then(croppedUrl => {
    photo.path = croppedUrl;
    if (cameraStore.mediaList.length < maxImage) {
      cameraStore.appendMediaList([photo]);
      CameraRoll.save(photo.path);
    }
    setLoading(false);
      });
@retyui
Copy link
Collaborator

retyui commented Apr 2, 2024

@duongdinh98 Could you provide video/image demo of what you see on Huawei P20 (I don't have such device for testing)
also an original image, version of @react-native-community/image-editor

@duongdinh98
Copy link
Author

Hi @retyui

I'm using "@react-native-community/image-editor": "^4.2.0",
This is demo video: https://www.youtube.com/shorts/gj6qNuNZVpA

In Galaxy Note 10+ and iPhone with this code above, it cropped square image perfectly like in camera preview screen, but on Huawei P20, it looks like crop without offset in the demo I shared

@retyui
Copy link
Collaborator

retyui commented Apr 3, 2024

@duongdinh98 how do you create imgSize obj ?

@duongdinh98
Copy link
Author

Here is what I did

import { Image as RNImage } from 'react-native';

export const getImageSize = (
  uri: string,
): Promise<{ width: number; height: number }> => {
  return new Promise((resolve, reject) => {
    RNImage.getSize(
      uri,
      (imgWidth, imgHeight) => {
        resolve({ width: imgWidth, height: imgHeight });
      },
      reject,
    );
  });
};

// camera obj from react-native-vision-camera
const photo = await camera.current.takePhoto(takePhotoOptions);
photo.path = `file://${photo.path}`;
const imgSize = await getImageSize(photo.path);

@retyui
Copy link
Collaborator

retyui commented Apr 3, 2024

can you try to swap w & h resolve({ width: imgHeight, height: imgWidth }) ?

@duongdinh98
Copy link
Author

Not work @retyui t

@retyui
Copy link
Collaborator

retyui commented Apr 3, 2024

@duongdinh98 also, can you omit displaySize prop from the params and check again

 displaySize: {
      width: imgSize.width,
      height: imgSize.width,
    },

@duongdinh98
Copy link
Author

@duongdinh98 also, can you omit displaySize prop from the params and check again

 displaySize: {
      width: imgSize.width,
      height: imgSize.width,
    },

Not work, I tried this before

@retyui
Copy link
Collaborator

retyui commented Apr 5, 2024

@duongdinh98 Could you please log the next variables (from Galaxy Note 10+ & Huawei P20 )

const photo = await camera.current.takePhoto(takePhotoOptions);
     // ^^^ - `photo` takePhoto result 
const imgSize = await getImageSize(photo.path);
      //. ^^^ `imgSize` - image size 

@duongdinh98
Copy link
Author

I found issue, my getImageSize() function not correctly work in some Android devices, so it will return incorrect image size => cause crop incorrectly

facebook/react-native#33498

To resolve, I swith to get image size from photo object return by react-native-vision-camera

const photo = await camera.current.takePhoto(takePhotoOptions);

@retyui retyui closed this as completed Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants