【问题标题】:Saving and getting Latitude and Longitude for a mp4 file in android 7.0在 android 7.0 中保存和获取 mp4 文件的纬度和经度
【发布时间】:2016-12-01 12:06:17
【问题描述】:

您好,我在 android 7.0 中捕获了一个视频,并获得了视频文件路径。我需要将纬度和经度存储在视频文件中,即(拍摄视频的位置),就像我们通常在使用 Exif 接口保存 gps 坐标的图像中所做的那样。 但是对 mp4 文件使用 Exif 接口不起作用,而是出现错误:

java.lang.UnsupportedOperationException:ExifInterface 仅支持保存 JPEG 格式的属性。

我想过使用下面的代码来保存和获取视频文件的纬度、经度,但它不起作用。 这是我用来保存和获取图像 gps 坐标的代码:

保存图像的纬度和经度:

public void setImageLatitudeLongitude(File imagePath, double imageLat, double imageLong) {
        Log.e("Cordinates", "LAT : " + imageLat + "," + " LONG : " + imageLong);
        try {
            ExifInterface exif = new ExifInterface(imagePath.getPath());
            int num1Lat = (int) Math.floor(imageLat);
            int num2Lat = (int) Math.floor((imageLat - num1Lat) * 60);
            double num3Lat = (imageLat - ((double) num1Lat + ((double) num2Lat / 60))) * 3600000;

            int num1Lon = (int) Math.floor(imageLong);
            int num2Lon = (int) Math.floor((imageLong - num1Lon) * 60);
            double num3Lon = (imageLong - ((double) num1Lon + ((double) num2Lon / 60))) * 3600000;

            exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, num1Lat + "/1," + num2Lat + "/1," + num3Lat + "/1000");
            exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, num1Lon + "/1," + num2Lon + "/1," + num3Lon + "/1000");

            Log.e("DMS LAT", num1Lat + "/1," + num2Lat + "/1," + num3Lat + "/1000");
            Log.e("DMS LONG", num1Lon + "/1," + num2Lon + "/1," + num3Lon + "/1000");
            if (imageLat > 0) {
                exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "N");
            } else {
                exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "S");
            }

            if (imageLong > 0) {
                exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "E");
            } else {
                exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "W");
            }

            exif.saveAttributes();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

从图像中获取纬度和经度:

public ArrayList getImageLatitudeLongitude(File imagePath) {
        double imageLat = 0.0, imageLong = 0.0;
        ArrayList<Double> arrayList = new ArrayList<Double>();
        try {
            ExifInterface exif = new ExifInterface(imagePath.getPath());
            String latitude = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
            String longitude = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);

            if (latitude != null && longitude != null) {
                imageLat = convertToDegree(latitude);
                imageLong = convertToDegree(longitude);
                Log.e("Convert LAT", String.valueOf(convertToDegree(latitude)));
                Log.e("Convert LONG", String.valueOf(convertToDegree(longitude)));
            }
            arrayList.add(imageLat);
            arrayList.add(imageLong);
        } catch (IOException e) {
            arrayList.add(imageLat);
            arrayList.add(imageLong);
            e.printStackTrace();
        }

        return arrayList;
    }



private Float convertToDegree(String stringDMS) {
        Float result = null;
        String[] DMS = stringDMS.split(",", 3);

        String[] stringD = DMS[0].split("/", 2);
        Double D0 = new Double(stringD[0]);
        Double D1 = new Double(stringD[1]);
        Double FloatD = D0 / D1;

        String[] stringM = DMS[1].split("/", 2);
        Double M0 = new Double(stringM[0]);
        Double M1 = new Double(stringM[1]);
        Double FloatM = M0 / M1;

        String[] stringS = DMS[2].split("/", 2);
        Double S0 = new Double(stringS[0]);
        Double S1 = new Double(stringS[1]);
        Double FloatS = S0 / S1;

        result = new Float(FloatD + (FloatM / 60) + (FloatS / 3600));

        return result;
    }

还有如何获取视频文件的经纬度?有没有人遇到过和我一样的问题,如果有,请提出解决这个问题的方法。提前致谢!

【问题讨论】:

  • 阅读 mp4 规范怎么样?查看存储元数据的位置。
  • @AlexWien 嗨,你是说我应该去更深入地研究 mp4 文件规范?

标签: android video gps


【解决方案1】:

ExifInterface 仅支持获取图像文件的属性。 您可以使用 MediaMetadataRetriever 类来获取视频 mp4 文件的位置数据。例如:

MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
    mediaMetadataRetriever.setDataSource(videoPath);
    String location = mediaMetadataRetriever
            .extractMetadata(MediaMetadataRetriever.METADATA_KEY_LOCATION);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 2012-06-30
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多