2

我有一个图片网址,例如,

String url = "http://XXXXXXX/img_High-Octane-Sauce-Company-JP5-400x1024.jpg";

我需要将此作为方法的输入,

ImageInputStream in = ImageIO.createImageInputStream( urlInputHere );

所以我可以在不使用图像缓冲区的情况下读取图像的宽度和高度。谁能告诉我该怎么做?

这是我阅读图像的方式

ImageInputStream in = ImageIO.createImageInputStream(resourceFile);
try {
    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();
        }
    }
} finally {
    if (in != null) in.close();
}

PS 取自:Java/ImageIO 在不读取整个文件的情况下获取图像尺寸?

4

1 回答 1

5

尝试这个:

    String url = "http://XXXXXXX/img_High-Octane-Sauce-Company-JP5-400x1024.jpg";
    InputStream urlInputHere = new URL(url).openStream();
    ImageInputStream in = ImageIO.createImageInputStream( urlInputHere );
于 2013-09-05T12:51:53.547 回答