ImageIO无法读取输入文件 [英] ImageIO can't read input file
问题描述
public static void imRes(String pat){
try {
BufferedImage bckimg = ImageIO.read(new File(c:/s/deneme.jpg ));
文件s = new File(pat);
BufferedImage im = ImageIO.read(s);
BufferedImage im1 = resIm(im);
BufferedImage finIm = mergIm(im1,bckimg);
ImageIO.write(finIm,jpg,新文件(c:/s/deneme1.jpg));
} catch(IOException e){
e.printStackTrace();
}
这是我的第一篇文章,如果我做错了,对不起。此代码正常运行,直到我尝试从源包中读取图像。但现在看不到任何图像。我究竟做错了什么?或者是关于eclipse的东西?
异常:
javax。 imageio.IIOException:无法读取输入文件!
at javax.imageio.ImageIO.read(Unknown Source)
at imRe.imRes(imRe.java:12)
at imReTest.main(imReTest.java:6)
谢谢... 解决方案> 如果您使用的是Windows,请更改 /
\
。
更多的跨平台方法将替代
C:
File.listRoots()[0]
每个 /
File.separator
。
阅读更多关于文件api文档
编辑
(我没有看到这行,对不起)
这段代码正常运行,直到我尝试从源代码中读取图像包
为了从jar包中获取文件,必须使用 getClass()。的getResource ()
方法。
示例:
application-package:
| -Main.java
| -resources
| -image.jpg
对于上述目录结构:
BufferedImage im = ImageIO.read(new File 。的getClass()的getResource( /资源/ image.jpg的)toURI()))。
会做的伎俩。
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?
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...
解决方案 Change /
for \
if you are using windows.
A more cross-platform approach would be substitute
C:
for File.listRoots()[0]
and every /
for File.separator
.
Read more on the File api documentation
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.
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.
这篇关于ImageIO无法读取输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文