使用java从图像中获取像素数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1604319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:39:19  来源:igfitidea点击:

Getting pixel data from an image using java

javargb

提问by Matt

I'm trying to get the pixel rgb values from a 64 x 48bit image. I get some values but nowhere near the 3072 (= 64 x 48)values that I'm expecting. I also get:

我正在尝试从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)

I can't find the out of bounds error...

我找不到越界错误...

Here's the code:

这是代码:

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

}

采纳答案by John Kugelman

This:

这个:

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

Does not match up with this:

与此不符:

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

You have icounting the rows and jthe columns, i.e. icontains yvalues and jcontains xvalues. That's backwards.

您已经i计算了行和j列,即i包含y值并j包含x值。那是倒退。

回答by Martijn Courteaux

You have to change:

你必须改变:

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

Into

进入

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

Because the second parameter from getPixelDatais the x-value and the thirth is the y-value. You switched the parameters.

因为 from 的第二个参数getPixelDatax-value ,第三个参数是-value y。您切换了参数。

回答by davenpcj

I was looking for this same ability. Didn't want to enumerate the entire image, so I did some searching and used PixelGrabber.

我一直在寻找同样的能力。不想枚举整个图像,所以我做了一些搜索并使用了 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();

You could use the int[]directly here, pixels are in a format dictated by the ColorModel from pg.getColorModel(), or you can change that false to true and force it to be RGB8-in-ints.

您可以在int[]此处直接使用,像素的格式由 ColorModel from 指定pg.getColorModel(),或者您可以将该 false 更改为 true 并强制其为 RGB8-in-int。

I've since discovered that the Rasterand Image classes can do this too, and there have been some useful classes added in javax.imageio.*.

后来我发现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);

There are several getPixels(...)methods in Rasteras well.

里面也有几种getPixels(...)方法Raster

回答by Richard

int argb = img.getRGB(x, y);Your code

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

int argb = img.getRGB(y, x);my changes now it works

int argb = img.getRGB(y, x);我的更改现在可以工作了

回答by Albus Dumbledore

Why didn't use just use:

为什么不使用只是使用:

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

It's built-in, man.

它是内置的,伙计。

回答by Mithusayel Murmu

This works too:

这也有效:

BufferedImage img = ImageIO.read(file);

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