如何将图像URL提供给createImageInputStream方法

时间:2013-09-05 12:44:24

标签: java image objectinputstream

我有一个图片网址,

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();
}

P.S。取自:Java/ImageIO getting image dimensions without reading the entire file?

1 个答案:

答案 0 :(得分:4)

试试这个:

    String url = "http://XXXXXXX/img_High-Octane-Sauce-Company-JP5-400x1024.jpg";
    InputStream urlInputHere = new URL(url).openStream();
    ImageInputStream in = ImageIO.createImageInputStream( urlInputHere );