我在一个基于Maven的Java项目中加载图像资源时遇到了问题。文件似乎正确地放置在src/main/resources目录中,但我一直收到一个错误,即找不到图像。我是处理文件和图像的新手。我正在编写一个小程序,可以获取图像并将其输出为ASCII艺术。我让它工作了一段时间,然后它停止工作。
第一个月
文件树:
ImageConverter
└───src
├───main
│ ├───java
│ │ └───(my packages)
│ └───resources
│ └───cat.png
└───test
字符串
相关图像类代码:
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
public class Image {
// Reference https://paulbourke.net/dataformats/asciiart/
private static final String DEFAULTIMAGE = "cat.png";
private static final String ASCIICHARS = ".'`^\\\",:;Il!i><~+_-?][}{1)(|\\\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$"; // taken from https://medium.com/@shubham0473/unleash-your-inner-artist-a-step-by-step-guide-to-converting-images-to-ascii-art-using-java-97860464f19a
private static final double MAX_LUMINANCE = 196964.0;
private BufferedImage img;
public Image(){
this(DEFAULTIMAGE);
}
public Image(String filename) {
try {
URL input = getClass().getClassLoader().getResource(filename);
//InputStream input = Image.class.getResourceAsStream(filename);
System.out.println(input);
if (input==null) {
System.err.println("Image not found in resources: " + filename);
return;
}
this.img = ImageIO.read(input);
} catch (IOException e){
System.err.println("Error loading image: " + filename);
e.printStackTrace();
}
}
public BufferedImage getImg(){
return this.img;
}
public String toString(){
// use convertToASCII to create the string to be printed to the console.
char[][] ASCIItable = convertToASCII(this.img);
StringBuilder s = new StringBuilder();
for (char[] table : ASCIItable){
for (char cell : table){
s.append(cell);
}
s.append("\n");
}
return s.toString();
}
public char[][] convertToASCII(BufferedImage img){
// https://en.wikipedia.org/wiki/Relative_luminance
FastRGB pixels = new FastRGB(img);
char[][] ASCII_array = new char[img.getWidth()][img.getHeight()];
for (int y = 0; y < img.getHeight(); y++){
for (int x = 0; x < img.getWidth(); x++){
short[] currentPixel = pixels.getRGB(x, y);
double r = currentPixel[0];
double g = currentPixel[1];
double b = currentPixel[2];
double rr = Math.pow(r, 2.2);
double gg = Math.pow(g, 2.2);
double bb = Math.pow(b, 2.2);
double luminance = rr * 0.2126 + gg * 0.7152 + bb * 0.0722;
char luminanceChar = luminanceToASCII(luminance);
ASCII_array[x][y] = luminanceChar;
}
}
return ASCII_array;
}
public char luminanceToASCII(double n){
n = ((n / MAX_LUMINANCE) * ASCIICHARS.length()) - 1;
if (n < 0)
n = 0;
return ASCIICHARS.charAt((int)n);
}
}
型
打印URL输入将打印null。我尝试了以下方法:
- 说明
cat.png
直接位于resources
文件夹中。 - 确保
src/main/resources
在IDE中标记为资源根。 - 使用
getClass().getClassLoader().getResource(filename)
代替。 - 使用InputStream和File对象。
- 同一文件夹中的3个不同图像。
- 尝试使用文件系统上的绝对路径加载映像。
我真的希望我没有粗心大意,使用git,这样我至少可以恢复到工作状态.建议?我希望我没有错过一些明显的东西。
2条答案
按热度按时间iyr7buue1#
编辑:我修好了!我使用intellij项目结构在intellij中重新创建了项目,复制了我所有的类文件,创建了一个resources文件夹,标记为resources route,现在它可以工作了。文件夹结构如下所示:
字符串
vbkedwbf2#
我用下面的结构做了一个基本的测试
的数据
(Maven应该是IDE不可知的,所以实际上IDE应该是无关紧要的)
然后我用了...
字符串
而且...
型
两者都奏效了。
作为个人偏好,我更喜欢第一种,因为我可以根据自己的意愿在相对路径和绝对路径之间切换。
在你的情况下,我会考虑:
.jar
文件并确保包含图像mvn clean install
或IDE提供的任何东西)