Sanselanを使ってTIFFを読み込む

JavaTIFF画像を読み込む必要が有ったので調べてみた。ImageIO辺りで既に対応済みかと思いつつ、念のため対応フォーマットをチェックしてみる。

public void doPrintImageFormat(){
    String[] formatNames = ImageIO.getReaderFormatNames();
    System.out.println("ReaderFormatNames:");
    for(String s:formatNames) {
        System.out.print(s+", ");
    }
}

結果は下記の通り。残念ながらJava SE6でも未対応らしい。

ReaderFormatNames:
BMP, bmp, jpg, JPG, jpeg, wbmp, png, JPEG, PNG, WBMP, GIF, gif, 

JAI (Java Advanced Imaging) を使っても良いのだけど、各動作環境でライブラリをインストールしなければならないのは面倒だし、ここまで高機能なものは要らない。jarファイル一つだけで簡単に使えるモノはないかと探してみたら、Sanselanを見つけた。画像サイズを取得するサンプルコードは下記の通り。

import java.io.File;
import java.io.IOException;
import org.apache.sanselan.ImageFormat;
import org.apache.sanselan.ImageReadException;
import org.apache.sanselan.Sanselan;
import org.apache.sanselan.ImageInfo;

public class Main {
    public static void main(String[] args) throws ImageReadException, IOException {
        doPrintImageSize("sample.jpg");
        doPrintImageSize("sample.tiff");
        doPrintImageSize("sample.psd");
    }	

    public static void doPrintImageSize(String filename) throws ImageReadException, IOException{
        long t1 = System.currentTimeMillis();
        File srcFile = new File(filename);
        ImageInfo info = Sanselan.getImageInfo(srcFile);
        long t2 = System.currentTimeMillis();
        System.out.println("- "+srcFile.getAbsolutePath()+", Width:"+info.getWidth()+", Height:"+info.getHeight()+", Time:"+(t2-t1));
    }
}

実行結果は下記の通り。JPEG, TIFF, PSDなどの画像サイズをメタデータから取得しており、なかなか良い感じ。

- /Users/hoge/Console/sample.jpg, Width:1600, Height:1200, Time:228
- /Users/hoge/Console/sample.tiff, Width:1600, Height:1200, Time:6
- /Users/hoge/Console/sample.psd, Width:1600, Height:1200, Time:2

画像データの読み込みも簡単。(呼び出し先はこちらコードを参照)

BufferedImage image = ImageReadExample.imageReadExample(new File("sample.tiff"));

あとは通常の画像データと同様に扱える。同様にJPEG画像のExifデータ取得も簡単。(呼び出し先はこちらを参照)

MetadataExample.metadataExample(new File("sample.jpg"));

この結果は下記の通り。

file: sample.jpg
XResolution: 180
Date Time: '2000:06:11 23:02:49'
Date Time Original: '2000:06:11 23:02:49'
Create Date: '2000:06:11 23:02:49'
ISO: Not Found.
Shutter Speed Value: 256042/65536 (3.907)
Aperture Value: 194698/65536 (2.971)
Brightness Value: Not Found.
GPS Latitude Ref: 'R98'
GPS Latitude: 48, 49, 48, 48
GPS Longitude Ref: Not Found.
GPS Longitude: Not Found.


        item: Make: 'Canon'
        item: Model: 'Canon IXY DIGITAL'
        item: Orientation: 1
        item: XResolution: 180
        item: YResolution: 180
        item: Resolution Unit: 2
        item: Modify Date: '2000:06:11 23:02:49'
        item: YCbCr Positioning: 1
        item: Exif Offset: 184
        item: Exposure Time: 1/15 (0.067)
        item: FNumber: 28/10 (2.8)
        item: Exif Version: 48, 50, 49, 48
        item: Date Time Original: '2000:06:11 23:02:49'
        item: Create Date: '2000:06:11 23:02:49'
        item: Components Configuration: 1, 2, 3, 0
        item: Compressed Bits Per Pixel: 3
        item: Shutter Speed Value: 256042/65536 (3.907)
        item: Aperture Value: 194698/65536 (2.971)
        item: Exposure Compensation: 0
        item: Max Aperture Value: 194698/65536 (2.971)
        item: Subject Distance: 2615/1000 (2.615)
        item: Metering Mode: 2
        item: Flash: 0
        item: Focal Length: 173/32 (5.406)
        item: Maker Note: 10, 0, 1, 0, 3, 0, 19, 0, 0, 0, 120, 3, 0, 0, 2, 0, 3, 0, 4, 0, 0, 0, -98, 3, 0, 0, 3, 0, 3, 0, 4, 0, 0, 0, -90, 3, 0, 0, 4, 0, 3, 0, 15, 0, 0, 0, -82, 3, 0, 0, 0... (310)
        item: UserComment: ''
        item: Flashpix Version: 48, 49, 48, 48
        item: Color Space: 1
        item: Exif Image Width: 1600
        item: Exif Image Length: 1200
        item: Interop Offset: 1088
        item: Focal Plane XResolution: 1600000/206 (7,766.99)
        item: Focal Plane YResolution: 1200000/155 (7,741.935)
        item: Focal Plane Resolution Unit: 2
        item: Sensing Method: 2
        item: File Source: 3
        item: Interop Index: 'R98'
        item: Interop Version: 48, 49, 48, 48
        item: Related Image Width: 1600
        item: Related Image Length: 1200
        item: Compression: 6
        item: XResolution: 180
        item: YResolution: 180
        item: Resolution Unit: 2
        item: Jpg From Raw Start: 1524
        item: Jpg From Raw Length: 4542

Jarファイル一つを含めれば良いだけなので、これは便利に使えそうですね。