tags:

views:

263

answers:

7

Hello.

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?

A: 

Well, short of opening the file and examining the contents, try

int b = myFileName.toUpperCase().indexOf(".BMP");
int j = myFileName.toUpperCase().indexOf(".JPG");
int t = myFileName.toUpperCase().indexOf(".TIF");

boolean isImage = (b > 0 && b == myFileName.length() - 4) ||
(t > 0 && t == myFileName.length() - 4) ||
(j > 0 && j == myFileName.length() - 4);
Matthew Flynn
You are only checking the filename if there is the extension of a image format. I want to check if it is really an image.Also it is easier to do a kind of substring/regular expression.
Tim
And for suffixes, I recommend the "public boolean endsWith(String suffix)" function of the String class.
Photodeus
I'd also suggest storing `myFileName.toUpperCase()` and `myFileName.length()` into new temporary variables, instead of using repeated calls
brian_d
A: 

The only (semi-)reliable way to determine the contents of a file is to open it and read the first few characters. Then you can use a set of tests such as implemented in the Unix file command to make an educated guess as to the contents of the file.

David Harris
A: 

You could use DROID, a tool for file format identification that also offers a Java API, to be used roughly like this:

AnalysisController controller = new AnalysisController();
controller.readSigFile(signatureFileLocation);
controller.addFile(fileToIdentify.getAbsolutePath());
controller.runFileFormatAnalysis();
Iterator<IdentificationFile> it = controller.getFileCollection().getIterator();

Documentation on the API usage is rather sparse, but you can have a look at this working example (the interesting part is in the identifyOneBinary method).

Fabian Steeg
+1  A: 

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

Birkan Cilingir
+3  A: 

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/index.html

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
Webinator
A: 

Expanding on Birkan's answer, there is a list of 'magic numbers' available here:

http://www.astro.keele.ac.uk/oldusers/rno/Computing/File_magic.html

I just checked a BMP and TIFF file (both just created in Windows XP / Paint), and they appear to be correct:

First two bytes "42 4d" -> BMP
First four bytes "4d 4d 00 2a" -> TIFF

I used VIM to edit the files and then did Tools | Convert to Hex, but you can also use 'od -c' or something similar to check them.

As a complete aside, I was slightly amused when I found out the magic numbers used for compiled Java Classes: 'ca fe ba be' - 'cafe babe' :)

monojohnny
A: 

Try using the standard JavaBeans Activation Framework (JAF)

With the JavaBeans Activation Framework standard extension, developers who use Java technology can take advantage of standard services to determine the type of an arbitrary piece of data, encapsulate access to it, discover the operations available on it, and to instantiate the appropriate bean to perform said operation(s). For example, if a browser obtained a JPEG image, this framework would enable the browser to identify that stream of data as an JPEG image, and from that type, the browser could locate and instantiate an object that could manipulate, or view that image.

Arturo Tena