我试图通过以下方式获取java中png或jpeg图像的实际宽度和高度:
BufferedImage image = ImageIO.read(new ByteArrayInputStream(byte [] imageContent));
image.getWidth();
image.getHeight();
它适用于风景图像(宽度大于高度),但对于肖像,它会自动旋转图像并交换宽度和高度,如果已完成任何旋转,则不会有任何标记。
另一种解决此问题的方法是直接从字节流中读取jpeg数据:
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();
}
但是,如果使用某些编辑器触摸,上述方法适用于已编辑的图像,但从相机拍摄的图像直接面临同样的问题。
非常感谢指向任何图书馆的指针。