0

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 {
	    pg.grabPixels();
	} catch (InterruptedException e) {
	    System.err.println("interrupted waiting for pixels!");
	    return;
	}
	if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
	    System.err.println("image fetch aborted or errored");
	    return;
	}

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



    }

Errors:
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

Edited by masijade: added code tags

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by JamesCherrill
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.