【问题标题】:Java - Why does ImageIO.read(filename) return null and the input image gets overwritten with 0 bytes?Java - 为什么 ImageIO.read(filename) 返回 null 并且输入图像被 0 字节覆盖?
【发布时间】:2014-11-22 01:18:29
【问题描述】:

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

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

【问题讨论】:

    标签: java image io


    【解决方案1】:

    (想评论但不能评论。)

    BufferedImage image = ImageIO.read(filename);
    

    应该是这样的

    BufferedImage image = ImageIO.read(imageFile);
    

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

    【讨论】:

    • 是的,应该是的,我一直在稍微修改讲师代码,但在我改回来时错过了那一点。
    猜你喜欢
    • 2016-05-28
    • 1970-01-01
    • 2021-12-15
    • 2018-05-31
    • 2014-11-08
    • 1970-01-01
    相关资源
    最近更新 更多