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
This article is about: Code Sample, J2ME,

Stacktips

StackTips provides programming tutorials, how-to guides and code snippets on different programming languages. Follow us on Twitter

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.
  • Anonymous

    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

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