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.