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

我的Java应用程序有问题,特别是从我的计算机中的某个位置加载图像。 按照这篇文章我使用BufferedImage和InputFileStream在我的计算机中加载图像首先,我将pic2.jpg图像放入源代码中,没关系。 但是,第二,我把这个图像放到另一个地方(在这种情况下是C:\ ImageTest \ pic2.jpg),Java IDE在行显示IllegalArgumentException“input == null”

return ImageIO.read(in); 

。 我去谷歌搜索确保,我放入代码的链接forms是正确的,所以,我不知道我在哪里做错了….

这是代码:

 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 in. return ImageIO.read(in); } catch (IOException e) { System.out.println("The image was not loaded."); //System.exit(1); } return null; } 

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

。通过FileInputStreamgetResourceAsStream加载文件之间的差异?

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

要从非相对路径读取.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环境,所以希望它能正常工作并且写得正确。

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

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

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

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

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

  //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("f:\\promotions\\4port usb hub.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(); } 

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

 BufferedImage bimg = null; String imageWHS = null; try { File imgObj = new File(imagePath);// Loading an image from local path int fileSize = (int) imgObj.length();// Used to find image size bimg = ImageIO.read(imgObj);// Now the file object send to buffered image, used to find image height and length imageWHS = bimg.getWidth()+"x"+bimg.getHeight()+"x"+Integer.toString(fileSize); } catch (IOException e) { e.printStackTrace(); }