如何使用getRGB在Java中获取图像的像素值数组 [英] How to get the array of pixel values for an image in java using getRGB
问题描述
我是Java图像处理的新手.实际上,我要执行的操作是将图像的所有像素值保存到数组rgbArray[]
中,问题是我在数组的所有索引(即数组的所有索引)中获得相同的值数组具有相同的值.下面给出了一部分代码:
I'm new to image processing in java. Actually what I'm trying to do is to save all the pixel values of an image into an array rgbArray[]
, and the problem is that I'm getting the same values in all the indexes of an array i.e. all the indexes of the array have the same value. A part of code is given below:
int[] rgbArray=new int[w*h]; // Array to store the Pixel values
BufferedImage buffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
buffer.getRGB(0, 0, w, h, rgbArray, 0, w );
for(int i=0;i<w*h;i++)
{
System.out.println("rgbArray["+i+"] = "+ rgbArray[i]);
}
对于rgbArray
中的所有索引,我得到的输出是-16777216
.图像的每个像素如何具有相同的值?以及如何获得正确的像素值?
The output which i'm getting is -16777216
for all indexes in rgbArray
. How is it possible to have the same value for each and every pixel of the image? And how do I get the correct pixel value??
推荐答案
由于不为BufferedImage提供任何值,因此每个像素默认为alpha = 255,红色= 0,绿色= 0和蓝色= 0;将所有这些都放入1 int,您将得到-16777216.我是从这里得到的:
Since you do not provide any values for the BufferedImage, every pixel defaults to alpha = 255, red = 0, green = 0, and blue = 0; Put all of those into 1 int and you get -16777216. I got this from:
int val = buffer.getRGB(5, 23);
int a = (0xff000000 & val) >>> 24;
int r = (0x00ff0000 & val) >> 16;
int g = (0x0000ff00 & val) >> 8;
int b = (0x000000ff & val);
System.out.println("a " + a + " r " + r + " g " + g + " b " + b);
哪个会产生255 r 0 g 0 b 0.
Which produces a 255 r 0 g 0 b 0.
这篇关于如何使用getRGB在Java中获取图像的像素值数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!