can not get red, green, blue values of a pixel
1 posts in topic
Thread View Thread View
TOPIC ACTIONS:
 

can not get red, green, blue values of a pixel...
maheshchavan
Sat Jun 16, 2012 10:47 AM

I want to know alph, red, blue, and green values of pixels in buffered image. Following displays the picture correctly, But all red blue and green values are always zero.  Can anybody guide me how to get actual values :
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class BuffIt {
  public static void main (String args[]) {
    // Get Image
    ImageIcon icon = new ImageIcon(args[0]);
    Image image = icon.getImage();
    int totval = 0;
    int picval;
        int rd, bl, gr;
    picval = 0;
        int i,j,w,h, clr;


    // Create empty BufferedImage, sized to Image
    BufferedImage buffImage =
      new BufferedImage(
          image.getWidth(null),
          image.getHeight(null),
          BufferedImage.TYPE_INT_ARGB);
        w = image.getWidth(null);
        h = image.getHeight(null);
        System.out.println("width : " + Integer.toString(w));
        System.out.println("height : " + Integer.toString(h));

        for (j=0;j<h;j++) {
        for (i=0;i<w;i++) {
        clr = buffImage.getRGB(i,j);
    rd   = (clr & 0x00ff0000) >> 16;
      gr = (clr & 0x0000ff00) >> 8;
      bl  =  clr & 0x000000ff;
        picval = ( picval + rd + gr + bl);
        if ( rd > 0 || gr > 0 || bl > 0 ) {
        System.out.println("red : " + Integer.toString(rd));
        System.out.println("green : " + Integer.toString(gr));
        System.out.println("blue : " + Integer.toString(bl));
        System.exit(0);
        }
    }
        }
        System.out.println("picval : " + Integer.toString(picval));
        totval = (totval + picval ) & 0xffffffff;
        System.out.println("totval: " + Integer.toString(totval));



    // Draw Image into BufferedImage
    Graphics g = buffImage.getGraphics();
    g.drawImage(image, 0, 0, null);

    // Show success
    JFrame frame = new JFrame();
    JLabel label = new JLabel(new ImageIcon(buffImage));
    frame.getContentPane().add(label);
    frame.pack();
    frame.show();
  }
}

Please help

 When instead of   BufferedImage buffImag...

maheshchavan
Sat Jun 23, 2012 09:39 AM

When instead of 
 BufferedImage buffImage =
      new BufferedImage(
          image.getWidth(null),
          image.getHeight(null),
          BufferedImage.TYPE_INT_ARGB);
I used
File file= new File(args[0]);
                buffImage = ImageIO.read(file);
It worked and gave me correct values of red, green blue !

About | Sitemap | Contact