2015年12月14日 星期一

[Java] Java Read EXIF

Java 讀取數位相片EXIF檔的程式碼
package exif;


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 java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author flowercatswets
 */
public class Exif {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        File jpegFile = new File("/Users/flowercatswets/Desktop/test/P1080033.JPG");  
    
    Metadata metadata = null;  
        try {
            metadata = JpegMetadataReader.readMetadata(jpegFile);
            print(metadata);
        } catch (JpegProcessingException ex) {
            Logger.getLogger(Exif.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Exif.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
     private static void print(Metadata metadata)
    {
        System.out.println("-------------------------------------");

      
        for (Directory directory : metadata.getDirectories()) {

          
            for (Tag tag : directory.getTags()) {
                System.out.println(tag);
                
               // String tagName= tag.getTagName();
                //String tagDes=tag.getDescription();
                
            }

            //
            // Each Directory may also contain error messages
            //
            if (directory.hasErrors()) {
                for (String error : directory.getErrors()) {
                    System.err.println("ERROR: " + error);
                }
            }
        }
    }
    
}


但是要先去拉一個專案的code下來

https://github.com/drewnoakes/metadata-extractor

Netbean滙不進這一個專案,所以我把裡面的Source資料夾下的程式碼COPY到

專案中,讓import可以正常,接下來找一張相片。

執行一下輸出結果

-------------------------------------
[JPEG] Compression Type - Baseline
[JPEG] Data Precision - 8 bits
[JPEG] Image Height - 3000 pixels
[JPEG] Image Width - 4000 pixels
[JPEG] Number of Components - 3
[JPEG] Component 1 - Y component: Quantization table 0, Sampling factors 1 horiz/1 vert
[JPEG] Component 2 - Cb component: Quantization table 1, Sampling factors 1 horiz/1 vert
[JPEG] Component 3 - Cr component: Quantization table 1, Sampling factors 1 horiz/1 vert
[JFIF] Version - 1.1
[JFIF] Resolution Units - none
[JFIF] X Resolution - 1 dot
[JFIF] Y Resolution - 1 dot
[JFIF] Thumbnail Width Pixels - 0
[JFIF] Thumbnail Height Pixels - 0
[Exif IFD0] Make - Panasonic
[Exif IFD0] Model - DMC-GF2
[Exif IFD0] Orientation - Top, left side (Horizontal / normal)
[Exif IFD0] Software - iPhoto 9.5.1
[Exif IFD0] Date/Time - 2014:07:13 16:31:27
[Exif SubIFD] Exposure Time - 1/400 sec
[Exif SubIFD] F-Number - f/10.0
[Exif SubIFD] Exposure Program - Program normal
[Exif SubIFD] ISO Speed Ratings - 100
[Exif SubIFD] Exif Version - 2.30
[Exif SubIFD] Date/Time Original - 2014:07:13 16:31:27
[Exif SubIFD] Date/Time Digitized - 2014:07:13 16:31:27
[Exif SubIFD] Components Configuration - YCbCr
[Exif SubIFD] Compressed Bits Per Pixel - 2 bits/pixel
[Exif SubIFD] Exposure Bias Value - 0 EV
[Exif SubIFD] Max Aperture Value - f/5.6
[Exif SubIFD] Metering Mode - Multi-segment
[Exif SubIFD] White Balance - Unknown
[Exif SubIFD] Flash - Flash did not fire, auto
[Exif SubIFD] Focal Length - 42 mm
[Exif SubIFD] FlashPix Version - 1.00
[Exif SubIFD] Color Space - sRGB
[Exif SubIFD] Exif Image Width - 4000 pixels
[Exif SubIFD] Exif Image Height - 3000 pixels
[Exif SubIFD] Sensing Method - One-chip color area sensor
[Exif SubIFD] File Source - Digital Still Camera (DSC)
[Exif SubIFD] Scene Type - Directly photographed image
[Exif SubIFD] Custom Rendered - Normal process
[Exif SubIFD] Exposure Mode - Auto exposure
[Exif SubIFD] White Balance Mode - Auto white balance
[Exif SubIFD] Digital Zoom Ratio - Digital zoom not used
[Exif SubIFD] Focal Length 35 - 84 mm
[Exif SubIFD] Scene Capture Type - Standard
[Exif SubIFD] Gain Control - None
[Exif SubIFD] Contrast - None
[Exif SubIFD] Saturation - None
[Exif SubIFD] Sharpness - None
[Xmp] XMP Value Count - 8
[Xmp] Lens - LUMIX G VARIO 14-42/F3.5-5.6  
[Xmp] Serial Number - F931109140259
[ICC Profile] Profile Size - 560
[ICC Profile] CMM Type - ADBE
[ICC Profile] Version - 2.1.0
[ICC Profile] Class - Display Device
[ICC Profile] Color space - RGB
[ICC Profile] Profile Connection Space - XYZ
[ICC Profile] Profile Date/Time - 星期六 八月 12 03:51:59 +08:00 2000
[ICC Profile] Signature - acsp
[ICC Profile] Primary Platform - Apple Computer, Inc.
[ICC Profile] Device manufacturer - none
[ICC Profile] XYZ values - 0.964 1 0.825
[ICC Profile] Tag Count - 10
[ICC Profile] Copyright - Copyright 2000 Adobe Systems Incorporated
[ICC Profile] Profile Description - Adobe RGB (1998)
[ICC Profile] Media White Point - (0.9505, 1, 1.0891)
[ICC Profile] Media Black Point - (0, 0, 0)
[ICC Profile] Red TRC - 0.0085908
[ICC Profile] Green TRC - 0.0085908
[ICC Profile] Blue TRC - 0.0085908
[ICC Profile] Red Colorant - (0.6097, 0.3111, 0.0195)
[ICC Profile] Green Colorant - (0.2053, 0.6257, 0.0609)
[ICC Profile] Blue Colorant - (0.1492, 0.0632, 0.7446)
[Photoshop] Caption Digest - 30 46 141 110 6 218 48 251 230 96 58 140 160 40 46 157
[IPTC] Coded Character Set - UTF-8
[IPTC] Application Record Version - 2
[IPTC] Digital Time Created - 16:31:27
[IPTC] Digital Date Created - 星期日 七月 13 00:00:00 +08:00 2014
[IPTC] Special Instructions - jyVgAHzDQNCOpuAZrUdFnQ
[IPTC] Date Created - 星期日 七月 13 00:00:00 +08:00 2014
[IPTC] Time Created - 16:31:27
[File] File Name - P1060712.JPG
[File] File Size - 9306586 bytes
[File] File Modified Date - 星期二 七月 15 23:28:01 +08:00 2014

參考資料來源:

https://github.com/drewnoakes/metadata-extractor

沒有留言:

張貼留言