Java 遍历文件下jpg图片并解析图片

 
 package filetest;

 import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList; import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
/**
* 文件的迭代输出
* 寻找某文件下以“.jpg”结尾的文件
* 并解析该图片
* */ class MyFilenameFilter implements FilenameFilter{
public boolean accept(File dir, String name){
return name.endsWith(".jpg")
|| new File(name).isDirectory();
}
} public class FileTest {
public static ArrayList<String> fiList = new ArrayList<String>();
public static int count = 0; public static void main(String[] agrs) throws JpegProcessingException, IOException{ File filePath = new File("E://psresource/Root");
String[] nameStrings = filePath.list(new MyFilenameFilter());
for(String name : nameStrings){
count++; File file = new File("E://psresource/Root/" + name);
fiList.add(file.getAbsolutePath());
File jpegFile = new File(file.getAbsolutePath());
try{
Metadata metadata = JpegMetadataReader.readMetadata(jpegFile); for(Directory directory : metadata.getDirectories()){
for(Tag tag : directory.getTags()){ System.out.println(file + "的tag信息为: " +tag);
}
}
}catch(JpegProcessingException e){
System.out.println("%%%%%%%%%%%%%%%%%"+jpegFile.getAbsolutePath());
if(jpegFile.delete()){
System.out.println(jpegFile.getAbsolutePath() + "已经被删除");
} e.printStackTrace();
} //System.out.println(Integer.toString(count) + file.getAbsolutePath()); }
//System.out.println(Integer.toString(count) + "所有的图片的名字:" + fiList.toString());
} } 图片的操作: 解析图片,需要两个包metadata-extractor-2.6.4.jar ,但是修改图片的EXIF信息,还要导入另一个jar包:mediautil-1.0.jar

图片的操作:

解析图片,需要两个包metadata-extractor-2.6.4.jar ,但是修改图片的EXIF信息,还要导入另一个jar包:mediautil-1.0.jar

 package exiftest;

 import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator; import mediautil.image.jpeg.Entry;
import mediautil.image.jpeg.Exif;
import mediautil.image.jpeg.LLJTran;
import mediautil.image.jpeg.LLJTranException;
import net.sf.json.JSONObject; import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag; /**
* 测试用于读取图片的EXIF信息
* @author Winter Lau
*/
public class Exiftest { /**
* 经纬度转换 度分秒转换
* @param point 坐标点
* @return
*/
public static 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) throws Exception {
//File jpegFile = new File("E:\\psresource\\img\\photos\\images\\20140627_140048.jpg");
File jpegFile = new File("E:\\psresource\\img\\photos\\wwd\\iphone\\20140521.jpg");
Metadata metadata = JpegMetadataReader.readMetadata(jpegFile); JSONObject jsonObject = new JSONObject(); Double lat = null;
Double lon=null;
Date date = null;
for(Directory directory : metadata.getDirectories()){
for(Tag tag : directory.getTags()){
System.out.println(tag); String tagname = tag.getTagName();
if(tagname.equals("GPS Longitude")){
System.out.println(pointToLatlong(tag.getDescription()));
lon = Double.valueOf(pointToLatlong(tag.getDescription()));
jsonObject.accumulate("lon", lon);
}
else if (tagname.equals("GPS Latitude")) {
System.out.println(pointToLatlong(tag.getDescription()));
lat = Double.valueOf(pointToLatlong(tag.getDescription()));
jsonObject.accumulate("lat", lat);
}
if(tagname.equals("Date/Time")){
System.out.println(tag.getDescription());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd hh:mm:ss"); Date date2 = sdf.parse(tag.getDescription());
Long dateLong = date2.getTime();
jsonObject.accumulate("time", dateLong); }
}
System.out.println(jsonObject.toString());
} }
}
上一篇:css之opacity


下一篇:让Scrapy的Spider更通用