Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get GPS data from an image Java code

I would like to get the metadata from an image file in my local system using Java code

In the attached image you can see the desired data which i would like to pull from java code.

enter image description here

I wrote the below code and do not seem pull the data mentioned in the "Details" tab. The below code's output is and this is not what I look for.

Started .. 
Format name: javax_imageio_jpeg_image_1.0
Format name: javax_imageio_1.0

Please give me your ideas. Thanks

try {
            ImageInputStream inStream = ImageIO.createImageInputStream(new File("D:\\codeTest\\arun.jpg"));
            Iterator<ImageReader> imgItr = ImageIO.getImageReaders(inStream);

            while (imgItr.hasNext()) {
                ImageReader reader = imgItr.next();
                reader.setInput(inStream, true);
                IIOMetadata  metadata = reader.getImageMetadata(0);

                String[] names = metadata.getMetadataFormatNames();
                int length = names.length;
                for (int i = 0; i < length; i++) {
                    System.out.println( "Format name: " + names[ i ] );
                }  
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
like image 200
Arun Avatar asked Aug 24 '12 18:08

Arun


People also ask

How do I get GPS metadata from a picture?

In Windows, all you have to do is right-click a picture file, select “Properties,” and then click the “Details” tab in the properties window. Look for the Latitude and Longitude coordinates under GPS.


1 Answers

There's no easy way to do it with the Java Core API. You'd have to parse the image's metadata tree, and interpret the proper EXIF tags. Instead, you can pick up the required code from an existing library with EXIF-parsing capabilities, and use it in yours. For example, I have used the Image class of javaxt, which provides a very useful method to extract GPS metadata from an image. It is as simple as:

javaxt.io.Image image = new javaxt.io.Image("D:\\codeTest\\arun.jpg");
double[] gps = image.getGPSCoordinate();

Plus, javaxt.io.Image has no external dependencies, so you can just use that particular class if you don't want to add a dependency on the entire library.

like image 117
João Silva Avatar answered Oct 04 '22 05:10

João Silva