• 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!

  • 相关阅读:
    Unix进程和线程管理及其异同
    UnixIPC之共享内存
    Unix/Linux常用文件操作
    java中int和Integer比较
    JAVA四种引用类型
    JAVA-Exception&Error
    JAVA特性-跨平台/面向对象
    JAVA单向链表实现
    linux安装及配置c++的opencv库
    static_cast、const_cast、dynamic_cast、reinterpret_cast
  • 原文地址:https://www.cnblogs.com/hongten/p/hongten_java_exif.html
Copyright © 2020-2023  润新知