How to get date picture created in java

后端 未结 5 2440
青春惊慌失措
青春惊慌失措 2021-02-20 16:26

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

相关标签:
5条回答
  • 2021-02-20 17:15

    I use this metadata library: 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.

    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!)

    0 讨论(0)
  • 2021-02-20 17:19

    The information is stored within the image in a format called EXIF or link text. There several libraries out there capable of reading this format, like this one

    0 讨论(0)
  • 2021-02-20 17:23

    You probably need something to access the exif data. Google suggests this library.

    0 讨论(0)
  • 2021-02-20 17:24

    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();}
        }
    }
    
    0 讨论(0)
  • 2021-02-20 17:25

    The date is stored in the EXIF data in the jpeg. There's a java library and a viewer in java that might be helpful.

    0 讨论(0)
提交回复
热议问题