前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Get the image file(s) some informations,Including the Make,Model,Date/Time,etc

Get the image file(s) some informations,Including the Make,Model,Date/Time,etc

作者头像
Hongten
发布2018-09-13 11:19:39
4390
发布2018-09-13 11:19:39
举报
文章被收录于专栏:HongtenHongten

This is a blog about how to get the image file(s) some informations.Including the Make,Model,Date/Tiime,etc.

while,how can do it? I should use a tool which name is :Exif,writed by Java,and now its version is 2.6.4.

and you can get more from the here. or osChina

How does it work?

while,I hava a demo and I will show the complete code to you.

代码语言:javascript
复制
  1 /**
  2  * 
  3  */
  4 package com.b510.test;
  5 
  6 import java.io.File;
  7 import java.io.IOException;
  8 import java.util.ArrayList;
  9 import java.util.Collection;
 10 import java.util.HashMap;
 11 import java.util.Iterator;
 12 import java.util.List;
 13 import java.util.Map;
 14 
 15 import com.drew.imaging.jpeg.JpegMetadataReader;
 16 import com.drew.imaging.jpeg.JpegProcessingException;
 17 import com.drew.metadata.Directory;
 18 import com.drew.metadata.Metadata;
 19 import com.drew.metadata.Tag;
 20 import com.drew.metadata.exif.ExifIFD0Directory;
 21 /**
 22  * only handle the "jpeg" format image file(s)<br>
 23  * <a href="https://drewnoakes.com/code/exif/">Get more about the Exif>></a>
 24  * @author Hongten
 25  * @date 2013-12-20
 26  */
 27 public class ExifTester {
 28     
 29     //separating character
 30     public static final String LINE = "-";
 31     public static final String RIGHT_PARENTHESES = "]";
 32     
 33     
 34     public static void main(String[] args) throws Exception {
 35         //if you want to run this code.you should be rewrite the following array of String.
 36         //and be sure the correct path of the image file
 37         String[] pathNames = { 
 38                 "C:/2013-02-08_12-24-54_100.jpg",//motorola ME865
 39                 "C:/20022013011.jpg",//nokia 6700s
 40                 "C:/IMG_0018.JPG",//download from web site
 41                 "C:/IMG_0697.JPG", //ipad4
 42                 "C:/P1080402.JPG"//Panasonic DMC-LX5
 43                 };
 44         List<File> files = new ArrayList<File>();
 45         for (String pathname : pathNames) {
 46             File file = new File(pathname);
 47             files.add(file);
 48         }
 49         List<Map<String, String>> imagesInfo = getImageInfoOfExif(files);
 50         printImagesInfo(imagesInfo);
 51         //Get the image file informations
 52         File file = new File("C:/IMG_0697.JPG");
 53         Map<String, String> map = getImageInfoOfExif(file);
 54         System.out.println("##### the image file informations");
 55         HandleMap(map);
 56      }
 57 
 58     /**
 59      * print the abstracts of image file 
 60      * @param imagesInfo
 61      */
 62     private static void printImagesInfo(List<Map<String, String>> imagesInfo) {
 63         if(!imagesInfo.isEmpty()){
 64             for(Map<String, String> map: imagesInfo){
 65                 HandleMap(map);
 66                 System.out.println("======================================");
 67             }
 68         }
 69     }
 70 
 71     /**
 72      * @param map
 73      */
 74     private static void HandleMap(Map<String, String> map) {
 75         Iterator<String> iterator = map.keySet().iterator();
 76         while (iterator.hasNext()) {
 77             String name = iterator.next();
 78             String value = map.get(name);
 79             printInfo(name, value);
 80         }
 81     }
 82 
 83     /**
 84      * @param name
 85      * @param value
 86      */
 87     private static void printInfo(String name, String value) {
 88         System.out.println(name + " : " + value);
 89     }
 90 
 91     /**
 92      * get the abstracts of the image file
 93      * @param jpegFile
 94      * @return
 95      * @throws JpegProcessingException
 96      * @throws IOException
 97      */
 98     @SuppressWarnings("unchecked")
 99     private static Map<String, String> getImageInfoOfExif(File jpegFile) throws JpegProcessingException, IOException {
100         Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);
101         Directory exifDirectory = metadata.getDirectory(ExifIFD0Directory.class);
102         Collection tags = exifDirectory.getTags();
103         Iterator iterator = tags.iterator();
104         Map<String, String> abstractsMap = new HashMap<String, String>();
105         while (iterator.hasNext()) {
106             Tag tag = (Tag) iterator.next();
107             String[] tagArrays = tag.toString().split(LINE);
108             String[] abstracts = tagArrays[0].trim().split(RIGHT_PARENTHESES);
109             abstractsMap.put(abstracts[1].trim(), tagArrays[1].trim());
110         }
111         return abstractsMap;
112     }
113     
114     /**
115      * handle more than one image files
116      * @param files
117      * @return
118      * @throws JpegProcessingException
119      * @throws IOException
120      */
121     private static List<Map<String, String>> getImageInfoOfExif(List<File> files) throws JpegProcessingException, IOException{
122         if(files == null)return null;
123         List<Map<String, String>> list = new ArrayList<Map<String,String>>();
124         if(files.size() > 0){
125             for(File file: files){
126                 Map<String, String> map = getImageInfoOfExif(file);
127                 list.add(map);
128             }
129         }
130         return list;
131     }
132 }

and the output as following:

代码语言:javascript
复制
Date/Time : 2013:02:08 12:24:53
Model : ME865
X Resolution : 72 dots per inch
YCbCr Positioning : Center of pixel array
Resolution Unit : Inch
Y Resolution : 72 dots per inch
Make : Motorola
======================================
Orientation : Top, left side (Horizontal / normal)
Model : 6700s
X Resolution : 300 dots per inch
YCbCr Positioning : Center of pixel array
Resolution Unit : Inch
Y Resolution : 300 dots per inch
Make : Nokia
======================================
Orientation : Top, left side (Horizontal / normal)
X Resolution : 72 dots per inch
YCbCr Positioning : Center of pixel array
Resolution Unit : Inch
Y Resolution : 72 dots per inch
======================================
Software : 6.1.3
Date/Time : 2013:06:27 23:31:19
Orientation : Right side, top (Rotate 90 CW)
Model : iPad
X Resolution : 72 dots per inch
YCbCr Positioning : Center of pixel array
Resolution Unit : Inch
Y Resolution : 72 dots per inch
Make : Apple
======================================
Software : Ver.1.0
Unknown tag (0xc4a5) : [208 bytes]
Model : DMC
Orientation : Top, left side (Horizontal / normal)
YCbCr Positioning : Datum point
Unknown tag (0xc6d3) : [128 bytes]
Date/Time : 2013:01:04 00:01:06
X Resolution : 180 dots per inch
Unknown tag (0xc6d2) : [64 bytes]
Resolution Unit : Inch
Artist : 
Y Resolution : 180 dots per inch
Make : Panasonic
======================================
##### the image file informations
Software : 6.1.3
Date/Time : 2013:06:27 23:31:19
Orientation : Right side, top (Rotate 90 CW)
Model : iPad
X Resolution : 72 dots per inch
YCbCr Positioning : Center of pixel array
Resolution Unit : Inch
Y Resolution : 72 dots per inch
Make : Apple

it work and the output is so cool.

then, you should have a try!

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-12-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档