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

时间:2023-03-09 00:50:32
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.

 /**
*
*/
package com.b510.test; import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map; 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;
import com.drew.metadata.exif.ExifIFD0Directory;
/**
* only handle the "jpeg" format image file(s)<br>
* <a href="https://drewnoakes.com/code/exif/">Get more about the Exif>></a>
* @author Hongten
* @date 2013-12-20
*/
public class ExifTester { //separating character
public static final String LINE = "-";
public static final String RIGHT_PARENTHESES = "]"; public static void main(String[] args) throws Exception {
//if you want to run this code.you should be rewrite the following array of String.
//and be sure the correct path of the image file
String[] pathNames = {
"C:/2013-02-08_12-24-54_100.jpg",//motorola ME865
"C:/20022013011.jpg",//nokia 6700s
"C:/IMG_0018.JPG",//download from web site
"C:/IMG_0697.JPG", //ipad4
"C:/P1080402.JPG"//Panasonic DMC-LX5
};
List<File> files = new ArrayList<File>();
for (String pathname : pathNames) {
File file = new File(pathname);
files.add(file);
}
List<Map<String, String>> imagesInfo = getImageInfoOfExif(files);
printImagesInfo(imagesInfo);
//Get the image file informations
File file = new File("C:/IMG_0697.JPG");
Map<String, String> map = getImageInfoOfExif(file);
System.out.println("##### the image file informations");
HandleMap(map);
} /**
* print the abstracts of image file
* @param imagesInfo
*/
private static void printImagesInfo(List<Map<String, String>> imagesInfo) {
if(!imagesInfo.isEmpty()){
for(Map<String, String> map: imagesInfo){
HandleMap(map);
System.out.println("======================================");
}
}
} /**
* @param map
*/
private static void HandleMap(Map<String, String> map) {
Iterator<String> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
String name = iterator.next();
String value = map.get(name);
printInfo(name, value);
}
} /**
* @param name
* @param value
*/
private static void printInfo(String name, String value) {
System.out.println(name + " : " + value);
} /**
* get the abstracts of the image file
* @param jpegFile
* @return
* @throws JpegProcessingException
* @throws IOException
*/
@SuppressWarnings("unchecked")
private static Map<String, String> getImageInfoOfExif(File jpegFile) throws JpegProcessingException, IOException {
Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);
Directory exifDirectory = metadata.getDirectory(ExifIFD0Directory.class);
Collection tags = exifDirectory.getTags();
Iterator iterator = tags.iterator();
Map<String, String> abstractsMap = new HashMap<String, String>();
while (iterator.hasNext()) {
Tag tag = (Tag) iterator.next();
String[] tagArrays = tag.toString().split(LINE);
String[] abstracts = tagArrays[0].trim().split(RIGHT_PARENTHESES);
abstractsMap.put(abstracts[1].trim(), tagArrays[1].trim());
}
return abstractsMap;
} /**
* handle more than one image files
* @param files
* @return
* @throws JpegProcessingException
* @throws IOException
*/
private static List<Map<String, String>> getImageInfoOfExif(List<File> files) throws JpegProcessingException, IOException{
if(files == null)return null;
List<Map<String, String>> list = new ArrayList<Map<String,String>>();
if(files.size() > 0){
for(File file: files){
Map<String, String> map = getImageInfoOfExif(file);
list.add(map);
}
}
return list;
}
}

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!