获取照片的Exif信息
在android.provider.MediaStore包中提供了ExifInterface类,ExifInterface类就是关于照片Exif信息的类,它的构造方法可以传入一个文件路径或文件对象。
通过ExifInterface的getAttribute()方法就可以获取对应的ExifI信息。而这个方法的参数值都是ExifInterface的常量,可以进入ExifInterface查看,也可以通过反射获取filed值。
获取Exif信息的代码
/**
* @param path 图片路径
*/
private void getInfo(String path) {
try {
ExifInterface exifInterface = new ExifInterface(path);
String guangquan = exifInterface.getAttribute(ExifInterface.TAG_APERTURE);
String shijain = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
String baoguangshijian = exifInterface.getAttribute(ExifInterface.TAG_EXPOSURE_TIME);
String jiaoju = exifInterface.getAttribute(ExifInterface.TAG_FOCAL_LENGTH);
String chang = exifInterface.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
String kuan = exifInterface.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
String moshi = exifInterface.getAttribute(ExifInterface.TAG_MODEL);
String zhizaoshang = exifInterface.getAttribute(ExifInterface.TAG_MAKE);
String iso = exifInterface.getAttribute(ExifInterface.TAG_ISO);
String jiaodu = exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION);
String baiph = exifInterface.getAttribute(ExifInterface.TAG_WHITE_BALANCE);
String altitude_ref = exifInterface.getAttribute(ExifInterface
.TAG_GPS_ALTITUDE_REF);
String altitude = exifInterface.getAttribute(ExifInterface.TAG_GPS_ALTITUDE);
String latitude = exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
String latitude_ref = exifInterface.getAttribute(ExifInterface
.TAG_GPS_LATITUDE_REF);
String longitude_ref = exifInterface.getAttribute(ExifInterface
.TAG_GPS_LONGITUDE_REF);
String longitude = exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
String timestamp = exifInterface.getAttribute(ExifInterface.TAG_GPS_TIMESTAMP);
String processing_method = exifInterface.getAttribute(ExifInterface
.TAG_GPS_PROCESSING_METHOD);
//转换经纬度格式
double lat = score2dimensionality(latitude);
double lon = score2dimensionality(longitude);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("光圈 = " + guangquan+"\n")
.append("时间 = " + shijain+"\n")
.append("曝光时长 = " + baoguangshijian+"\n")
.append("焦距 = " + jiaoju+"\n")
.append("长 = " + chang+"\n")
.append("宽 = " + kuan+"\n")
.append("型号 = " + moshi+"\n")
.append("制造商 = " + zhizaoshang+"\n")
.append("ISO = " + iso+"\n")
.append("角度 = " + jiaodu+"\n")
.append("白平衡 = " + baiph+"\n")
.append("海拔高度 = " + altitude_ref+"\n")
.append("GPS参考高度 = " + altitude+"\n")
.append("GPS时间戳 = " + timestamp+"\n")
.append("GPS定位类型 = " + processing_method+"\n")
.append("GPS参考经度 = " + latitude_ref+"\n")
.append("GPS参考纬度 = " + longitude_ref+"\n")
.append("GPS经度 = " + lat+"\n")
.append("GPS经度 = " + lon+"\n");
//将获取的到的信息设置到TextView上
mText.setText(stringBuilder.toString());
/**
* 将wgs坐标转换成百度坐标
* 就可以用这个坐标通过百度SDK 去获取该经纬度的地址描述
*/
double[] wgs2bd = GpsUtil.wgs2bd(lat, lon);
} catch (IOException e) {
e.printStackTrace();
}
}
经纬度显示形式转换
经纬度一般有以下几种显示形式
- ddd.ddd (度.度)112.99434397362694
- ddd.mm.sss (度.分.秒)112°58′39.0971″
度分秒的换算
- 1度=60分
- 1分=60秒
- 1度=3600秒
通过ExifInterface获取到的经纬度显示方式是112/1,58/1,390971/10000,它和上面的两种都不一样,但可以看出它通过,
将整个整个数值分成了3份,其实它就相当于了112/1度,58/1分,390971/10000秒。中间的/
就是除号,所以它其实就是112°58′39.0971″。
将度分秒的形式转换成度的显示形式
通过上面度分秒的换算可以知道,用度+分/60+秒/3600就等于度的显示形式。
所以:112+58/60+39.0971/3600=112.99434397362694 就是度的
换算代码的编写
/**
* 将 112/1,58/1,390971/10000 格式的经纬度转换成 112.99434397362694格式
* @param string 度分秒
* @return 度
*/
private double score2dimensionality(String string) {
double dimensionality = 0.0;
if (null==string){
return dimensionality;
}
//用 ,将数值分成3份
String[] split = string.split(",");
for (int i = 0; i < split.length; i++) {
String[] s = split[i].split("/");
//用112/1得到度分秒数值
double v = Double.parseDouble(s[0]) / Double.parseDouble(s[1]);
//将分秒分别除以60和3600得到度,并将度分秒相加
dimensionality=dimensionality+v/Math.pow(60,i);
}
return dimensionality;
}
坐标转换
一共有以下几种坐标类型
- WGS84:大地坐标系,目前广泛使用的GPS全球卫星定位系统使用的坐标系;
- GCJ02:火星坐标,是由中国国家测绘局制订的地理信息系统的坐标系统,是由WGS84坐标系经加密后的坐标系;
- BD09:百度坐标系
照片Exif信息中存储的都是WGS84坐标的经纬度,如果要使用百度的SDK通过经纬度获取位置描述信息,就需要转换成BD09坐标系。
坐标系转换工具代码如下(在网上找的,原出处找不到了,有个类似的:http://blog.csdn.net/ma969070578/article/details/41013547)
public class GpsUtil {
static double pi = 3.14159265358979324;
static double a = 6378245.0;
static double ee = 0.00669342162296594323;
public final static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
public static double[] wgs2bd(double lat, double lon) {
double[] wgs2gcj = wgs2gcj(lat, lon);
double[] gcj2bd = gcj2bd(wgs2gcj[0], wgs2gcj[1]);
return gcj2bd;
}
public static double[] gcj2bd(double lat, double lon) {
double x = lon, y = lat;
double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
double bd_lon = z * Math.cos(theta) + 0.0065;
double bd_lat = z * Math.sin(theta) + 0.006;
return new double[] { bd_lat, bd_lon };
}
public static double[] bd2gcj(double lat, double lon) {
double x = lon - 0.0065, y = lat - 0.006;
double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
double gg_lon = z * Math.cos(theta);
double gg_lat = z * Math.sin(theta);
return new double[] { gg_lat, gg_lon };
}
public static double[] wgs2gcj(double lat, double lon) {
double dLat = transformLat(lon - 105.0, lat - 35.0);
double dLon = transformLon(lon - 105.0, lat - 35.0);
double radLat = lat / 180.0 * pi;
double magic = Math.sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
double mgLat = lat + dLat;
double mgLon = lon + dLon;
double[] loc = { mgLat, mgLon };
return loc;
}
private static double transformLat(double lat, double lon) {
double ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon + 0.2 * Math.sqrt(Math.abs(lat));
ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lon * pi) + 40.0 * Math.sin(lon / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(lon / 12.0 * pi) + 320 * Math.sin(lon * pi / 30.0)) * 2.0 / 3.0;
return ret;
}
private static double transformLon(double lat, double lon) {
double ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * Math.sqrt(Math.abs(lat));
ret += (20.0 * Math.sin(6.0 * lat * pi) + 20.0 * Math.sin(2.0 * lat * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lat * pi) + 40.0 * Math.sin(lat / 3.0 * pi)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(lat / 12.0 * pi) + 300.0 * Math.sin(lat / 30.0 * pi)) * 2.0 / 3.0;
return ret;
}
}