Java ImageIO.read() 在 Maven 项目中找不到资源

问题描述 投票:0回答:1

我在基于 Maven 的 Java 项目中加载图像资源时遇到问题。该文件似乎正确放置在 src/main/resources 目录中,但我不断收到找不到图像的错误。我是处理文件和图像的新手。我正在开发一个小程序,它获取图像并将其输出为 ASCII 艺术。我让它工作了一段时间,然后就停止工作了。

Image not found in resources: cat.png

文件树:

ImageConverter
└───src
    ├───main
    │   ├───java
    │   │   └───(my packages)
    │   └───resources
    │       └───cat.png
    └───test

相关Image类代码:

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)
    代替。
  • 使用输入流和文件对象。
  • 同一文件夹内有 3 个不同的图像。
  • 尝试使用文件系统上的绝对路径加载图像。

我真的希望我没有粗心并使用git,这样我至少可以恢复到工作状态...... 建议?我希望我没有遗漏一些明显的东西。

java maven intellij-idea bufferedimage javax.imageio
1个回答
0
投票

编辑:我修好了!我使用 intellij 项目结构在 intellij 中重新制作了项目,复制了所有类文件,创建了一个资源文件夹,标记为资源路径,现在它可以工作了。文件夹结构如下所示:

ImageConverter2
├───.idea
├───out
│   └───production
│       └───ImageConverter2
├───resources
└───src
© www.soinside.com 2019 - 2024. All rights reserved.