通过BufferedImage从文件路径加载图像

问题描述 投票:10回答:5

我的Java应用程序有问题,特别是从我的计算机中的某个位置加载图像。

this post之后,我使用BufferedImageInputFileStream在我的计算机上加载图像。首先,我将图像(pic2.jpg)放入源代码中,这是有效的。但是,如果我把图像放到另一个地方(让我们说C:\\ImageTest\pic2.jpg),Java IDE会给我看一个IllegalArgumentException

return ImageIO.read(in);

这是代码:

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;
    }
}
java image embedded-resource bufferedimage
5个回答
5
投票

getResourcegetResourceAsStream不适用于文件路径,而是相对于代码库的路径。如果代码库是C:,则定位资源的相对路径是/ImageTest/pic2.jpg

.. FileInputStreamgetResourceAsStream加载文件之间的差异?

一个主要的区别是,getResource..将使用Jar内的资源,这不再是File。因此,FileInputStream不能用于访问这样的资源。


13
投票

要从非相对路径读取.jpg文件,您可以使用:

BufferedImage img = null;

try 
{
    img = ImageIO.read(new File("C:/ImageTest/pic2.jpg")); // eventually C:\\ImageTest\\pic2.jpg
} 
catch (IOException e) 
{
    e.printStackTrace();
}

我目前没有任何Java环境,所以希望它能正常工作并且写得正确。


4
投票

在这种情况下,你不能使用Class#getResource(String)Class#getResourceAsStream(String)。搜索与给定类关联的资源的规则由类的定义类加载器实现。此方法委托给此对象的类加载器。如果此对象由引导类加载器加载,则该方法委托给ClassLoader.getSystemResourceAsStream(java.lang.String)

在委派之前,使用此算法从给定资源名称构造绝对资源名称:

如果名称以/\u002f)开头,则资源的绝对名称是/后面的名称部分。否则,绝对名称的格式如下:modified_pa​​ckage_name / name

其中modified_pa​​ckage_name是此对象的包名称,其中/替换为.\u002e)。

通常,在代码中对资源的系统位置进行硬编码并不是一件好事。干净利落的方法是将您的资源放在类路径中并访问它们。希望这可以澄清为什么它不起作用


0
投票

查找图像宽度,高度和大小

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

0
投票
//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();
}
© www.soinside.com 2019 - 2024. All rights reserved.