0

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!

Vanesa C.
  • 1
  • 1
  • Images can have metadata in them. One thing that is sometimes stored in metadata is the orientation of the image. What could be happening is that Paint knows to honor the orientation specified in the file, and for the problem image, that orientation is different from how the data is stored in the file. So Paint is rotating the image before displaying it, because of the metadata, but when you read the image into a BufferImage, you're getting the pixels the way they're actually stored in the file. Just a guess....a possibility. – CryptoFool Oct 29 '20 at 23:52
  • Take a look at this: https://stackoverflow.com/questions/5905868/how-to-rotate-jpeg-images-based-on-the-orientation-metadata – CryptoFool Oct 30 '20 at 00:00
  • Thanks @Steve! That post is really usefull! – Vanesa C. Oct 30 '20 at 12:39

0 Answers0