I need to know if an image is landscape oriented or not, for that i´m getting the width and height of a BufferImage but sometimes the values appear as inverted.
This is the code I use to detect if an image is landscape:
public class ProcessImage{
private static final String IMAGE_ORIENTATION_ERROR ="001";
public static void main(String[] args) {
try {
File file = new File("C:\\myFolder\\nothing.jpg");
byte[] fileContent = Files.readAllBytes(file.toPath());
BufferedImage image = createImageFromBytes(fileContent);
validateImage(image);
System.out.println(image.getWidth());
} catch (IOException | ImageOrientationException e) {
e.printStackTrace();
}
}
private static BufferedImage createImageFromBytes(byte[] imageData) {
ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
try {
return ImageIO.read(bais);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/* Here is where i validate if the image is landscape
But Values of image.getWidth() and image.getHeight() appear as inverted.*/
private static void validateImage (BufferedImage image)
throws ImageOrientationException{
//if this happens the picture is NOT landscape
if (image.getWidth() < image.getHeight()) {
throw new ImageOrientationException(IMAGE_ORIENTATION_ERROR,
"error oriented picture");
}
}
}
The problem i'm having is that for some images the values of heigth and width are inverted .
For example, for an image which has a width of 3024 and a height of 4032 , the values of width and height the BufferImage returns are width: 4032 and height:3024. But if i edit the same image in Paint (respecting the original sizes) the BufferImage returns width:3024 and height: 4032 (As it should be).
I have tested with other images with the same width and height and the BufferImage gets the rigth values
Do you have any idea of why is this happening? Is there any way to know if the image is really landscape oriented?
Here is a link with the image which has the problem: http://www.mediafire.com/file/n1a8uhy195zgesd/nothing.jpg/file
Thanks in advance!