简体   繁体   English

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

[英]How to get date picture created in java

I would like to extract the date a jpg file was created. 我想提取一个jpg文件的创建日期。 Java has the lastModified method for the File object, but appears to provide no support for extracting the created date from the file. Java具有File对象的lastModified方法,但似乎不支持从文件中提取创建的日期。 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. 我相信信息存储在文件中,因为我在Win XP中将鼠标指针悬停在文件上时所看到的日期与我在DOS中使用带有“dir / TC”的JNI所获得的日期不同。

The information is stored within the image in a format called EXIF or link text . 信息以称为EXIF链接文本的格式存储在图像中。 There several libraries out there capable of reading this format, like this one 那里有几个能够读取这种格式的库,就像这个一样

The date is stored in the EXIF data in the jpeg. 日期存储在jpeg中的EXIF数据中。 There's a java library and a viewer in java that might be helpful. java中有一个java库和一个查看器可能会有所帮助。

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(...)并使用系统函数找出(我不会'但是推荐这个!)

You probably need something to access the exif data. 您可能需要一些东西来访问exif数据。 Google suggests this library . 谷歌建议这个库

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM