aspose file tools*
The moose likes Beginning Java and the fly likes PixelGrabber Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "PixelGrabber" Watch "PixelGrabber" New topic
Author

PixelGrabber

Suma MM
Ranch Hand

Joined: Apr 30, 2001
Posts: 31
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)
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

I'll bet that this has something to do with the image not being fully loaded and returning a height and width of 0...

try to add the following to your code between the loading and display of the image and see if it helps...



HTH,
-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Suma MM
Ranch Hand

Joined: Apr 30, 2001
Posts: 31
Thanks Nathan!
I did figure out the problem when I tried to print out the height and width of the image. They got printed as -1, -1! So, I figured out that there was something wrong with the loading of the image and so made the same change u had suggested. The code worked fine.
One question on the same topic.....If I am loading more than 1 image, do I assign separate IDs to each of the image and wait for each of them to load in a "sequential" way??
Thanks once again
-S
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

That is one way to do it... I prefer this way because you can show a progress bar as the images load, and it also solves one strange problem I will mention in a moment.

You can also use the MediaTracker waitForAll() method to just load all the images at once. waitForAll() doesn't care if the images have unique IDs or are all the same. I do this when I am being lazy. Or when I only have to load a few images and don't think the small passage of time will upset the user.

I have noticed one problem, though... if you assign alot of your images to the same ID it is okay up to about 4 or 5 images. After that, some of the images don't get loaded. Weird problem, but easy to overcome by assigning unique IDs to each image.

HTH,
-Nate
Suma MM
Ranch Hand

Joined: Apr 30, 2001
Posts: 31
Thanks Nathan!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PixelGrabber