检查文件是否是图像 [英] Check if a file is an image
问题描述
我正在使用JAI并创建一个文件:
I am using JAI and create a file with:
PlanarImage img = JAI.create("fileload", myFilename);
如果文件存在,我会在该行前检查。但是我如何检查文件是.bmp还是.tiff或图像文件?
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?
有人知道吗?
推荐答案
Image Magick项目具有识别图像的功能,并且Image Magick的Java包装器名为JMagick,我认为您可能需要考虑而不是重新发明轮子:
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:
我一直在使用Image Magick,包括命令行中的识别功能,一旦确定图片,它就永远不会失败。
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.
回到我绝对需要该功能且JMagick不存在的日子我已经习惯了 Runtime.exec()
ImageMagick的识别来自Java的
命令并且它运行良好。
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.
现在JMagick存在这可能不再需要了(但我没有尝试了JMagick)。
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
这篇关于检查文件是否是图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!