Java program to get location from an image

Here is the Java programs 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.");
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.