Java - 为什么 ImageIO.read(文件名) 返回 null 并且输入图像被 0 字节覆盖



我有几种方法可以操作.jpg图像:在x和y轴上镜像,平铺,转换为ASCII文本以及调整其亮度。除亮度方法外,所有方法都可以正常工作。当我运行亮度方法时,readGrayscaleImage()中有一个 NullPointerException(见下文 - 讲师编写的代码)。BufferedImage为 null,但是当从我的任何其他方法(镜像、平铺、ASCII 都正确读取文件)调用该方法时,它不是。不仅BufferedImage为空,它尝试读取的输入图像会被 0 字节覆盖,并且无法在图像查看器中查看,这可能解释了为什么BufferedImage为 null。

这是一个调用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);
}

以下是导致问题的亮度方法:

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

以下是讲师编写的代码,用于读取灰度.jpg文件并返回整数数组:

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
}

以下是教师编写.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);
    }
}

(想发表评论,但无法评论。

BufferedImage image = ImageIO.read(filename);

这应该是

BufferedImage image = ImageIO.read(imageFile);

? 我什至没有看到需要字符串的读取覆盖。

最新更新