• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting the color of a particular pixel from an image loaded in applet

 
vartika srivastava
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have used the class PixelGrabber and been able to get the RGB componenet in to an array pixels...i want to b able to compare the color of a particular pixel with black...if the color is same then it must add to the array arr...can somebody help me? ihave pasted the code


private Image logo1;

int pixels[];
Color col[];
String msg;
int arr[];
int k=0;
public void init()
{
logo1=getImage(getDocumentBase(),"trial2.jpg");
int width=getWidth();
int height=getHeight();
pixels = new int[width*height];
PixelGrabber pg =
new PixelGrabber(logo1, 0, 0, width, height, pixels, 0, width);
try
{
pg.grabPixels();
}
catch (InterruptedException e) { }

col=new Color[width*height];
arr=new int[width*height];

for(int i=0;i<width*height;i++)
{
int c=pixels[i];
int red = (c & 0x00ff0000) >> 16;
int green = (c & 0x0000ff00) >> 8;
int blue = c & 0x000000ff;
col[i] = new Color(red,blue,green);
if(col[i]==Color.black)
{
arr[k]=i;
k++;
}
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

The color black has r=0, g=0 and b=0. Since you already know how to access the RGB color components, comparing a color to black should be easy.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic