Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a file is an image

Tags:

java

image

jai

I am using JAI and create a file with:

PlanarImage img = JAI.create("fileload", myFilename);

I check before that line if the file exists. But how could I check if the file is a .bmp or a .tiff or an image file?

Does anyone know?

like image 642
Tim Avatar asked Feb 03 '10 14:02

Tim


People also ask

How do you check if a file is a JPG?

If you are having trouble and want to check if you photo is a JPEG, look at the writing under the photo in its file name. If it ends . jpg or . jpeg- then the file is a JPEG and will upload.

How can you tell if a file is a PNG?

PNG files start with 'PNG', . jpg files should have 'exif'or 'JFIF' somewhere in the beginning. Use file command: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/file.1.html For example: file 'ysauyft87ggsa67fgeg&w=1600'


4 Answers

The Image Magick project has facilities to identify image and there's a Java wrapper for Image Magick called JMagick which I think you may want to consider instead of reinventing the wheel:

http://www.jmagick.org

I'm using Image Magick all the time, including its "identify" feature from the command line and it never failed once to identify a picture.

Back in the days where I absolutely needed that feature and JMagick didn't exist yet I used to Runtime.exec() ImageMagick's identify command from Java and it worked perfectly.

Nowadays that JMagick exist this is probably not necessary anymore (but I haven't tried JMagick yet).

Note that it gives much more than just the format, for example:

$  identify tmp3.jpg 
tmp3.jpg JPEG 1680x1050 1680x1050+0+0 DirectClass 8-bit 293.582kb 

$  identify tmp.png
tmp.png PNG 1012x900 1012x900+0+0 DirectClass 8-bit 475.119kb
like image 54
SyntaxT3rr0r Avatar answered Oct 05 '22 13:10

SyntaxT3rr0r


Try using the width of the image:

boolean isImage(String image_path){
  Image image = new ImageIcon(image_path).getImage();
  if(image.getWidth(null) == -1){
        return false;
  }
  else{
        return true;
  }
}

if the width is -1 then is not image.

like image 30
Estuardo López Avatar answered Oct 05 '22 12:10

Estuardo López


To tell if something is a png, I've used this below snippet in Android java.

public CompressFormat getCompressFormat(Context context, Uri fileUri) throws IOException {
    // create input stream
    int numRead;
    byte[] signature = new byte[8];
    byte[] pngIdBytes = { -119, 80, 78, 71, 13, 10, 26, 10 };
    InputStream is = null;

    try {
        ContentResolver resolver = context.getContentResolver();
        is = resolver.openInputStream(fileUri);

        // if first 8 bytes are PNG then return PNG reader
        numRead = is.read(signature);

        if (numRead == -1)
            throw new IOException("Trying to reda from 0 byte stream");

    } finally {
        if (is != null)
            is.close();
    }

    if (numRead == 8 && Arrays.equals(signature, pngIdBytes)) {
        return CompressFormat.PNG;
    }

    return null;
}
like image 40
Matt Wear Avatar answered Oct 05 '22 13:10

Matt Wear


At the beginning of files, there is an identifying character sequence. For example JPEG files starts with FF D8 FF.

You can check for this sequence in your program but I am not sure whether this works for every file.

For information about identifying characters you can have a look at http://filext.com

like image 42
Birkan Cilingir Avatar answered Oct 05 '22 13:10

Birkan Cilingir