javamavenintellij-ideaclassloadergetresource

getResourceAsStream() Not Finding Resource in Maven Project


I'm having trouble loading an image resource in a Maven-based Java project. The file seems to be correctly placed in the src/main/resources directory, but I keep getting an error that the image is not found. I am new to working with files and images. I'm working on a little program that takes an image and outputs it as ASCII art. I had it working for a while, and then it stopped working.

Image not found in resources: cat.png

File Tree:

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

Relevant Image class code:

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);
    }
}

Printing the URL input prints null. I have tried the following:

I really wish I didn't get careless and used git so I could at least revert to a working state... Suggestions? I hope I'm not missing something obvious.


Solution

  • EDIT: I fixed it! I remade the project in intellij using the intellij project structure, copied over all my class files, made a resources folder, marked as resources route, and now it works. The folder structure looks as follows:

    ImageConverter2
    ├───.idea
    ├───out
    │   └───production
    │       └───ImageConverter2
    ├───resources
    └───src