• Post Reply Bookmark Topic Watch Topic
  • New Topic

loading large images safely

 
Jonathan Lister
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a safe way to prevent images which are too large from being loaded? Imagine a slideshow application and the user can load whatever jpeg he wants, and stupidly uses full resolution images from a high res. scanner.
I tried using media tracker, and even addes a safety timer - but when a very large image is loaded, even the timer doesn't seem to fire, the app just seems to hang whilst trying to find enough memory to load the large image. I have increased size of heap with -Xms to *very large*, e.g. 120MB which helps to some degree, but it would be better to be able to decline large images politely rather than fail whilst attempting to load them.



and the calling fragment is e.g
ImageLoader l = new ImageLoader();

for (int i = 0; i < loopCount; i++)
{
try
{
image = l.getImage(thisFileName);
printlnWithDate("loaded " + thisFileName);
if (image != null)
{
w.repaint();
sleep(sleepTime);
image.flush();
image = null;
}
else
printlnWithDate("image was null: " + thisFileName);
}
catch (OutOfMemoryError e)
{
}
 
Brian Pipa
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your public Image getImage(String fName) method, before you try to load the file, create a File object of it and then call File.length() to see how big it is. If it's bigger than you want to load, don't load it.
Somthing like:

brian
[ February 23, 2004: Message edited by: Brian Pipa ]
 
Jonathan Lister
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the constructive suggestion Brian, it is something I have considered, but there isn't always a good relationship between physical file size and pixel byte size, especially for jpegs with heavy compression.
I think that the jpeg file itself must have the pixel size stored in a 'header' record - for example in Windows XP file browser the tooltip over a jpeg file will show the pixel dimensions in addition to file size.
Is there a way to read this 'header' data before trying to load the image?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you could try reading the header information from the JPEG file... you would just have to open a file stream on the image file and search for the information you want.

If the information on this website is correct, it looks like you would scan the file for the hex string "FFE0" and skip the next nine bytes. The tenth byte indicates what units height and width are in - 0 = pixels, 1=dots per inch, 2=dots per cm. The two bytes after this are the width, and the next two are the height.
 
Jonathan Lister
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nate,
I followed the same idea and found a useful java class (JpegInfo.java) which knows how to read the jpeg header information at
Giant Java Tree
It works a treat.
 
Brian Pipa
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that - when you said size I thought you meant file size not resolution. Glad you found your answer.
Brian
 
Paper has not yet been outlawed – so let your Java Swing application print with wild abandon: JPDF
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!