美文网首页Android开发Android开发Android技术知识
Android获取Camera录制的视频的地理位置

Android获取Camera录制的视频的地理位置

作者: blingbling_5a3f | 来源:发表于2018-07-15 20:01 被阅读16次

    前段时间在做视频播放器的时候碰到一个新需求:需要将视频的地理位置显示在视频的详情里面。第一反应就是每一个视频文件都可以记录下来一些信息,这些信息里面包含了经纬度等等,就像照片的ExifInterface类一样。于是就去查看了MediaRecorder类,便发现了这个类里面有public void setLocation(float latitude, float longitude)这么个方法可以给录制的视频设置经纬度。

    /**
         * Set and store the geodata (latitude and longitude) in the output file.
         * This method should be called before prepare(). The geodata is
         * stored in udta box if the output format is OutputFormat.THREE_GPP
         * or OutputFormat.MPEG_4, and is ignored for other output formats.
         * The geodata is stored according to ISO-6709 standard.
         *
         * @param latitude latitude in degrees. Its value must be in the
         * range [-90, 90].
         * @param longitude longitude in degrees. Its value must be in the
         * range [-180, 180].
         *
         * @throws IllegalArgumentException if the given latitude or
         * longitude is out of range.
         *
         */
        public void setLocation(float latitude, float longitude) {
            int latitudex10000  = (int) (latitude * 10000 + 0.5);
            int longitudex10000 = (int) (longitude * 10000 + 0.5);
    
            if (latitudex10000 > 900000 || latitudex10000 < -900000) {
                String msg = "Latitude: " + latitude + " out of range.";
                throw new IllegalArgumentException(msg);
            }
            if (longitudex10000 > 1800000 || longitudex10000 < -1800000) {
                String msg = "Longitude: " + longitude + " out of range";
                throw new IllegalArgumentException(msg);
            }
    
            setParameter("param-geotag-latitude=" + latitudex10000);
            setParameter("param-geotag-longitude=" + longitudex10000);
        }
    

    所以说明视频文件里面是存储了经纬度的,现在的关键就是找到对应的API去获取视频文件存储的经纬度。回忆以前获取视频的某一帧图像使用的是MediaMetadataRetriever类,通过这个对象是否也可以获取一些别的信息呢?创建mediaMetadataRetriever对象后发现了这么个方法:mediaMetadataRetriever.extractMetadata(int keyCode);看见这个方法名就感觉找到了(提炼出元数据),现在还需要一个关键的keyCode。于是进入到这个类里面浏览源码,发现了一大堆的key:

    /**
         * The metadata key to retrieve the numeric string describing the
         * order of the audio data source on its original recording.
         */
        public static final int METADATA_KEY_CD_TRACK_NUMBER = 0;
        /**
         * The metadata key to retrieve the information about the album title
         * of the data source.
         */
        public static final int METADATA_KEY_ALBUM           = 1;
        /**
         * The metadata key to retrieve the information about the artist of
         * the data source.
         */
        public static final int METADATA_KEY_ARTIST          = 2;
        /**
         * The metadata key to retrieve the information about the author of
         * the data source.
         */
        public static final int METADATA_KEY_AUTHOR          = 3;
        /**
         * The metadata key to retrieve the information about the composer of
         * the data source.
         */
        public static final int METADATA_KEY_COMPOSER        = 4;
        /**
         * The metadata key to retrieve the date when the data source was created
         * or modified.
         */
        public static final int METADATA_KEY_DATE            = 5;
        /**
         * The metadata key to retrieve the content type or genre of the data
         * source.
         */
        public static final int METADATA_KEY_GENRE           = 6;
        /**
         * The metadata key to retrieve the data source title.
         */
        public static final int METADATA_KEY_TITLE           = 7;
        /**
         * The metadata key to retrieve the year when the data source was created
         * or modified.
         */
        public static final int METADATA_KEY_YEAR            = 8;
        /**
         * The metadata key to retrieve the playback duration of the data source.
         */
        public static final int METADATA_KEY_DURATION        = 9;
        /**
         * The metadata key to retrieve the number of tracks, such as audio, video,
         * text, in the data source, such as a mp4 or 3gpp file.
         */
        public static final int METADATA_KEY_NUM_TRACKS      = 10;
        /**
         * The metadata key to retrieve the information of the writer (such as
         * lyricist) of the data source.
         */
        public static final int METADATA_KEY_WRITER          = 11;
        /**
         * The metadata key to retrieve the mime type of the data source. Some
         * example mime types include: "video/mp4", "audio/mp4", "audio/amr-wb",
         * etc.
         */
        public static final int METADATA_KEY_MIMETYPE        = 12;
        /**
         * The metadata key to retrieve the information about the performers or
         * artist associated with the data source.
         */
        public static final int METADATA_KEY_ALBUMARTIST     = 13;
        /**
         * The metadata key to retrieve the numberic string that describes which
         * part of a set the audio data source comes from.
         */
        public static final int METADATA_KEY_DISC_NUMBER     = 14;
        /**
         * The metadata key to retrieve the music album compilation status.
         */
        public static final int METADATA_KEY_COMPILATION     = 15;
        /**
         * If this key exists the media contains audio content.
         */
        public static final int METADATA_KEY_HAS_AUDIO       = 16;
        /**
         * If this key exists the media contains video content.
         */
        public static final int METADATA_KEY_HAS_VIDEO       = 17;
        /**
         * If the media contains video, this key retrieves its width.
         */
        public static final int METADATA_KEY_VIDEO_WIDTH     = 18;
        /**
         * If the media contains video, this key retrieves its height.
         */
        public static final int METADATA_KEY_VIDEO_HEIGHT    = 19;
        /**
         * This key retrieves the average bitrate (in bits/sec), if available.
         */
        public static final int METADATA_KEY_BITRATE         = 20;
        /**
         * This key retrieves the language code of text tracks, if available.
         * If multiple text tracks present, the return value will look like:
         * "eng:chi"
         * @hide
         */
        public static final int METADATA_KEY_TIMED_TEXT_LANGUAGES      = 21;
        /**
         * If this key exists the media is drm-protected.
         * @hide
         */
        public static final int METADATA_KEY_IS_DRM          = 22;
        /**
         * This key retrieves the location information, if available.
         * The location should be specified according to ISO-6709 standard, under
         * a mp4/3gp box "@xyz". Location with longitude of -90 degrees and latitude
         * of 180 degrees will be retrieved as "-90.0000+180.0000", for instance.
         */
        public static final int METADATA_KEY_LOCATION        = 23;
        /**
         * This key retrieves the video rotation angle in degrees, if available.
         * The video rotation angle may be 0, 90, 180, or 270 degrees.
         */
        public static final int METADATA_KEY_VIDEO_ROTATION = 24;
        /**
         * This key retrieves the original capture framerate, if it's
         * available. The capture framerate will be a floating point
         * number.
         */
        public static final int METADATA_KEY_CAPTURE_FRAMERATE = 25;
    

    这里我所需要的仅仅是:public static final int METADATA_KEY_LOCATION = 23;

    /**
         * 获取视频保存的地理位置信息
         *
         * @return +22.000+119.999
         */
        public static String getVideoLocationInfo(String path) {
            MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
            try {
                metadataRetriever.setDataSource(path);
            } catch (Exception e) {
                e.printStackTrace();
                return "";
            }
            return metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_LOCATION);
        }
    

    返回的经纬度格式:+22.000+119.999,这里需要将经度部分和纬度部分分割开来。

    String s = getVideoLocationInfo(videoPath);
            if(!TextUtils.isEmpty(s)){
                    char[]chars=s.toCharArray();
                    String latitude=null;
                    String longitude=null;
                    for(int i=0;i<chars.length;i++){
            if((chars[i]=='+'||chars[i]=='-')&&i>0){
            latitude=s.substring(0,i);
            longitude=s.substring(i,chars.length);
            break;
            }
            }
            double dLat=Double.parseDouble(latitude);
            double dLon=Double.parseDouble(longitude);
            }
    

    最后通过Geocoder解析出经纬度对应的具体城市位置:

    @WorkerThread
        private static void getAddress(Activity a, double latitude, double longitude, final TextView localeTxt) {
            Geocoder geocoder = new Geocoder(a);
            final StringBuilder stringBuilder = new StringBuilder();
            try {
                //根据经纬度获取地理位置信息
                List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
                if (addresses.size() > 0) {
                    Address address = addresses.get(0);
                    String countryName = address.getCountryName();//国家
                    String adminArea = address.getAdminArea();//省份
                    String locality = address.getLocality();//市
                    String subLocality = address.getSubLocality();//区
                    String thoroughfare = address.getThoroughfare();//街道
                    if (!TextUtils.isEmpty(countryName)) {
                        stringBuilder.append(countryName).append(a.getResources().getString(R.string.divide));
                    }
                    if (!TextUtils.isEmpty(adminArea)) {
                        stringBuilder.append(adminArea).append(a.getResources().getString(R.string.divide));
                    }
                    if (!TextUtils.isEmpty(locality)) {
                        stringBuilder.append(locality).append(a.getResources().getString(R.string.divide));
                    }
                    if (!TextUtils.isEmpty(subLocality)) {
                        stringBuilder.append(subLocality).append(a.getResources().getString(R.string.divide));
                    }
                    if (!TextUtils.isEmpty(thoroughfare)) {
                        stringBuilder.append(thoroughfare).append(a.getResources().getString(R.string.divide));
                    }
                    stringBuilder.deleteCharAt(stringBuilder.lastIndexOf(a.getResources().getString(R.string.divide)));
                }
            } catch (IOException e) {
                e.printStackTrace();
                a.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        localeTxt.setVisibility(View.GONE);
                    }
                });
            }
            final String s = stringBuilder.toString();
            if (!TextUtils.isEmpty(s)) {
                a.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        localeTxt.setText(s);
                    }
                });
            } else {
                a.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        localeTxt.setVisibility(View.GONE);
                    }
                });
            }
        }
    

    相关文章

      网友评论

        本文标题:Android获取Camera录制的视频的地理位置

        本文链接:https://www.haomeiwen.com/subject/cxfepftx.html