hey i had started coding in java.I want to display the pixels format of image with the following code but i get errors...Can anyone suggest my code??

public void Pixelvalue(Image img){
     int[] pixelss = new int[w * h];
	PixelGrabber pg = new PixelGrabber(img, 0, 0, w, h, pixelss, 0, w);
	try {
	} catch (InterruptedException e) {
	    System.err.println("interrupted waiting for pixels!");
	if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
	    System.err.println("image fetch aborted or errored");

	    for (int i = 0; i < w; i++) {
		//handlesinglepixel(x+i, y+j, pixels[j * w + i]);
                System.out.println(" pixelss[i]") ;


Uncaught error fetching image:
java.lang.ArrayIndexOutOfBoundsException: 1
at java.awt.image.PixelGrabber.setPixels(PixelGrabber.java:599)
at sun.awt.image.ImageDecoder.setPixels(ImageDecoder.java:120)
at sun.awt.image.JPEGImageDecoder.sendPixels(JPEGImageDecoder.java:97)
at sun.awt.image.JPEGImageDecoder.readImage(Native Method)
at sun.awt.image.JPEGImageDecoder.produceImage(JPEGImageDecoder.java:119)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:246)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
image fetch aborted or errored

Recommended Answers

All 2 Replies

Hard to say. Are you sure the img object holds a valid image?

w and h look dangerous - suggest you make them local and set them explicitly from the img itself at the start of this method. I'd print them out here as well, just in case.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.