Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to check if image is valid

 
Adam Kronicki
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a prolem. For my application I dynamically download images for results form a database. I use a4j:mediaOutput to display them. The problem is that some images returned are not valid to display (can't even display them on a PC). Is there a way I can detect such thing and display sth like 'image unavailable' in it's place? I don't mean what happens if my image isn't downloaded or I can't connect to get it - this is easily done by checking if the byte array is null. The problem is that if I download an image but i.e. it is corrupted so can't be displayed. Does anyone have any advice?
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

ImageIO library has a lot of built in routines to get the format of the image. That format you can compare.
If you get something valid, proceed on else it is a corrupt image.

HTH,
 
Adam Kronicki
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes now I am using ImageReader.getFormatName() for this. I only wish the funcion would return the format in a mime type thing. So for example "image/jpeg" instead of "JPEG" - that way I could easily compare with what I download, without making some kind of a switch and predict the kind of images I might get.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Adam,

If you are storing the MIME types in the server, as you already know the type is image.
So for custom basis may be, if you get something like this from the database for example
image/jpeg. You can take the substring after / and then compare the formats. I am not sure
how your MIME types are stored.
HTH,
 
Adam Kronicki
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I though about that and I need to see the list of results I can get from getFormatName() and compere them with mime types. Maybe a simple upper case and 'equals' can do the trick. Thanks any way :>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic