ImageIO不能读取输入文件。

时间:2022-06-30 21:20:31
public static void imRes(String pat) {
        try {
            BufferedImage bckimg = ImageIO.read(new File("c:/s/deneme.jpg"));
            File s = new File(pat);
            BufferedImage im = ImageIO.read(s);
            BufferedImage im1 = resIm(im);
            BufferedImage finIm = mergIm(im1, bckimg);
            ImageIO.write(finIm, "jpg", new File("c:/s/deneme1.jpg"));
        } catch (IOException e) {

            e.printStackTrace();
        }

This is my first post, excuse me if I've done something wrong. This code was running properly untill i try to read an image from the source package. But now it can't read any image. What am I doing wrong? Or is it something about eclipse?

这是我的第一个帖子,对不起,如果我做错了什么。这段代码运行正常,直到我尝试从源包中读取图像。但是现在它不能读取任何图像。我做错了什么?或者它是关于eclipse的?

Exception:

例外:

javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(Unknown Source)
    at imRe.imRes(imRe.java:12)
    at imReTest.main(imReTest.java:6)

Thanks...

谢谢……

1 个解决方案

#1


11  

Change / for \ if you are using windows.

如果你使用的是windows系统,那就改变吧。

A more cross-platform approach would be substitute

一个更跨平台的方法将是替代品。

C: for File.listRoots()[0] and every / for File.separator.

C:对于File.listRoots()[0]和每个/用于文件.分隔符。

Read more on the File api documentation

请阅读更多关于文件api文档的内容。

EDIT

编辑

(I didn't read this line, sorry)

(我没有读这一行,抱歉)

This code was running properly untill i try to read an image from the source package

这段代码运行正常,直到我尝试从源包中读取图像。

In order to get a file from inside your jar package, one must use the getClass().getResource() method.

为了从jar包中获取一个文件,必须使用getClass(). getresource()方法。

Example:

例子:

application-package:
|-Main.java
|-resources
  |-image.jpg

For the above directory structure:

以上目录结构:

BufferedImage im = ImageIO.read(new File(getClass().getResource("/resources/image.jpg").toURI()));

Would do the trick.

就可以做到。

#1


11  

Change / for \ if you are using windows.

如果你使用的是windows系统,那就改变吧。

A more cross-platform approach would be substitute

一个更跨平台的方法将是替代品。

C: for File.listRoots()[0] and every / for File.separator.

C:对于File.listRoots()[0]和每个/用于文件.分隔符。

Read more on the File api documentation

请阅读更多关于文件api文档的内容。

EDIT

编辑

(I didn't read this line, sorry)

(我没有读这一行,抱歉)

This code was running properly untill i try to read an image from the source package

这段代码运行正常,直到我尝试从源包中读取图像。

In order to get a file from inside your jar package, one must use the getClass().getResource() method.

为了从jar包中获取一个文件,必须使用getClass(). getresource()方法。

Example:

例子:

application-package:
|-Main.java
|-resources
  |-image.jpg

For the above directory structure:

以上目录结构:

BufferedImage im = ImageIO.read(new File(getClass().getResource("/resources/image.jpg").toURI()));

Would do the trick.

就可以做到。