简体   繁体   中英

Java - Why does ImageIO.read(filename) return null and the input image gets overwritten with 0 bytes?

I have several methods that manipulate a .jpg image: mirroring on x and y axis, tiling it, converting to ASCII text, and adjusting its brightness. All the methods work properly except the brightness one. When I run the brightness method, there is a NullPointerException in readGrayscaleImage() (see below - instructor written code). The BufferedImage is null, however it isn't when the method is called from any other of my methods (mirrors, tiling, ascii all read the file correctly). Not only is the BufferedImage null, the input image it is trying to read gets overwritten with 0 bytes and cannot be viewed in an image viewer, which would probably explain why the BufferedImage is null.

Here is a working method that calls readGrayscaleImage() :

public static void tileImage(int h, int v, String infile, String outfile) {
    int[][] result = readGrayscaleImage(infile);
    int[][] tileResult = new int[result.length * h][result[0].length * v];
    for (int hh = 0; hh < h; hh++) {
        for (int vv = 0; vv < v; vv++) {
            for (int i = 0; i < result.length; i++) {
                for (int j = 0; j < result[i].length; j++) {
                    tileResult[i + hh * result.length][j + vv * result[i].length] = result[i][j];
                }
            }
        }
    }
    writeGrayscaleImage(outfile, tileResult);
}

Here is the brightness method that results in the problem:

public static void adjustBrightness(int amount, String infile, String outfile) {

    int[][] result = readGrayscaleImage(infile); // NullPointerException trace points here

    for (int i = 0; i < result.length; i++) {
        for (int j = 0; j < result[i].length; j++) {
            if (result[i][j] + amount > 255)
                result[i][j] = 255;
            else if (result[i][j] + amount < 0)
                result[i][j] = 0;
            else 
                result[i][j] += amount;
        }
    }
    writeGrayscaleImage(outfile, result);
}

Here is the instructor-written code that reads a greyscale .jpg file and returns an array of integers:

public static int[][] readGrayscaleImage(String filename) {
    int [][] result = null; //create the array
    try {
        File imageFile = new File(filename);    //create the file
        BufferedImage image = ImageIO.read(imageFile);

        int height = image.getHeight();  // NullPointerException POINTS HERE - image IS NULL
        int width  = image.getWidth();
        result = new int[height][width];        //read each pixel value
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int rgb = image.getRGB(x, y);
                result[y][x] = rgb & 0xff;
            }
        }
    }
    catch (Exception ioe) {
        System.err.println("Problems reading file named " + filename);
        ioe.printStackTrace();
        System.exit(-1);
    }

    return result;  //once we're done filling it, return the new array
}

Here is the instructor written method to write a .jpg:

public static void writeGrayscaleImage(String filename, int[][] array) {
    int width = array[0].length;
    int height = array.length;

    try {
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);    //create the image

        //set all its pixel values based on values in the input array
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                int rgb = array[y][x];
                rgb |= rgb << 8;
                rgb |= rgb << 16;
                image.setRGB(x, y, rgb);
            }
        }

        //write the image to a file
        File imageFile = new File(filename);
        ImageIO.write(image, "jpg", imageFile);
    }
    catch (IOException ioe) {
        System.err.println("Problems writing file named " + filename);
        System.exit(-1);
    }
}

(Wanted to comment but am not able to.)

BufferedImage image = ImageIO.read(filename);

Should this be

BufferedImage image = ImageIO.read(imageFile);

? I don't even see an override of read that takes a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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