如何获取用Java创建的日期图片
时间:2020-03-05 18:59:00 来源:igfitidea点击:
我想提取创建jpg文件的日期。 Java具有File对象的lastModified方法,但似乎不支持从文件中提取创建的日期。我认为信息存储在文件中的日期与在Win XP中将鼠标指针悬停在文件上时的日期不同,这与在DOS中对文件中的" dir / TC"使用JNI所获得的日期有所不同。
解决方案
回答
日期存储在jpeg的EXIF数据中。在Java中有一个Java库和一个查看器可能会有所帮助。
回答
我们可能需要一些东西来访问exif数据。 Google建议使用此库。
回答
我使用此元数据库:http://www.drewnoakes.com/code/exif/
尽管要记住并非所有的JPEG图像都具有此信息,但看起来似乎效果很好,因此不能做到100%可靠。
如果EXIF元数据不包含创建日期,那么除非我们想求助于Runtime.exec(...)并使用系统函数来找出(我不会,还是推荐这个!)
回答
信息以称为EXIF或者链接文本的格式存储在图像中。那里有几个图书馆可以读取这种格式,例如
回答
下面的代码示例向用户询问文件路径,然后输出创建日期和时间:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(final String[] args) { try { // get runtime environment and execute child process Runtime systemShell = Runtime.getRuntime(); BufferedReader br1=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter filename: "); String fname=(String)br1.readLine(); Process output = systemShell.exec("cmd /c dir /a "+fname); // open reader to get output from process BufferedReader br = new BufferedReader (new InputStreamReader(output.getInputStream())); String out=""; String line = null; int step=1; while((line = br.readLine()) != null ) { if(step==6) { out=line; } step++; } // display process output try{ out=out.replaceAll(" ",""); System.out.println("CreationDate: "+out.substring(0,10)); System.out.println("CreationTime: "+out.substring(10,15)); } catch(StringIndexOutOfBoundsException se) { System.out.println("File not found"); } } catch (IOException ioe){ System.err.println(ioe); } catch (Throwable t) { t.printStackTrace();} } }