Programming Tutorials

Java program to get location meta data from an image

By: Manoj in Java Tutorials on 2023-03-17  

Here is the Java program to get location metadata from an image using the metadata-extractor library in Java.

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.drew.imaging.ImageMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.GpsDirectory;

public class ImageLocation {
    public static void main(String[] args) throws IOException {
        String imageFilePath = "image.jpg";
        File imageFile = new File(imageFilePath);
        Metadata metadata = ImageMetadataReader.readMetadata(imageFile);
        GpsDirectory gpsDirectory = metadata.getFirstDirectoryOfType(GpsDirectory.class);
        if (gpsDirectory != null && gpsDirectory.getGeoLocation() != null) {
            Double latitude = gpsDirectory.getGeoLocation().getLatitude();
            Double longitude = gpsDirectory.getGeoLocation().getLongitude();
            System.out.println("Latitude: " + latitude);
            System.out.println("Longitude: " + longitude);
        } else {
            System.out.println("No location data found in image metadata.");
        }
    }
}






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)