SoFunction
Updated on 2025-03-08

Java skills: Quickly get picture shooting time

Introduction

In development, sometimes it is necessary to read the shooting time information of the picture. This article will introduce the method of using Java to read the time of pictures, and provide some sample code to help new developers implement this function.

flow chart

erDiagram
    Read the picture and take the time --> Get picture files
    Read the picture and take the time --> ReadEXIFinformation
    ReadEXIFinformation --> Analyze shooting time

step

The following are the steps to realize the time to read the picture shooting:

step describe
Get picture files Get the image file to be read from the file system
Read EXIF ​​information Read EXIF ​​information of an image using Java's ExifInterface class
Analyze shooting time Parse the shooting time of the picture from the EXIF ​​information
Show shooting time Show pictures on console or other interfaces

Code Example

Get picture files

First, we need to get the image file to be read from the file system. You can use Java File class to implement:

String imagePath = "path/to/";
File imageFile = new File(imagePath);

Read EXIF ​​information

Next, we need to read the EXIF ​​information of the image. You can use third-party libraries such as metadata-extractor to parse the EXIF ​​information of the picture. First, you need to add the metadata-extractor library to the project's dependencies.

<dependency>
    <groupId></groupId>
    <artifactId>metadata-extractor</artifactId>
    <version>2.16.0</version>
</dependency>

Then, you can use the following code to read the EXIF ​​information of the picture:

try {
    Metadata metadata = (imageFile);
    ExifSubIFDDirectory directory = ();
    Date date = (ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL);
} catch (ImageProcessingException | IOException e) {
    ();
}

Analyze shooting time

After reading the EXIF ​​information, we can parse the shooting time of the picture from it. It should be noted that different pictures may store the labels for shooting time slightly different, and can be adjusted according to specific needs.

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
String shootTime = (date);

Show shooting time

Finally, we can display the shooting time of the picture on the console or other interface:

("Picture shooting time:" + shootTime);

Complete code example

Here is the complete sample code:

import ;
import ;
import ;
import ;

import ;
import ;
import ;
import .ExifIFD0Directory;
import ;

public class ImageMetadataExample {
    public static void main(String[] args) {
        String imagePath = "path/to/";
        File imageFile = new File(imagePath);

        try {
            Metadata metadata = (imageFile);
            ExifSubIFDDirectory directory = ();
            Date date = (ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL);

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
            String shootTime = (date);

            ("Picture shooting time:" + shootTime);
        } catch (Exception e) {
            ();
        }
    }
}

in conclusion

Through the introduction of this article, I hope that new developers can understand how to use Java to read pictures to shoot. It should be noted that different pictures may store the labels for shooting time slightly different, and can be adjusted according to specific needs.

This is the article about Java skills: Quickly obtaining the time of image shooting. For more related Java reading of image shooting, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!