从图像 Java 代码中获取 GPS 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12114837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:38:47  来源:igfitidea点击:

Get GPS data from an image Java code

javaimagemetadatajpeg

提问by Arun

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

我想使用 Java 代码从本地系统中的图像文件中获取元数据

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

在附加的图像中,您可以看到我想从 Java 代码中提取的所需数据。

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();
        }

采纳答案by Jo?o Silva

There's no easyway 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 Imageclass of javaxt, which provides a very useful method to extract GPS metadata from an image. It is as simple as:

使用 Java Core API没有简单的方法可以做到这一点。您必须解析图像的元数据树,并解释正确的 EXIF 标签。相反,您可以从具有 EXIF 解析功能的现有库中选取所需的代码,并在您的库中使用它。例如,我使用了 的Imagejavaxt,它提供了一种非常有用的方法来从图像中提取 GPS 元数据。这很简单:

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

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

另外,javaxt.io.Image没有外部依赖项,因此如果您不想添加对整个库的依赖项,则可以仅使用该特定类。

回答by Dan D.

I suggest you read the EXIF header of the image and then parse the tags for finding the GPS information. In Java there is a great library (called metadata-extractor) for extracting and parsing the EXIF header. Please see the getting started for this library here.

我建议您阅读图像的 EXIF 标头,然后解析标签以查找 GPS 信息。在 Java 中有一个很棒的库(称为元数据提取器)用于提取和解析 EXIF 标头。请在此处查看此库的入门指南。

Once you do the first 2 steps in the tutorial, look for the tags starting with [GPS]([GPS] GPS Longitude, [GPS] GPS Latitude, ...).

完成本教程中的前 2 个步骤后,查找以[GPS]( [GPS] GPS Longitude, [GPS] GPS Latitude, ...)开头的标签。

回答by ThomasEdwin

Based on @dan-d answer, here is my code (kotlin)

基于@dan-d 的回答,这是我的代码(kotlin)

private fun readGps(file: String): Optional<GeoLocation> {
    // Read all metadata from the image

    // Read all metadata from the image
    val metadata: Metadata = ImageMetadataReader.readMetadata(File(file))
    // See whether it has GPS data
    val gpsDirectories = metadata.getDirectoriesOfType(
            GpsDirectory::class.java)
    for (gpsDirectory in gpsDirectories) {
        // Try to read out the location, making sure it's non-zero
        val geoLocation = gpsDirectory.geoLocation
        if (geoLocation != null && !geoLocation.isZero) {
            return Optional.of(geoLocation)
        }
    }
    return Optional.empty()
}