1 package filetest; 2 3 import java.io.File; 4 import java.io.FilenameFilter; 5 import java.io.IOException; 6 import java.util.ArrayList; 7 8 import com.drew.imaging.jpeg.JpegMetadataReader; 9 import com.drew.imaging.jpeg.JpegProcessingException; 10 import com.drew.metadata.Directory; 11 import com.drew.metadata.Metadata; 12 import com.drew.metadata.Tag; 13 /** 14 * 文件的迭代输出 15 * 寻找某文件下以“.jpg”结尾的文件 16 * 并解析该图片 17 * */ 18 19 class MyFilenameFilter implements FilenameFilter{ 20 public boolean accept(File dir, String name){ 21 return name.endsWith(".jpg") 22 || new File(name).isDirectory(); 23 } 24 } 25 26 27 public class FileTest { 28 public static ArrayList<String> fiList = new ArrayList<String>(); 29 public static int count = 0; 30 31 32 33 public static void main(String[] agrs) throws JpegProcessingException, IOException{ 34 35 File filePath = new File("E://psresource/Root"); 36 String[] nameStrings = filePath.list(new MyFilenameFilter()); 37 for(String name : nameStrings){ 38 count++; 39 40 File file = new File("E://psresource/Root/" + name); 41 fiList.add(file.getAbsolutePath()); 42 File jpegFile = new File(file.getAbsolutePath()); 43 try{ 44 Metadata metadata = JpegMetadataReader.readMetadata(jpegFile); 45 46 for(Directory directory : metadata.getDirectories()){ 47 for(Tag tag : directory.getTags()){ 48 49 System.out.println(file + "的tag信息为: " +tag); 50 } 51 } 52 }catch(JpegProcessingException e){ 53 System.out.println("%%%%%%%%%%%%%%%%%"+jpegFile.getAbsolutePath()); 54 if(jpegFile.delete()){ 55 System.out.println(jpegFile.getAbsolutePath() + "已经被删除"); 56 } 57 58 e.printStackTrace(); 59 } 60 61 //System.out.println(Integer.toString(count) + file.getAbsolutePath()); 62 63 } 64 //System.out.println(Integer.toString(count) + "所有的图片的名字:" + fiList.toString()); 65 } 66 67 68 69 70 } 71 72 73 图片的操作: 74 75 76 77 解析图片,需要两个包metadata-extractor-2.6.4.jar ,但是修改图片的EXIF信息,还要导入另一个jar包:mediautil-1.0.jar 78 79
图片的操作:
解析图片,需要两个包metadata-extractor-2.6.4.jar ,但是修改图片的EXIF信息,还要导入另一个jar包:mediautil-1.0.jar
1 package exiftest; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileOutputStream; 8 import java.io.InputStream; 9 import java.io.InputStreamReader; 10 import java.io.OutputStream; 11 import java.text.SimpleDateFormat; 12 import java.util.Date; 13 import java.util.Iterator; 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 import mediautil.image.jpeg.Entry; 31 import mediautil.image.jpeg.Exif; 32 import mediautil.image.jpeg.LLJTran; 33 import mediautil.image.jpeg.LLJTranException; 34 import net.sf.json.JSONObject; 35 36 import com.drew.imaging.jpeg.JpegMetadataReader; 37 import com.drew.metadata.Directory; 38 import com.drew.metadata.Metadata; 39 import com.drew.metadata.Tag; 40 41 /** 42 * 测试用于读取图片的EXIF信息 43 * @author Winter Lau 44 */ 45 public class Exiftest { 46 47 /** 48 * 经纬度转换 度分秒转换 49 * @param point 坐标点 50 * @return 51 */ 52 public static String pointToLatlong (String point ) { 53 Double du = Double.parseDouble(point.substring(0, point.indexOf("°")).trim()); 54 Double fen = Double.parseDouble(point.substring(point.indexOf("°")+1, point.indexOf("‘")).trim()); 55 Double miao = Double.parseDouble(point.substring(point.indexOf("‘")+1, point.indexOf("\"")).trim()); 56 Double duStr = du + fen / 60 + miao / 60 / 60 ; 57 return duStr.toString(); 58 } 59 public static void main(String[] args) throws Exception { 60 //File jpegFile = new File("E:\\psresource\\img\\photos\\images\\20140627_140048.jpg"); 61 File jpegFile = new File("E:\\psresource\\img\\photos\\wwd\\iphone\\20140521.jpg"); 62 Metadata metadata = JpegMetadataReader.readMetadata(jpegFile); 63 64 65 66 JSONObject jsonObject = new JSONObject(); 67 68 Double lat = null; 69 Double lon=null; 70 Date date = null; 71 for(Directory directory : metadata.getDirectories()){ 72 for(Tag tag : directory.getTags()){ 73 System.out.println(tag); 74 75 String tagname = tag.getTagName(); 76 if(tagname.equals("GPS Longitude")){ 77 System.out.println(pointToLatlong(tag.getDescription())); 78 lon = Double.valueOf(pointToLatlong(tag.getDescription())); 79 jsonObject.accumulate("lon", lon); 80 } 81 else if (tagname.equals("GPS Latitude")) { 82 System.out.println(pointToLatlong(tag.getDescription())); 83 lat = Double.valueOf(pointToLatlong(tag.getDescription())); 84 jsonObject.accumulate("lat", lat); 85 } 86 if(tagname.equals("Date/Time")){ 87 System.out.println(tag.getDescription()); 88 SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd hh:mm:ss"); 89 90 Date date2 = sdf.parse(tag.getDescription()); 91 Long dateLong = date2.getTime(); 92 jsonObject.accumulate("time", dateLong); 93 94 } 95 } 96 System.out.println(jsonObject.toString()); 97 } 98 99 100 } 101 }
时间: 2024-10-10 07:59:06