• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

PixelGrabber

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Suma MM
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Nathan!
 
reply
    Bookmark Topic Watch Topic
  • New Topic