how to check for valid image

Discussion in 'Java' started by damon, Oct 28, 2010.

  1. damon

    damon Guest

    hi,
    I have a couple of files from which I want to create BufferedImage so
    that I can display it on a jpanel.Problem is that their extensions are
    unreliable.
    /myimgs/image1.png

    /myimgs/realimage2.txt

    /myimgs/fakeimage3.png

    the first two are actual png images and the last one is a text file
    renamed as .png
    Is there some way to test for the valid images?

    I tried this

    public boolean checkIfImageFile(String filename){
    boolean isimage=false;
    try{
    BufferedImage bi=ImageIO.read(new File(filename));
    if (bi!=null)isimage=true;
    }catch(Exception e){
    isimage=false;
    }

    return isimage;
    }

    Is this the way to do such a test?If there is a proper way please tell
    me.
    thanks in advance
    sajan
    damon, Oct 28, 2010
    #1
    1. Advertising

  2. damon

    markspace Guest

    On 10/28/2010 10:25 AM, damon wrote:
    > the first two are actual png images and the last one is a text file
    > renamed as .png
    > Is there some way to test for the valid images?



    What you have probably works well enough. I would open the file and
    look for magic numbers and such, first, as it might be faster/less CPU.
    As long as what you have seems to actually function, I don't see a
    reason to change it.
    markspace, Oct 28, 2010
    #2
    1. Advertising

  3. damon

    Arne Vajhøj Guest

    On 28-10-2010 13:25, damon wrote:
    > I have a couple of files from which I want to create BufferedImage so
    > that I can display it on a jpanel.Problem is that their extensions are
    > unreliable.
    > /myimgs/image1.png
    >
    > /myimgs/realimage2.txt
    >
    > /myimgs/fakeimage3.png
    >
    > the first two are actual png images and the last one is a text file
    > renamed as .png
    > Is there some way to test for the valid images?
    >
    > I tried this
    >
    > public boolean checkIfImageFile(String filename){
    > boolean isimage=false;
    > try{
    > BufferedImage bi=ImageIO.read(new File(filename));
    > if (bi!=null)isimage=true;
    > }catch(Exception e){
    > isimage=false;
    > }
    >
    > return isimage;
    > }
    >
    > Is this the way to do such a test?If there is a proper way please tell
    > me.


    The technique looks fine.

    I may have written the code slightly different, but it does not matter.

    Arne
    Arne Vajhøj, Oct 28, 2010
    #3
  4. damon

    Roedy Green Guest

    On Thu, 28 Oct 2010 10:25:29 -0700 (PDT), damon
    <> wrote, quoted or indirectly quoted someone who
    said :

    >
    >the first two are actual png images and the last one is a text file
    >renamed as .png
    >Is there some way to test for the valid images?


    Have a look at the code in the ImageInfo class part of the Common11
    package you can download at
    http://mindprod.com/products1.html#COMMON11
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    Microsoft has a new version out, Windows XP, which according to everybody is the "most reliable Windows ever." To me, this is like saying that asparagus is "the most articulate vegetable ever."
    ~ Dave Barry
    Roedy Green, Oct 29, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tomasz Judycki

    Socked not valid - how to check it?

    Tomasz Judycki, Mar 3, 2004, in forum: Perl
    Replies:
    0
    Views:
    472
    Tomasz Judycki
    Mar 3, 2004
  2. Shapper
    Replies:
    4
    Views:
    9,307
    leotiger
    Sep 20, 2006
  3. Varangian

    Check Image is over another Image

    Varangian, Mar 31, 2006, in forum: ASP .Net
    Replies:
    9
    Views:
    368
    Varangian
    Apr 5, 2006
  4. Filip De Backer

    Check if image file exists before linking to Image object

    Filip De Backer, Jun 11, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    294
    Filip De Backer
    Jun 11, 2004
  5. Replies:
    0
    Views:
    82
Loading...

Share This Page