下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
一般情况下是java程序读取不到gps等扩展信息的。如果想要解析到里面的信息需要下载一个jar包,Metadata-extractor-2.6.4.jar(下载地址:
http://code.google.com/p/metadata-extractor/),这个jar提供了支持获取扩展信息的功能。
import java.io.File;
import java.io.IOException;
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
public class SampleUsage
{
/**
* 图片信息获取Metadata元数据信息
* @param fileName 需要解析的文件
* @return
*/
public ImgInfoBean parseImgInfo (String fileName)
{
File file = new File(fileName);
ImgInfoBean imgInfoBean = null;
try {
Metadata Metadata = ImageMetadataReader.readMetadata(file);
imgInfoBean = printImageTags(file,Metadata);
} catch (ImageProcessingException e) {
System.err.println("error 1a: " + e);
} catch (IOException e) {
System.err.println("error 1b: " + e);
}
return imgInfoBean;
}
/**
* 读取Metadata里面的信息
* @param sourceFile 源文件
* @param Metadata Metadata元数据信息
* @return
*/
private ImgInfoBean printImageTags(File sourceFile,Metadata Metadata)
{
ImgInfoBean imgInfoBean = new ImgInfoBean ();
imgInfoBean.setImgSize(sourceFile.getTotalSpace());
imgInfoBean.setImgName(sourceFile.getName());
for (Directory directory : Metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
String tagName = tag.getTagName();
String desc = tag.getDescription();
if (tagName.equals("Image Height")) {
//图片高度
imgInfoBean.setImgHeight(desc);
} else if (tagName.equals("Image Width")) {
//图片宽度
imgInfoBean.setImgWidth(desc);
} else if (tagName.equals("Date/Time Original")) {
//拍摄时间
imgInfoBean.setDateTime(desc);
} else if (tagName.equals("GPS Altitude")) {
//海拔
imgInfoBean.setAltitude(desc);
} else if (tagName.equals("GPS Latitude")) {
//纬度
imgInfoBean.setLatitude(pointToLatlong(desc));
} else if (tagName.equals("GPS Longitude")) {
//经度
imgInfoBean.setLongitude(pointToLatlong(desc));
}
}
for (String error : directory.getErrors()){
System.err.println("ERROR: " + error);
}
}
return imgInfoBean;
}
/**
* 经纬度转换 度分秒转换
* @param point 坐标点
* @return
*/
public String pointToLatlong (String point ) {
Double du = Double.parseDouble(point.substring(0,point.indexOf("°")).trim());
Double fen = Double.parseDouble(point.substring(point.indexOf("°")+1,point.indexOf("'")).trim());
Double miao = Double.parseDouble(point.substring(point.indexOf("'")+1,point.indexOf("\"")).trim());
Double duStr = du + fen / 60 + miao / 60 / 60 ;
return duStr.toString();
}
public static void main(String[] args)
{
ImgInfoBean imgInfoBean = new SampleUsage().parseImgInfo("C:\\DSC_4564.JPG");
System.out.println(imgInfoBean.toString());
}
}
文件信息bean类。
public class ImgInfoBean {
private String imgHeight ;//图片高度
private String imgWidth ;//图片宽度
private String dateTime ;//拍摄时间
private String altitude ;//海拔
private String latitude;//纬度
private String longitude ;//经度
private Long imgSize; //文件大小
private String imgName; //文件名称
public Long getImgSize() {
return imgSize;
}
public void setImgSize(Long imgSize) {
this.imgSize = imgSize;
}
public String getImgName() {
return imgName;
}
public void setImgName(String imgName) {
this.imgName = imgName;
}
public String getImgHeight() {
return imgHeight;
}
public void setImgHeight(String imgHeight) {
this.imgHeight = imgHeight;
}
public String getImgWidth() {
return imgWidth;
}
public void setImgWidth(String imgWidth) {
this.imgWidth = imgWidth;
}
public String getDateTime() {
return dateTime;
}
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
public String getAltitude() {
return altitude;
}
public void setAltitude(String altitude) {
this.altitude = altitude;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String toString (){
return "[图片信息]文件名称:"+ this.imgName+" 文件大小:"+this.imgSize +" 高度:"+this.imgHeight+" 宽度:"+this.imgWidth+" 拍摄时间:"+this.dateTime+" 海拔:"+this.altitude+" 纬度:"+this.latitude+" 经度:"+this.longitude;
}
}
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
总结
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250