保存并检索JPEG图像内的位置

时间:2015-11-24 08:09:55

标签: android image image-processing location jpeg

我将分享一个解决方案,其中包括在JPEG图像文件中保存和检索位置。使用ExifInterface在图像元数据中保存和检索纬度和经度 有关ExifInterface的更多信息,请点击此处 http://developer.android.com/reference/android/media/ExifInterface.html

2 个答案:

答案 0 :(得分:2)

public void saveLocation() {
    ExifInterface exif = null;
    try {
        exif = new ExifInterface(imagePath);
        exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, latitude);
        exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, longitude);
        exif.saveAttributes();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void retriveLocation() {
    ExifInterface exif = null;
    try {
        exif = new ExifInterface(imagePath);
        String[] latitudeValue = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE).split(",");
        String[] longitudeValue = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE).split(",");
        String[] tmp = new String[2];
        tmp = latitudeValue[0].split("/");
        setLatitude(String.valueOf(Float.valueOf(tmp[0]) / Float.valueOf(tmp[1])));
        tmp = longitudeValue[0].split("/");
        setLongitude(String.valueOf(Float.valueOf(tmp[0]) / Float.valueOf(tmp[1])));

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


}

答案 1 :(得分:0)

使用ExifInterface,您可以从媒体获取以下信息。

变量声明。

String mediaDateTime,attrLATITUDE,attrLATITUDE_REF,attrLONGITUDE,attrLONGITUDE_REF,zip, city,state, country;;
Double Latitude, Longitude;
List<Address> addresses;
Geocoder geocoder;

用户ExifInterface使用以下方式。

    ExifInterface exifInterfaceMedia = new ExifInterface(<Your Image Path>);
    // This will give you data and time 
    mediaDateTime = exifInterfaceMedia.getAttribute(ExifInterface.TAG_DATETIME);

    attrLATITUDE = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
    attrLATITUDE_REF = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
    attrLONGITUDE = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
    attrLONGITUDE_REF = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);

    if ((attrLATITUDE != null) && (attrLATITUDE_REF != null) && (attrLONGITUDE != null) && (attrLONGITUDE_REF != null)) {
            valid = true;

            if (attrLATITUDE_REF.equals("N")) {
                    Latitude = convertToDegree(attrLATITUDE);
            } else {
                    Latitude = 0 - convertToDegree(attrLATITUDE);
            }
            if (attrLONGITUDE_REF.equals("E")) {
                    Longitude = convertToDegree(attrLONGITUDE);
            } else {
                    Longitude = 0 - convertToDegree(attrLONGITUDE);
            }

            try {
                    addresses = geocoder.getFromLocation(Latitude, Longitude, 1);
            } catch (IOException e) {
                    e.printStackTrace();
            }
            if (addresses != null && addresses.size() > 0) {

                    zip = addresses.get(0).getPostalCode();
                    city = addresses.get(0).getLocality();
                    state = addresses.get(0).getAdminArea();
                    country = addresses.get(0).getCountryName();
                    if (zip != null) {
                        title += zip + ",";
                    }
                    if (city != null) {
                        title += city + ",";
                    }
                    if (state != null) {
                        title += state + ",";
                    }
                    if (country != null) {
                        title += country;
                    }

            } else {
                    title = "Unknown Location";
                }
}

private Double convertToDegree(String stringDMS) {
        Double 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 Double(FloatD + (FloatM / 60) + (FloatS / 3600));

        return result;
}