0

I am using java and vaadin 14 as a web framework. In vaadin i am using Vaadin Uploader component to upload an image from camera or image gallery on android phone . When i choose an image from gallery then it's uploaded and shows normally. But when i am trying to upload an image from phone camera in particular device(samsung) then uploaded image rotated automatically That means, uploaded image shows in portrait if captured in landscape and if captured in portrait it shows landscape. how to solve it?

Note: It does not happen in all android devices. Only in samsung android devices like samsung s7.

here is my code-

import com.vaadin.flow.component.upload.SucceededEvent;
import com.vaadin.flow.component.upload.Upload;

private void fileUploaderInit() {
        uploader = new Upload(buffer);
        uploader.setMaxFiles(1);
        uploader.setDropAllowed(false);
        uploader.setAcceptedFileTypes("image/*");
        uploader.getElement().removeProperty("capture");
        uploader.setUploadButton(image);

        uploader.addSucceededListener(event -> {
            succeedEvent = event;
            saveUploadedImage();
            setUserImage();
        });
        uploader.getElement().addEventListener("file-abort", remove -> {
            succeedEvent = null;
        });
        uploadLayout.add(uploader);
    }

    private void saveUploadedImage() {
        if (succeedEvent != null && userEntity != null) {
            addData();
            succeedEvent = null;
            Notification.show("Image saved successfully.",2000, Position.MIDDLE);
            userEvent.fire(new UserEvent(userEntity,false));
        }
        uploader.getElement().setPropertyJson("files", Json.createArray());
    }

1 Answers1

0

I usually use Retrofit for uploading image and files but it is not related uploading service. It just is related to camera image processing. Let me show you my code to fix rotation issue.

public Bitmap getImageFromPath(String photoPath) {
    Bitmap bitmap = BitmapFactory.decodeFile(photoPath);
    Bitmap rotatedBitmap = bitmap;
    ExifInterface ei = null;
    try {
        ei = new ExifInterface(photoPath);

        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

        switch(orientation) {

            case ExifInterface.ORIENTATION_ROTATE_90:
                rotatedBitmap = rotateImage(bitmap, 90);
                break;

            case ExifInterface.ORIENTATION_ROTATE_180:
                rotatedBitmap = rotateImage(bitmap, 180);
                break;

            case ExifInterface.ORIENTATION_ROTATE_270:
                rotatedBitmap = rotateImage(bitmap, 270);
                break;

            case ExifInterface.ORIENTATION_NORMAL:
            default:
                rotatedBitmap = bitmap;
        }
    } catch (IOException e) {

        e.printStackTrace();

    }
    return rotatedBitmap;

}

After taking image from Camera, you save it to android directory and you should detect its orientation and rotate like this code. Good luck.

KpStar
  • 122
  • 7