5 Replies Latest reply: Jun 1, 2008 6:43 AM by 843851

# How to get pixel colour?

Is there any way to get pixel colour from specified point? Example: colour = image.getPixel(x, y);

J2ME/MIDP 1.0

Thanks!
• ###### 1. Re: How to get pixel colour?
Hello.

You can get a unique pixel from an image using the getRGB() method. Here is an example:

int[] aux=new int[1];
image1.getRGB(aux,0,image1.getWidth(),x,y,1,1);

At this moment, you have in aux the ARGB code of the (x,y) pixel contained in the image image1. aux is an array, and then have only one component with the pixel in format 0xAARRGGBB (A=opacity, R=red level, G=green level, B=blue level (levels from 0 to 255)). If you need the red, green and blue levels, you can use it:

int[] p=new int[3];
p[0]=(int)((aux[0]&0x00FF0000)>>>16); //Red level
p[1]=(int)((aux[0]&0x0000FF00)>>>8); //Green level
p[2]=(int)(aux[0]&0x000000FF); //Blue level

And if you need the opacity level, you can do it:

int a=(int)((aux[0]&0xFF000000)>>>24); //Opacity level

That�s all.
• ###### 2. Re: How to get pixel colour?

Get the color of a specific pixel

We assume that we have an Image called picture

pixels = new int[width*height];
PixelGrabber pg = new PixelGrabber(picture, 0, 0, width, height, pixels, 0, width);
try {
pg.grabPixels();
}
catch (InterruptedException e) { }

From here, individual pixel can be accessed via the pixels array.

int c = pixels[index]; // or pixels[x * width + y]
int red = (c & 0x00ff0000) >> 16;
int green = (c & 0x0000ff00) >> 8;
int blue = c & 0x000000ff;
// and the Java Color is ...
Color c = new Color(red,blue,green);
• ###### 3. Re: How to get pixel colour?
May I know the exact syntax in j2me for how to get pixel colour.

thanks
• ###### 4. Re: How to get pixel colour?
Thanks.........
It is very usefull for me
• ###### 5. Re: How to get pixel colour?

{color:#800000}*I am happy to compare the two pixels of same image using this code in Java:*
{color}
Image ori, nw;

public void init()
{ ori = getImage(getDocumentBase(),"flower.jpg");
int pxl[]= new int [65536];
PixelGrabber pg = new PixelGrabber(ori,0,0,256,256,pxl,0,256);
// Image is stored into Array

try{ pg.grabPixels(); }
catch(InterruptedException e) {}

for(int i=0;i<65536;i++)
{ int p = pxl[i];
int ri = (0xff & (p>>16));
int gi = (0xff & (p>>8));
int bi = (0xff & p); // Take the RGB value of each pixel

for(int j=0;j<255;j++) // Chk. other similar pixel in color range of 0 to 255
{
int q = pxl[j];
int rj = (0xff & (q>>16));
int gj = (0xff & (q>>8));
int bj = (0xff & q);

if ( (ri==rj)&&(gi==gj)&&(bi==bj)&&(!(i==j)))

{ ri+=70;//if (ri<0) ri = 0;
gi+=70;//if (gi<0) gi = 0;
bi+=70;//if (bi<0) bi = 0;
pxl[i] = ( 0xff000000|ri<<16|gi<<8|bi); // Change value
}
else
pxl[i] = ( 0xff000000|ri<<16|gi<<8|bi);// Keep as it is
}

Email : jajalbr@yahoo.com, Profile: www.aribas.edu.in/Mr.Brijesh_Jajal.htm