Hi!
I am trying to 'grab the pixels' from an image using an object of the class PixelGrabber as shown below. The run-time error generated is listed below the code. Can somebody suggest the reason for the error? Any help will be appreciated. Thanks!
P.S: The image "img1.jpg" is stored in the directory in which the program is run.
-S
CODE:
-----------------------------------------------------------------
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
public class CBImageRetrieval extends JFrame
{
Image image = null;
Toolkit tk;
public static void main(
String args[])
{
CBImageRetrieval ir = new CBImageRetrieval();
ir.setSize(900,700);
ir.show();
}
public void paint(Graphics g)
{
tk = this.getToolkit();
image = tk.getImage("img1.jpg");
g.drawImage(image,100,100,this);
int height = image.getHeight(this);
int width = image.getWidth(this);
int [] pixels = new int[width * height];
PixelGrabber imagegrabber;
imagegrabber = new PixelGrabber(image, 0,0, width, height, pixels, 0, width);
try
{
imagegrabber.grabPixels();
}
catch(Exception e)
{
System.out.println("Exception!!");
}
/*for (int i = 0; i < height*width; i++)
{
System.out.print(pixels[i] + " ");
}*/
}
}
-----------------------------------------------------------------
ERROR:
Uncaught error fetching image:
java.lang.ArrayIndexOutOfBoundsException
at java.awt.image.PixelGrabber.setPixels(Compiled Code)
at sun.awt.image.ImageDecoder.setPixels(Compiled Code)
at sun.awt.image.JPEGImageDecoder.sendPixels(JPEGImageDecoder.java:124)
at sun.awt.image.JPEGImageDecoder.readImage(Native Method)
at sun.awt.image.JPEGImageDecoder.produceImage(JPEGImageDecoder.java:150
)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.j
ava:248)
at sun.awt.image.ImageFetcher.fetchloop(Compiled Code)
at sun.awt.image.ImageFetcher.run(Compiled Code)