Tutorials J2ME Capturing RGB Color of pixel

Capturing RGB Color of pixel

Make a mutable image, as you do in case of screen capture and then allow the user to play with pointer over it. Create mutable image using:

Image mutableImage=Image.createImage(getWidth(), getHeight());
Graphics g=mutableImage.getGraphics();
mutableImage.paint(g);

Now to get the color of the selected pixel x,y:
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
Total
0
Shares
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Anonymous
Anonymous
11 years ago

Thank you very much for this very rich post.
I was really struggling to get a color at a specific pixel on a picture, and this post has been of a tremendous help for me.
I think I’ll definitely stick to this blog and bring my humble contribution to help others as well.
Once again, thank you.

javatechig
javatechig
10 years ago
Reply to  Anonymous

Thank you.. I appreciate you leaving your name here.

Previous Post
j2me_tutorials_javatechig_featured_image

Optimization & Perfomance Tunning

Next Post
j2me_tutorials_javatechig_featured_image

Localization in LWUIT

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All