理解“ImageIO.read(getClass().getResource(path))" [英] Understanding "ImageIO.read(getClass().getResource(path))"

查看:38
本文介绍了理解“ImageIO.read(getClass().getResource(path))"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于以下代码示例:

My question is about the following code-example:

    public class BufferedImageLoader {

        private BufferedImage image;

        public BufferedImage loadImage(String path) throws IOException {

            image = ImageIO.read(getClass().getResource(path));
            return image;

        }

    }

我查看了 Java-API,在 ImageIO 类中发现了 3 个不同的 read() 方法:

I looked in the Java-API and found 3 different read() methods in the ImageIO Class:

1.:读取(文件输入)

1.: read(File input)

2.: 读取(ImageInputStream 流)

2.: read(ImageInputStream stream)

3.: 读取(InputStream 输入)

3.: read(InputStream input)

4.: 读取(输入网址)

4.: read(URL input)

我的问题是:这个例子中使用了这四种方法中的哪一种?我有点困惑,因为在示例中

My question is: Which of them four methods is used in this example? I'm a little bit confused, because in the example stands

read(getClass().getResource(path));

getClass()"在这里返回BufferedImageLoader",对吗?然后我们调用方法read(getClass().getResource(path))",这个方法必须站在BufferedImageLoader Class中,但事实并非如此!

"getClass()" returns here "BufferedImageLoader", right? Then we call the method "read(getClass().getResource(path))", which must stand in the BufferedImageLoader Class, but this is not the case!

我错在哪里?

推荐答案

getClass().getResource(path)) 返回一个 URL,所以在这种情况下,它将使用 ImageIO.read(URL)

getClass().getResource(path)) returns a URL, so in this case, it would using ImageIO.read(URL)

另外,如果你使用了Class#getResourceAsInputStream,它会返回一个InputStream,这意味着它将使用ImageIO.read(InputStream) 代替

In addition, if you used Class#getResourceAsInputStream, it would return an InputStream, meaning it would be using ImageIO.read(InputStream) instead

这篇关于理解“ImageIO.read(getClass().getResource(path))"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆