2008-09-17 42 views
8

我想提取一個jpg文件的創建日期。 Java對File對象具有lastModified方法,但似乎不支持從文件中提取創建的日期。我相信信息存儲在文件中,就像我在Win XP中將鼠標指針懸停在文件上時所看到的日期不同於我在DOS中使用帶有「dir/TC」的JNI所能獲得的日期。如何獲取在java中創建的日期圖片

回答

10

信息以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

0

您可能需要一些東西才能訪問exif數據。谷歌建議this library

4

我用這個元數據庫:http://www.drewnoakes.com/code/exif/

似乎工作得很好,但要記住,並不是所有的JPEG圖像有這個信息,所以它不可能是100%萬無一失的。

如果EXIF元數據不包含創建日期,那麼您可能不得不使用Java的lastUpdated - 除非您想使用Runtime.exec(...)並使用系統函數來查明(我不會推薦這個,但是!)

+0

呀,它爲一些我的圖像,並不適用於存儲在我們的數據庫中的圖像數據。 – 2016-04-18 11:39:41

0

下面的代碼示例要求一個文件路徑的用戶,然後輸出的創建日期和時間:

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();} 
    } 
} 
相關問題