简体   繁体   English

如何将图像写入文件– Java ImageIO

[英]How to write an image to file – Java ImageIO

I would like to write an image to a file using Java ImageIO. 我想使用Java ImageIO将图像写入文件。 This is my piece of code: 这是我的代码:

Document doc = convertDocument (fileName, "grey");                       
      ImageIO.write(Converter.convertSVGToPNG(doc), "png",
              new File(FOLDER_PNG_OUT_GREY + fileName.replaceAll(".svg", ".png")));

But I get this error when running my code: 但是在运行代码时出现此错误:

java.io.FileNotFoundException: C:\\Work\\eclipse-tdk\\svgManager\\svgManager\\src\\main\\resources\\icons\\svg\\grey_png\\$pac.png (The system cannot find the path specified) java.io.FileNotFoundException:C:\\ Work \\ eclipse-tdk \\ svgManager \\ svgManager \\ src \\ main \\ resources \\ icons \\ svg \\ grey_png \\ $ pac.png(系统找不到指定的路径)

I also tried: 我也尝试过:

String pngFileName = FOLDER_PNG_OUT_GREY + fileName.replaceAll(".svg", ".png");
File outputfile = new File(pngFileName);
bImage = ImageIO.read(outputfile);

but then I got the error: Can't read input file! 但是然后我得到了错误: 无法读取输入文件!

我建议使用getResource(String file)方法获取图像资源,如下所示:

File initialImage = new File(YourClass.class.getResource("pac.png").getFile());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM