ImageIO 无法读取输入文件 [英] ImageIO can't read input file
问题描述
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?
异常:
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)
谢谢...
推荐答案
如果您使用的是 Windows,请将 /
更改为 \
.
Change /
for \
if you are using windows.
更跨平台的方法将被替代
A more cross-platform approach would be substitute
C:
用于 File.listRoots()[0]
和每个 /
用于 File.separator
.
C:
for File.listRoots()[0]
and every /
for File.separator
.
阅读有关 文件 API 文档
编辑
(我没看过这行,抱歉)
(I didn't read this line, sorry)
此代码运行正常,直到我尝试从源包中读取图像
This code was running properly untill i try to read an image from the source package
为了从 jar 包中获取文件,必须使用 getClass().getResource()
方法.
In order to get a file from inside your jar package, one must use the getClass().getResource()
method.
示例:
application-package:
|-Main.java
|-resources
|-image.jpg
对于上面的目录结构:
BufferedImage im = ImageIO.read(new File(getClass().getResource("/resources/image.jpg").toURI()));
会成功.
这篇关于ImageIO 无法读取输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!