0

I am trying to get actual width and height of a png or jpeg image in java via following ways:

BufferedImage image = ImageIO.read(new ByteArrayInputStream(byte [] imageContent));
image.getWidth();
image.getHeight();

It works as as desired for landscape image ( width greater than height) however for portrait it automatically rotates image and interchange width and height without any flag to get if any rotation has been done.

Another approach taken to cater this issue was reading jpeg data directly from byte stream:

    while (c3 == 255) {
        int marker = is.read();
        int len = readInt(is,2,true);
        if (marker == 192 || marker == 193 || marker == 194) {
            is.skip(1);
            height = readInt(is,2,true);
            width = readInt(is,2,true);
            mimeType = "image/jpeg";
            break;
        }
        is.skip(len - 2);
        c3 = is.read();
}

However above approach works for edited images if touched with some editor, however images taken from camera directly faces same issue.

Pointers to any library on same are highly appreciated.

tarunkumar
  • 861
  • 2
  • 15
  • 30
  • possible duplicate of [How to rotate JPEG images based on the orientation metadata?](http://stackoverflow.com/questions/5905868/how-to-rotate-jpeg-images-based-on-the-orientation-metadata) – RealSkeptic Jul 14 '15 at 04:11
  • 1
    Neither approach should lie to you. Is it possible that the image isn't actually rotated? – user253751 Jul 14 '15 at 04:16
  • Check this link it includes links to explanations and code samples [landscape or portrait][1] [1]: http://stackoverflow.com/questions/2601587/how-to-tell-if-a-photo-was-taken-in-landscape-or-portrait-jpeg-net-metadata-or – liorlis Jul 14 '15 at 05:00
  • @Realskeptic Yes I have already saw the link you mentioned and tried to used that library. Yes it did worked for images captured from camera. However if an image has been edited with editor say photoscape this library fails to extract EXIF data probably its editor removing this info. I am just wondering as i just need basic property of Image width and height and I cannot restrict user from uploading any sort of file just wondering may be i am missing very basic thing. – tarunkumar Jul 14 '15 at 14:32
  • 1
    If there is no exif information, then there is no orientation. This means it's a rotated image, and that's how it will be displayed in a browser as well. It is therefore a landscape image. The fact that as a human who looks at it you know that it *should* have been a portrait and needs to be rotated is something that a computer can't tell. – RealSkeptic Jul 14 '15 at 14:38
  • @immibis I am doing testing for images created png and jpg for both mode landscape and portrait in gimp as well as some images clicked from my camera directly. Now while debugging I figured out that library suggested by Realskeptic gives correct orientation for camera images but it fails to get data for any edited image. However second approach do works for any generated / edited image while fails for a camera image. – tarunkumar Jul 14 '15 at 14:40
  • @Realskeptic no matter an image contains exif data or not any OS say win/ mac is able to show correct thumbnails as per its orientation in correct order. Also same is applicable if I check properties of image via OS. I always get correct width and height no matter of image origin. Even if any API can give me correct width and height of image I can myself determine about orientation of image without reading its EXIF data. – tarunkumar Jul 14 '15 at 14:47
  • Did you check the thumbnail created from your image after the editor removed its exif information? – RealSkeptic Jul 14 '15 at 14:55
  • @Realskeptic Yes I did, in fact I did created 2 blank images (png, jpeg) using GIMP editor so it should not have any EXIF data (API you mentioned fails here). Even though Mac is able to show its correct thumbnail as well as I am able to get correct width and height. while java ImageIO.read at the first step is giving me rotated image. Since I was looking to get basic width and height of image not some very detailed info like photo aperture etc, it makes me feels like I am missing something very basic. – tarunkumar Jul 14 '15 at 15:06
  • I'm wondering, then, where you get the byte array. – RealSkeptic Jul 14 '15 at 16:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83240/discussion-between-tarunkumar-and-realskeptic). – tarunkumar Jul 14 '15 at 16:49

0 Answers0