我正在尝试从 64 x 48 位图中获取像素rgb值。我得到了一些值,但远不及我期待的 3072(= 64 x 48)值。我也得到了:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:301)
at java.awt.image.BufferedImage.getRGB(BufferedImage.java:871)
at imagetesting.Main.getPixelData(Main.java:45)
at imagetesting.Main.main(Main.java:27)

我找不到越界错误......

以下是代码:

package imagetesting;

import java.io.IOException;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.image.BufferedImage;



public class Main {

public static final String IMG = "matty.jpg";

public static void main(String[] args) {

    BufferedImage img;

    try {
        img = ImageIO.read(new File(IMG));

        int[][] pixelData = new int[img.getHeight() * img.getWidth()][3];
        int[] rgb;

        int counter = 0;
        for(int i = 0; i < img.getHeight(); i++){
            for(int j = 0; j < img.getWidth(); j++){
                rgb = getPixelData(img, i, j);

                for(int k = 0; k < rgb.length; k++){
                    pixelData[counter][k] = rgb[k];
                }

                counter++;
            }
        }


    } catch (IOException e) {
        e.printStackTrace();
    }

}

private static int[] getPixelData(BufferedImage img, int x, int y) {
int argb = img.getRGB(x, y);

int rgb[] = new int[] {
    (argb >> 16) & 0xff, //red
    (argb >>  8) & 0xff, //green
    (argb      ) & 0xff  //blue
};

System.out.println("rgb: " + rgb[0] + " " + rgb[1] + " " + rgb[2]);
return rgb;
}

}
有帮助吗?

解决方案

此:

for(int i = 0; i < img.getHeight(); i++){
    for(int j = 0; j < img.getWidth(); j++){
        rgb = getPixelData(img, i, j);

与此不匹配:

private static int[] getPixelData(BufferedImage img, int x, int y) {

你有 i 计算行, i 列,即 i 包含 y 值和 j 包含 x 值。那是倒退。

其他提示

这也有效:

BufferedImage img = ImageIO.read(file);

int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();

我一直在寻找同样的能力。不想枚举整个图像,所以我做了一些搜索并使用了PixelGrabber。

Image img = Toolkit.getDefaultToolkit().createImage(filename);
PixelGrabber pg = new PixelGrabber(img, 0, 0, -1, -1, false);

pg.grabPixels(); // Throws InterruptedException

width = pg.getWidth();
height = pg.getHeight();

int[] pixels = (int[])pg.getPixels();

你可以在这里直接使用 int [] ,像素是由 pg.getColorModel()中的ColorModel指定的格式,或者你可以将false更改为为true并强制它为RGB8-in-ints。

我发现 Raster 和Image类也可以这样做,并且在 javax.imageio。* 中添加了一些有用的类。

BufferedImage img = ImageIO.read(new File(filename)); // Throws IOException
int[] pixels = img.getRGB(0,0, img.getWidth(), img.getHeight, null, 0, img.getWidth());

// also available through the BufferedImage's Raster, in multiple formats.
Raster r = img.getData();
int[] pixels = r.getPixels(0,0,r.getWidth(), r.getHeight(), (int[])null);

Raster 中还有几个 getPixels(...)方法。

int argb = img.getRGB(x,y); 您的代码

int argb = img.getRGB(y,x); 我的更改现在可以正常使用

你必须改变:

for(int i = 0; i < img.getHeight(); i++){
    for(int j = 0; j < img.getWidth(); j++){
        rgb = getPixelData(img, i, j);

for(int i = 0; i < img.getWidth(); i++){
    for(int j = 0; j < img.getHeight(); j++){
        rgb = getPixelData(img, i, j);

因为 getPixelData 中的第二个参数是 x -value,而thirth是 y -value。你切换了参数。

为什么不使用只使用:

public int[] getRGB(int startX,
                    int startY,
                    int w,
                    int h,
                    int[] rgbArray,
                    int offset,
                    int scansize)

它是内置的,伙计。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top