0

I have a Java Swing application that for business reason has to load image

the application working fine, it is in production, all the images are correctly loaded but we receive a bug related to an image that is loaded upside down (rotated by 180 degrees)

As first step I started to load the image in my application and then, the application was effectively loaded rotated.

I was thinking that the issue was related to same of our code: same time we do a minimal manipulation on the BufferedImage.

Then I create a very stupid test, with just pure java code without any infrastructure code:

public class FlipImage extends JDialog {

private final JPanel contentPanel = new ImagePanel();

public static void main(String[] args) {
    try {
        FlipImage dialog = new FlipImage();
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
        
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public FlipImage() {
    setBounds(100, 100, 450, 300);
    getContentPane().setLayout(new BorderLayout());
    contentPanel.setLayout(new FlowLayout());
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    {
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
        getContentPane().add(buttonPane, BorderLayout.SOUTH);
        {
            JButton okButton = new JButton("OK");
            okButton.setActionCommand("OK");
            buttonPane.add(okButton);
            getRootPane().setDefaultButton(okButton);
        }
        {
            JButton cancelButton = new JButton("Cancel");
            cancelButton.setActionCommand("Cancel");
            buttonPane.add(cancelButton);
        }
    }
}

private class ImagePanel extends JPanel{

    private BufferedImage image;

    public ImagePanel() {
       try {                
          image = ImageIO.read( new File("C:\\Users\\Alessandro\\Desktop\\flip_little.jpg"));
       } catch (IOException ex) {
       }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
    }
}
}

To my big surprise the image is really loaded rotated. But if I open the same image with Windows Paint or any other tool, the other tools correctly load the image.

From the perspective of the code then, there is not issue.

The problem is related to the image itself.

Has someone has faced the same issue, and found the root cause of this problem?

May be is an issue in the implementation of the encoding in Java, I'm not an expert in image manipulation - also browsing on internet I didn't find much.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Alesky
  • 31
  • 2
  • 4
    This might be a problem with the image actually being rotated and [EXIF](https://en.wikipedia.org/wiki/Exif) metadata containing information about that orientation. As far as I know [basic Java image routines don't handle exif at all](https://stackoverflow.com/questions/47657302/does-imageio-read-take-exif-orientation-metadata-into-account) (that's one of the reasons for switching to more advanced libraries in a previous job). Try to have a look at the metadata of that image by using an EXIF viewer. – Thomas May 27 '21 at 08:03
  • Does this question help? https://stackoverflow.com/questions/5905868/how-to-rotate-jpeg-images-based-on-the-orientation-metadata – Guillaume May 27 '21 at 08:04
  • 1
    Agree, very likely Exif issue. I've made [a utility for ImageIO that reads a JPEG and rotates the image according to the Exif orientation](https://github.com/haraldk/TwelveMonkeys/blob/4e9fa9442c61e3c4765bb41017baa5744404aa2a/contrib/src/main/java/com/twelvemonkeys/contrib/exif/EXIFUtilities.java#L77) that might be useful. – Harald K May 27 '21 at 09:52
  • `setBounds(100, 100, 450, 300);` This is no better than a guess. If it's the 'right' size on your computer it will be wrong on most others. Instead `pack()` the dialog to the exact size needed - anywhere it appears. – Andrew Thompson May 27 '21 at 14:00
  • BTW - can you upload the image somewhere we can access it? I expect the comment of @Thomas has nailed it, but if we have access to the image, we might be able to confirm your result (& some will be clever enough to access the EXIF info). – Andrew Thompson May 27 '21 at 14:06

0 Answers0