java 如何获取在java中创建的日期图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/83787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get date picture created in java
提问by user16029
I would like to extract the date a jpg file was created. Java has the lastModified method for the File object, but appears to provide no support for extracting the created date from the file. I believe the information is stored within the file as the date I see when I hover the mouse pointer over the file in Win XP is different than what I can get by using JNI with "dir /TC" on the file in DOS.
我想提取创建 jpg 文件的日期。Java 具有 File 对象的 lastModified 方法,但似乎不支持从文件中提取创建日期。我相信信息存储在文件中,因为当我在 Win XP 中将鼠标指针悬停在文件上时我看到的日期与我在 DOS 中在文件上使用带有“dir /TC”的 JNI 获得的日期不同。
回答by amo-ej1
回答by pjz
The date is stored in the EXIFdata in the jpeg. There's a java libraryand a viewer in javathat might be helpful.
回答by pjz
I use this metadata library: http://www.drewnoakes.com/code/exif/
我使用这个元数据库:http: //www.drewnoakes.com/code/exif/
Seems to work pretty well, although bear in mind that not all JPEG images have this information, so it can't be 100% fool-proof.
似乎工作得很好,但请记住,并非所有 JPEG 图像都具有此信息,因此不能 100% 万无一失。
If the EXIF metadata doesn't contain the created date, then you'll probably have to make do with Java's lastUpdated - unless you want to resort to Runtime.exec(...) and using system functions to find out (I wouldn't recommend this, though!)
如果 EXIF 元数据不包含创建日期,那么您可能不得不使用 Java 的 lastUpdated - 除非您想求助于 Runtime.exec(...) 并使用系统函数来找出(我不会)不过不推荐这个!)
回答by Dan Dyer
You probably need something to access the exifdata. Google suggests this library.
回答by Jorge Ferreira
The code example below asks the user for a file path and then outputs the creation date and time:
下面的代码示例要求用户提供文件路径,然后输出创建日期和时间:
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();}
}
}

