Java / ImageIO获取图像尺寸而不读取整个文件?

有没有一种方法可以获取图像尺寸而无需读取整个文件?

URL url=new URL(<BIG_IMAGE_URL>);

BufferedImage img=ImageIO.read(url);

System.out.println(img.getWidth()+" "+img.getHeight());

img=null;

回答:

try(ImageInputStream in = ImageIO.createImageInputStream(resourceFile)){

final Iterator<ImageReader> readers = ImageIO.getImageReaders(in);

if (readers.hasNext()) {

ImageReader reader = readers.next();

try {

reader.setInput(in);

return new Dimension(reader.getWidth(0), reader.getHeight(0));

} finally {

reader.dispose();

}

}

}

以上是 Java / ImageIO获取图像尺寸而不读取整个文件? 的全部内容, 来源链接: utcz.com/qa/412938.html

回到顶部