java - 无法将图像加载到缓冲图像二维数组中

标签 java arrays bufferedimage

File newFile = null;
    BufferedImage oldImage = null;
    BufferedImage newImage = null;
    String fileName = new String((args[0]));

    String newFileName = new String(fileName.replaceFirst(".png", "-tiled.png"));

    try{
        oldImage = ImageIO.read(new File(fileName));
        System.out.println("Reading Complete");
                }
    catch(IOException e) {
    System.out.println("Error: "+ e);
    }

    int width = oldImage.getWidth();
    int height = oldImage.getHeight();
    newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    int imagePixels [][] = new int [height][width];
        for (int i = 0; i < height -1; ++i) {
        for (int j = 0; j < width - 1; ++j) {
            imagePixels[i][j] = oldImage.getRGB(i,j); //error here

            }
    }

它编译得很好,但是当我运行程序时,它可以完美地运行 10x10 图像,但是当我尝试使用 800x600 图像时,它不起作用,并且在注释行上出现错误,指出

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
    at sun.awt.image.ByteInterleavedRaster.getDataElements(Unknown Source)
    at java.awt.image.BufferedImage.getRGB(Unknown Source)
    at imagery.ImageTiler.main(ImageTiler.java:35)

有人能发现这个令人恼火的错误吗?

一些辅助信息:程序从命令行获取文件名,然后将 RGB 像素加载到 2D 数组中。

感谢任何帮助。干杯。

最佳答案

您在 getRGB 调用中得到的坐标是错误的。图像高度位于 Y 方向,但您使用高度作为 i 的边界 ... X 坐标。

This page解释Java图像坐标系。

关于java - 无法将图像加载到缓冲图像二维数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23027203/

相关文章:

java - 移动应用程序避免或保护 CORS?

Java - 更改对象后更新 JList

java - 二维数组未正确声明

JavaFX:将像素写入 PixelWriter 的最快方法

java - com.itextpdf.text.exceptions.IllegalPdfSyntaxException : Unbalanced begin/end text operators

Java:比较两个字符串数组并删除两个数组中存在的元素

php - 使用array_search和array_column检查多维数组中是否存在值,而不返回Array [0]的值

java - 扩展 BufferedImage

java - 尝试使用颜色图绘制垂直线性渐变,但仅使用前 9 种颜色 : why?

java - Base64 Java 编码和解码字符串