I want to know individual alpha red blue and green values of a pixel. Following code displays image from a file ( given as first argument ) but does not show any rgba vaures ( all values returned are 0 )
Pl. let me know what is the error in the code :
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));
// 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();
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);
}
}
}
}
}
I have also tried to get rgba values in a array int rgba[] form. But I am always getting zero values for all pixels.
Please help.