how to get rgb value each pixel of an image file
hi everyone,
I am working with my grad. project. It is about data compression of an image file. I will use LWZ / HUFFMAN algorithm to do it. And first step is to get rgb value of each pixel. So, I have a sample code for this but it does not work.
Could you please tell where the problem is
thanks in advance,
S.Ozan IRBIK.
Code:
import java.awt.Component;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class WalkImageTest1 extends Component {
public static void main(String[] foo) {
new WalkImageTest1();
}
public void printPixelARGB(int pixel) {
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
System.out.println("argb: " + alpha + ", " + red + ", " + green + ", " + blue);
}
private void marchThroughImage(BufferedImage image) {
int w = image.getWidth();
int h = image.getHeight();
System.out.println("width, height: " + w + ", " + h);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
System.out.println("x,y: " + j + ", " + i);
int pixel = image.getRGB(j, i);
printPixelARGB(pixel);
System.out.println("");
}
}
}
public WalkImageTest1() {
try {
// this is an image of a white spot on a black background.
// with the smoothing in the image it's of course not all black
// and white
BufferedImage image =
ImageIO.read(this.getClass().getResource("C:\\1.jpeg"));
marchThroughImage(image);
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
I am getting these errors:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at WalkImageTest1.<init>(WalkImageTest1.java:41)
at WalkImageTest1.main(WalkImageTest1.java:9)