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.