如何获取在java中创建的日期图片

问题描述:

我想提取一个jpg文件的创建日期。 Java对File对象具有lastModified方法,但似乎不支持从文件中提取创建的日期。我相信信息存储在文件中,就像我在Win XP中将鼠标指针悬停在文件上时所看到的日期不同于我在DOS中使用带有“dir/TC”的JNI所能获得的日期。如何获取在java中创建的日期图片

信息以EXIF或的格式存储在图像中。有几个图书馆那里能够读取这种格式,像this one

+0

太棒了!感谢所有有用的评论!我相信我会在drewnoakes.com上使用这个库。 – user16029 2008-09-17 14:46:57

+0

可交换图像文件格式(正式Exif,**不EXIF **根据JEIDA/JEITA/CIPA规范)是一个标准... – 2014-10-06 18:31:21

日期存储在jpeg中的EXIF数据中。有一个java libraryviewer in java可能会有所帮助。

您可能需要一些东西才能访问exif数据。谷歌建议this library

我用这个元数据库:http://www.drewnoakes.com/code/exif/

似乎工作得很好,但要记住,并不是所有的JPEG图像有这个信息,所以它不可能是100%万无一失的。

如果EXIF元数据不包含创建日期,那么您可能不得不使用Java的lastUpdated - 除非您想使用Runtime.exec(...)并使用系统函数来查明(我不会推荐这个,但是!)

+0

呀,它为一些我的图像,并不适用于存储在我们的数据库中的图像数据。 – 2016-04-18 11:39:41

下面的代码示例要求一个文件路径的用户,然后输出的创建日期和时间:

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();} 
    } 
}