JPEGのExif情報を読み取み～metadata-extractor～

Java/AndroidでExifを読み込む処理を作るときにmetadata-extractorという便利なライブラリを使わせて頂きました。
metadata-extractorは読み取り専用なのでExif情報の削除や追加・修正は、他のライブラリを使用するか自分で実装して下さい。


# 使い方

```java
String path = "c:\image\img0001.jpg";   // ファイルパス
File jpegFile = new File(path);

// メタ情報の取得
Metadata metadata = ImageMetadataReader.readMetadata(jpegFile);

// Exif情報の全表示
for (Directory directory : metadata.getDirectories()) {
    for (Tag tag : directory.getTags()) {
        System.out.println(tag);
    }
}
```



# 値を個別に出力する

値を個別に出力するにはこちら。


```java
public static void Main(String[] args) {

    String path = "c:\image\img0001.jpg";   // ファイルパス
    File jpegFile = new File(path);

    // メタ情報の取得
    Metadata metadata = ImageMetadataReader.readMetadata(jpegFile);


    // JPEG/TIFF(未対応)
    JpegDirectory jpegDirectory = metadata.getFirstDirectoryOfType(JpegDirectory.class);
    if (jpegDirectory != null) {
        for (Tag tag : jpegDirectory.getTags()) {
            getJpegTag(tag);
        }
    }

    // IFD0 情報を取得
    ExifIFD0Directory ifdDirectory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
    if( ifdDirectory != null ) {
        for (Tag tag : ifdDirectory.getTags()) {
            getExifTag(tag);
        }
    }

    // SubIFD 情報を取得
    ExifSubIFDDirectory subIfdDirectory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
    if( subIfdDirectory!=null ) {
        for (Tag tag : subIfdDirectory.getTags()) {
            getExifTag(tag);
        }
    }

    // Thumbnail 情報を取得
    ExifThumbnailDirectory thumbnailDirectory = metadata.getFirstDirectoryOfType(ExifThumbnailDirectory.class);
    if (thumbnailDirectory != null) {
        for (Tag tag : thumbnailDirectory.getTags()) {
            getExifTag(tag);
        }
    }

    // GPS 情報を取得
    GpsDirectory gpsDirectory = metadata.getFirstDirectoryOfType(GpsDirectory.class);
    if( gpsDirectory!=null ) {
        for (Tag tag : gpsDirectory.getTags()) {
            getGPSTag(tag);
        }
    }
}

// Jpeg の全タグ
private void getJpegTag(Tag tag) {
    switch (tag.getTagType()) {
        case JpegDirectory.TAG_COMPRESSION_TYPE:
        case JpegDirectory.TAG_DATA_PRECISION:
        case JpegDirectory.TAG_IMAGE_WIDTH:
        case JpegDirectory.TAG_IMAGE_HEIGHT:
        case JpegDirectory.TAG_NUMBER_OF_COMPONENTS:
        case JpegDirectory.TAG_COMPONENT_DATA_1:
        case JpegDirectory.TAG_COMPONENT_DATA_2:
        case JpegDirectory.TAG_COMPONENT_DATA_3:
        case JpegDirectory.TAG_COMPONENT_DATA_4:
        default:
            // 必要なcaseの中で処理を行う
            System.out.println(tag.getTagName, tag.getDescription()));
            break;
    }
}

// Exif の全タグ
private void getExifTag(Tag tag) {
    switch (tag.getTagType()) {
        switch (tag.getTagType()) {
            case ExifDirectoryBase.TAG_INTEROP_INDEX:
            case ExifDirectoryBase.TAG_INTEROP_VERSION:
            case ExifDirectoryBase.TAG_NEW_SUBFILE_TYPE:
            case ExifDirectoryBase.TAG_SUBFILE_TYPE:
            case ExifDirectoryBase.TAG_IMAGE_WIDTH:
            case ExifDirectoryBase.TAG_IMAGE_HEIGHT:
            case ExifDirectoryBase.TAG_BITS_PER_SAMPLE:
            case ExifDirectoryBase.TAG_COMPRESSION:
            case ExifDirectoryBase.TAG_PHOTOMETRIC_INTERPRETATION:
            case ExifDirectoryBase.TAG_THRESHOLDING:
            case ExifDirectoryBase.TAG_FILL_ORDER:
            case ExifDirectoryBase.TAG_DOCUMENT_NAME:
            case ExifDirectoryBase.TAG_IMAGE_DESCRIPTION:
            case ExifDirectoryBase.TAG_MAKE:
            case ExifDirectoryBase.TAG_MODEL:
            case ExifDirectoryBase.TAG_STRIP_OFFSETS:
            case ExifDirectoryBase.TAG_ORIENTATION:
            case ExifDirectoryBase.TAG_SAMPLES_PER_PIXEL:
            case ExifDirectoryBase.TAG_ROWS_PER_STRIP:
            case ExifDirectoryBase.TAG_STRIP_BYTE_COUNTS:
            case ExifDirectoryBase.TAG_MIN_SAMPLE_VALUE:
            case ExifDirectoryBase.TAG_MAX_SAMPLE_VALUE:
            case ExifDirectoryBase.TAG_X_RESOLUTION:
            case ExifDirectoryBase.TAG_Y_RESOLUTION:
            case ExifDirectoryBase.TAG_PLANAR_CONFIGURATION:
            case ExifDirectoryBase.TAG_PAGE_NAME:
            case ExifDirectoryBase.TAG_RESOLUTION_UNIT:
            case ExifDirectoryBase.TAG_TRANSFER_FUNCTION:
            case ExifDirectoryBase.TAG_SOFTWARE:
            case ExifDirectoryBase.TAG_DATETIME:
            case ExifDirectoryBase.TAG_ARTIST:
            case ExifDirectoryBase.TAG_HOST_COMPUTER:
            case ExifDirectoryBase.TAG_PREDICTOR:
            case ExifDirectoryBase.TAG_WHITE_POINT:
            case ExifDirectoryBase.TAG_PRIMARY_CHROMATICITIES:
            case ExifDirectoryBase.TAG_TILE_WIDTH:
            case ExifDirectoryBase.TAG_TILE_LENGTH:
            case ExifDirectoryBase.TAG_TILE_OFFSETS:
            case ExifDirectoryBase.TAG_TILE_BYTE_COUNTS:
            case ExifDirectoryBase.TAG_SUB_IFD_OFFSET:
            case ExifDirectoryBase.TAG_TRANSFER_RANGE:
            case ExifDirectoryBase.TAG_JPEG_TABLES:
            case ExifDirectoryBase.TAG_JPEG_PROC:
            case ExifDirectoryBase.TAG_YCBCR_COEFFICIENTS:
            case ExifDirectoryBase.TAG_YCBCR_SUBSAMPLING:
            case ExifDirectoryBase.TAG_YCBCR_POSITIONING:
            case ExifDirectoryBase.TAG_REFERENCE_BLACK_WHITE:
            case ExifDirectoryBase.TAG_STRIP_ROW_COUNTS:
            case ExifDirectoryBase.TAG_APPLICATION_NOTES:
            case ExifDirectoryBase.TAG_RELATED_IMAGE_FILE_FORMAT:
            case ExifDirectoryBase.TAG_RELATED_IMAGE_WIDTH:
            case ExifDirectoryBase.TAG_RELATED_IMAGE_HEIGHT:
            case ExifDirectoryBase.TAG_RATING:
            case ExifDirectoryBase.TAG_CFA_REPEAT_PATTERN_DIM:
            case ExifDirectoryBase.TAG_CFA_PATTERN_2:
            case ExifDirectoryBase.TAG_BATTERY_LEVEL:
            case ExifDirectoryBase.TAG_COPYRIGHT:
            case ExifDirectoryBase.TAG_EXPOSURE_TIME:
            case ExifDirectoryBase.TAG_FNUMBER:
            case ExifDirectoryBase.TAG_IPTC_NAA:
            case ExifDirectoryBase.TAG_INTER_COLOR_PROFILE:
            case ExifDirectoryBase.TAG_EXPOSURE_PROGRAM:
            case ExifDirectoryBase.TAG_SPECTRAL_SENSITIVITY:
            case ExifDirectoryBase.TAG_ISO_EQUIVALENT:
            case ExifDirectoryBase.TAG_OPTO_ELECTRIC_CONVERSION_FUNCTION:
            case ExifDirectoryBase.TAG_INTERLACE:
            case ExifDirectoryBase.TAG_TIME_ZONE_OFFSET_TIFF_EP:
            case ExifDirectoryBase.TAG_SELF_TIMER_MODE_TIFF_EP:
            case ExifDirectoryBase.TAG_SENSITIVITY_TYPE:
            case ExifDirectoryBase.TAG_STANDARD_OUTPUT_SENSITIVITY:
            case ExifDirectoryBase.TAG_RECOMMENDED_EXPOSURE_INDEX:
            case ExifDirectoryBase.TAG_TIME_ZONE_OFFSET:
            case ExifDirectoryBase.TAG_SELF_TIMER_MODE:
            case ExifDirectoryBase.TAG_EXIF_VERSION:
            case ExifDirectoryBase.TAG_DATETIME_ORIGINAL:
            case ExifDirectoryBase.TAG_DATETIME_DIGITIZED:
            case ExifDirectoryBase.TAG_COMPONENTS_CONFIGURATION:
            case ExifDirectoryBase.TAG_COMPRESSED_AVERAGE_BITS_PER_PIXEL:
            case ExifDirectoryBase.TAG_SHUTTER_SPEED:
            case ExifDirectoryBase.TAG_APERTURE:
            case ExifDirectoryBase.TAG_BRIGHTNESS_VALUE:
            case ExifDirectoryBase.TAG_EXPOSURE_BIAS:
            case ExifDirectoryBase.TAG_MAX_APERTURE:
            case ExifDirectoryBase.TAG_SUBJECT_DISTANCE:
            case ExifDirectoryBase.TAG_METERING_MODE:
            case ExifDirectoryBase.TAG_LIGHT_SOURCE:
            case ExifDirectoryBase.TAG_WHITE_BALANCE:
            case ExifDirectoryBase.TAG_FLASH:
            case ExifDirectoryBase.TAG_FOCAL_LENGTH:
            case ExifDirectoryBase.TAG_FLASH_ENERGY_TIFF_EP:
            case ExifDirectoryBase.TAG_SPATIAL_FREQ_RESPONSE_TIFF_EP:
            case ExifDirectoryBase.TAG_NOISE:
            case ExifDirectoryBase.TAG_FOCAL_PLANE_X_RESOLUTION_TIFF_EP:
            case ExifDirectoryBase.TAG_FOCAL_PLANE_Y_RESOLUTION_TIFF_EP:
            case ExifDirectoryBase.TAG_IMAGE_NUMBER:
            case ExifDirectoryBase.TAG_SECURITY_CLASSIFICATION:
            case ExifDirectoryBase.TAG_IMAGE_HISTORY:
            case ExifDirectoryBase.TAG_SUBJECT_LOCATION_TIFF_EP:
            case ExifDirectoryBase.TAG_EXPOSURE_INDEX_TIFF_EP:
            case ExifDirectoryBase.TAG_STANDARD_ID_TIFF_EP:
            case ExifDirectoryBase.TAG_MAKERNOTE:
            case ExifDirectoryBase.TAG_USER_COMMENT:
            case ExifDirectoryBase.TAG_SUBSECOND_TIME:
            case ExifDirectoryBase.TAG_SUBSECOND_TIME_ORIGINAL:
            case ExifDirectoryBase.TAG_SUBSECOND_TIME_DIGITIZED:
            case ExifDirectoryBase.TAG_WIN_TITLE:
            case ExifDirectoryBase.TAG_WIN_COMMENT:
            case ExifDirectoryBase.TAG_WIN_AUTHOR:
            case ExifDirectoryBase.TAG_WIN_KEYWORDS:
            case ExifDirectoryBase.TAG_WIN_SUBJECT:
            case ExifDirectoryBase.TAG_FLASHPIX_VERSION:
            case ExifDirectoryBase.TAG_COLOR_SPACE:
            case ExifDirectoryBase.TAG_EXIF_IMAGE_WIDTH:
            case ExifDirectoryBase.TAG_EXIF_IMAGE_HEIGHT:
            case ExifDirectoryBase.TAG_RELATED_SOUND_FILE:
            case ExifDirectoryBase.TAG_FLASH_ENERGY:
            case ExifDirectoryBase.TAG_SPATIAL_FREQ_RESPONSE:
            case ExifDirectoryBase.TAG_FOCAL_PLANE_X_RESOLUTION:
            case ExifDirectoryBase.TAG_FOCAL_PLANE_Y_RESOLUTION:
            case ExifDirectoryBase.TAG_FOCAL_PLANE_RESOLUTION_UNIT:
            case ExifDirectoryBase.TAG_SUBJECT_LOCATION:
            case ExifDirectoryBase.TAG_EXPOSURE_INDEX:
            case ExifDirectoryBase.TAG_SENSING_METHOD:
            case ExifDirectoryBase.TAG_FILE_SOURCE:
            case ExifDirectoryBase.TAG_SCENE_TYPE:
            case ExifDirectoryBase.TAG_CFA_PATTERN:
            case ExifDirectoryBase.TAG_CUSTOM_RENDERED:
            case ExifDirectoryBase.TAG_EXPOSURE_MODE:
            case ExifDirectoryBase.TAG_WHITE_BALANCE_MODE:
            case ExifDirectoryBase.TAG_DIGITAL_ZOOM_RATIO:
            case ExifDirectoryBase.TAG_35MM_FILM_EQUIV_FOCAL_LENGTH:
            case ExifDirectoryBase.TAG_SCENE_CAPTURE_TYPE:
            case ExifDirectoryBase.TAG_GAIN_CONTROL:
            case ExifDirectoryBase.TAG_CONTRAST:
            case ExifDirectoryBase.TAG_SATURATION:
            case ExifDirectoryBase.TAG_SHARPNESS:
            case ExifDirectoryBase.TAG_DEVICE_SETTING_DESCRIPTION:
            case ExifDirectoryBase.TAG_SUBJECT_DISTANCE_RANGE:
            case ExifDirectoryBase.TAG_IMAGE_UNIQUE_ID:
            case ExifDirectoryBase.TAG_CAMERA_OWNER_NAME:
            case ExifDirectoryBase.TAG_BODY_SERIAL_NUMBER:
            case ExifDirectoryBase.TAG_LENS_SPECIFICATION:
            case ExifDirectoryBase.TAG_LENS_MAKE:
            case ExifDirectoryBase.TAG_LENS_MODEL:
            case ExifDirectoryBase.TAG_LENS_SERIAL_NUMBER:
            case ExifDirectoryBase.TAG_GAMMA:
            case ExifDirectoryBase.TAG_PRINT_IM:
            case ExifDirectoryBase.TAG_PANASONIC_TITLE:
            case ExifDirectoryBase.TAG_PANASONIC_TITLE_2:
            case ExifDirectoryBase.TAG_PADDING:
            case ExifDirectoryBase.TAG_LENS:
            // 必要なcaseの中で処理を行う
            System.out.println(tag.getTagName, tag.getDescription()));
            break;
        }
    }
}

// GPS の全タグ
void addGPSListData(Tag tag) {
    switch (tag.getTagType()) {
        case GpsDirectory.TAG_VERSION_ID:
        case GpsDirectory.TAG_LATITUDE_REF:
        case GpsDirectory.TAG_LATITUDE:
        case GpsDirectory.TAG_LONGITUDE_REF:
        case GpsDirectory.TAG_LONGITUDE:
        case GpsDirectory.TAG_ALTITUDE_REF:
        case GpsDirectory.TAG_ALTITUDE:
        case GpsDirectory.TAG_TIME_STAMP:
        case GpsDirectory.TAG_SATELLITES:
        case GpsDirectory.TAG_STATUS:
        case GpsDirectory.TAG_MEASURE_MODE:
        case GpsDirectory.TAG_DOP:
        case GpsDirectory.TAG_SPEED_REF:
        case GpsDirectory.TAG_SPEED:
        case GpsDirectory.TAG_TRACK_REF:
        case GpsDirectory.TAG_TRACK:
        case GpsDirectory.TAG_IMG_DIRECTION_REF:
        case GpsDirectory.TAG_IMG_DIRECTION:
        case GpsDirectory.TAG_MAP_DATUM:
        case GpsDirectory.TAG_DEST_LATITUDE_REF:
        case GpsDirectory.TAG_DEST_LATITUDE:
        case GpsDirectory.TAG_DEST_LONGITUDE_REF:
        case GpsDirectory.TAG_DEST_LONGITUDE:
        case GpsDirectory.TAG_DEST_BEARING_REF:
        case GpsDirectory.TAG_DEST_BEARING:
        case GpsDirectory.TAG_DEST_DISTANCE_REF:
        case GpsDirectory.TAG_DEST_DISTANCE:
        case GpsDirectory.TAG_PROCESSING_METHOD:
        case GpsDirectory.TAG_AREA_INFORMATION:
        case GpsDirectory.TAG_DATE_STAMP:
        case GpsDirectory.TAG_DIFFERENTIAL:
            // 必要なcaseの中で処理を行う
            System.out.println(tag.getTagName, tag.getDescription()));
            break;
    }
}