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

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.

  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:

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!

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏码匠的流水账

聊聊storm的AssignmentDistributionService

本文主要研究一下storm的AssignmentDistributionService

15610
来自专栏java、Spring、技术分享

java应用CAS

  CAS(Compare and Swap),即比较并替换。jdk里的大量源码通过CAS来提供线程安全操作,比如AtomicInteger类。下面我们来分析一...

15530
来自专栏Java架构师历程

jsoup解析的常见用法

1、解析attribute中值,如下面所示的serviceID和serviceName:

29430
来自专栏Golang语言社区

【Golang语言社区】源码篇--sync包map

早晨看到知乎上一篇介绍Go1.9X版本部分功能,特产关注了一下;把源码想给大家呈现下,实际测试请看下一篇文章:Go语言sync.map 实际测...

367100
来自专栏曾大稳的博客

Android ClassLoader流程解读并简单方式实现热更新

ClassLoader在启动Activity的时候会调用loadClass方法,我们就从这里入手:

31420
来自专栏码匠的流水账

聊聊storm的IEventLogger

storm-2.0.0/storm-client/src/jvm/org/apache/storm/metric/IEventLogger.java

9420
来自专栏码匠的流水账

聊聊flink的CheckpointScheduler

flink-runtime_2.11-1.7.0-sources.jar!/org/apache/flink/runtime/checkpoint/Checkp...

18630
来自专栏码匠的流水账

bloomfilter的简单实现

布隆过滤器(英语:Bloom Filter)是1970年由布隆提出的,可以用于检索一个元素是否在一个集合中。

8110
来自专栏机器学习与自然语言处理

03-树3. Tree Traversals Again (25)将先序遍历和中序遍历转为后序遍历

03-树3. Tree Traversals Again (25) 题目来源:http://www.patest.cn/contests/mooc-ds/03-...

35490
来自专栏码匠的流水账

聊聊storm的IEventLogger

storm-2.0.0/storm-client/src/jvm/org/apache/storm/metric/IEventLogger.java

14730

扫码关注云+社区

领取腾讯云代金券