Java 通过 BufferedImage 从文件路径加载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19447104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Load image from a filepath via BufferedImage
提问by Lup
I have a problem with Java application, particular in loading a image from a location in my computer.
我在使用 Java 应用程序时遇到问题,特别是在从计算机中的某个位置加载图像时。
Following this postI used a BufferedImage
and a InputFileStream
to load an image on my computer. First, I put the image (pic2.jpg
) into the source code and that is working. However, if I put the image to another place (let's say C:\\ImageTest\pic2.jpg
), Java IDE show me an IllegalArgumentException
在这篇文章之后,我使用 aBufferedImage
和 aInputFileStream
在我的计算机上加载图像。首先,我将图像 ( pic2.jpg
) 放入源代码中,然后就可以工作了。但是,如果我将图像放到另一个地方(比方说C:\\ImageTest\pic2.jpg
),Java IDE 会显示一个IllegalArgumentException
return ImageIO.read(in);
here is the code:
这是代码:
public class MiddlePanel extends JPanel {
private BufferedImage img;
public MiddlePanel(int width) {
//img = getImage("pic2.jpg");
img = getImage("C:\ImageTest\pic2.jpg");
this.setPreferredSize(new Dimension(800,460));
}
public void paintComponent(Graphics g) {
// ...
}
private BufferedImage getImage(String filename) {
// This time, you can use an InputStream to load
try {
// Grab the InputStream for the image.
InputStream in = getClass().getResourceAsStream(filename);
// Then read it.
return ImageIO.read(in);
} catch (IOException e) {
System.out.println("The image was not loaded.");
//System.exit(1);
}
return null;
}
}
采纳答案by Andrew Thompson
getResource
& getResourceAsStream
do not work with file paths, but paths relative the code base. If the code base is C:
then a relative path that would locate the resource is /ImageTest/pic2.jpg
.
getResource
&getResourceAsStream
不适用于文件路径,而是相对于代码库的路径。如果代码库是C:
定位资源的相对路径是/ImageTest/pic2.jpg
.
..difference between load file by
FileInputStream
andgetResourceAsStream
?
..load file by
FileInputStream
和getResourceAsStream
?
One major difference is that the getResource..
will work with a resource inside a Jar, which is no longer a File
. Therefore FileInputStream
cannotbe used to access such a resource.
一个主要区别是getResource..
将使用 Jar 中的资源,而 Jar 不再是File
. 因此FileInputStream
不能用于访问这样的资源。
回答by Tafari
To read an .jpg file from non-relative path you could use this:
要从非相对路径读取 .jpg 文件,您可以使用:
BufferedImage img = null;
try
{
img = ImageIO.read(new File("C:/ImageTest/pic2.jpg")); // eventually C:\ImageTest\pic2.jpg
}
catch (IOException e)
{
e.printStackTrace();
}
I do not have any Java environment at the moment, so hope it works and is written correctly.
我目前没有任何 Java 环境,所以希望它可以正常工作并正确编写。
回答by Keerthivasan
You cannot use Class#getResource(String)
or Class#getResourceAsStream(String)
in this case. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream(java.lang.String)
.
在这种情况下,您不能使用Class#getResource(String)
或Class#getResourceAsStream(String)
。搜索与给定类相关联的资源的规则由类的定义类加载器实现。此方法委托给此对象的类加载器。如果此对象由引导类加载器加载,则该方法委托给ClassLoader.getSystemResourceAsStream(java.lang.String)
。
Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:
在委托之前,使用以下算法从给定的资源名称构造一个绝对资源名称:
If the name begins with a /
(\u002f
), then the absolute name of the resource is the portion of the name following the /
.
Otherwise, the absolute name is of the following form:
modified_package_name/name
如果名称以/
( \u002f
)开头,则资源的绝对名称是名称后面的部分/
。否则,绝对名称采用以下形式: modified_package_name/name
Where the modified_package_name is the package name of this object with /
substituted for .
(\u002e
).
其中 modified_package_name 是此对象的包名,/
替换为.
( \u002e
)。
Generally, it is not a good thing to hard code the system location of your resources in your code. The neat and clean way is to put your resources in your classpath and access them. Hope this clarifies why it's not working
通常,在代码中硬编码资源的系统位置并不是一件好事。整洁干净的方法是将您的资源放在类路径中并访问它们。希望这能澄清为什么它不起作用
回答by Charles
//This code snippet read an image from location on the computer and writes it to a different location on the disk
try {
byte[] imageInByte;
BufferedImage imageOnDisk = ImageIO.read(new File("C:\ImageTest\pic2.jpg"));
//Create a ByteArrayOutputStrea object to write image to
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//Write the image to the OutputStream
ImageIO.write(imageOnDisk, "jpg", baos);
baos.flush();
//Initialise the byte array object with the image that was written to the OutputStream
imageInByte = baos.toByteArray();
baos.close();
// convert byte array back to BufferedImage
InputStream in = new ByteArrayInputStream(imageInByte);
BufferedImage bImageFromConvert = ImageIO.read(in);
//write the image to a new location with a different file name(optionally)
ImageIO.write(bImageFromConvert, "jpg", new File(
"c:\index.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
回答by Harjinder Banga
To find the image Width, height and size
查找图像的宽度、高度和大小
BufferedImage image = null;
int imageWidth = -1;
int imageHeight = -1;
int fileSize = -1;
try {
File imageFile = new File(imagePath);
int fileSize = (int) imageFile.length();
image = ImageIO.read(imageFile); // Reading the Image from the file system
imageWidth = image.getWidth();
imageHeight = image.getHeight();
} catch (IOException e) {
e.printStackTrace();
}