Introduction
In this Java: Read Image Metadata by Java Image IO post, I demonstrated the code snippet for reading image metadata only using standard java imageio library.
The problem is the standard image library cannot read jpeg exif metadata as human readable format (they can be extracted as byte[] data).
In this post, I will show you the code for reading jpeg exif metadata using Apache Commons Imaging library.
In order to run the example code, you should download commons-imaging-1.0-SNAPSHOT.jar from commons-imaging.
Basic of Read Exif Data
Extracting jpeg exif metadata can be done by using findEXIFValue method in JpegImageMetadata class.
The method takes TagInfo as an argument and return back TiffField.
Available TagInfo static fields (constants) are mainly defined in the following class in org.apache.commons.imaging.formats.tiff.constants package:
- ExifTagConstants
- TiffTagConstants
- GpsTagConstants
Available TagInfo in ExifTagConstants, TiffTagConstants, GpsTagConstants
There are a lot of static fields are defined in ExifTagConstants, TiffTagConstants, GpsTagConstants classes.
So first, I wrote small code snippet for printing out all static fields whose type is TagInfo.
The code might be good example for Java 8 lambda and reflection, too.
package com.dulesoftware.image; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import org.apache.commons.imaging.formats.tiff.constants.ExifTagConstants; import org.apache.commons.imaging.formats.tiff.constants.GpsTagConstants; import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants; import org.apache.commons.imaging.formats.tiff.taginfos.TagInfo; public class ImageMetaDtaUtils { public static void main(String[] args) { List<Field> exifTagfields = getTagInfoDefinedInClass(ExifTagConstants.class); System.out.printf("Fields defined in %s\n", ExifTagConstants.class.getSimpleName()); printFieldsName(exifTagfields); System.out.println(); List<Field> tiffTagfields = getTagInfoDefinedInClass(TiffTagConstants.class); System.out.printf("Fields defined in %s\n", TiffTagConstants.class.getSimpleName()); printFieldsName(tiffTagfields); System.out.println(); List<Field> gpsTagfields = getTagInfoDefinedInClass(GpsTagConstants.class); System.out.printf("Fields defined in %s\n", GpsTagConstants.class.getSimpleName()); printFieldsName(gpsTagfields); System.out.println(); } private static void printFieldsName(List<Field> fields) { for (Field field : fields) { System.out.println(field.getName()); } } private static List<Field> getTagInfoDefinedInClass(Class<?> clazz) { Field[] declaredFields = clazz.getDeclaredFields(); return Arrays.stream(declaredFields) .filter(field -> TagInfo.class.isAssignableFrom(field.getType()) && Modifier.isStatic(field.getModifiers()) ) .collect(Collectors.toList()); } }
The result of above code is below... Surprised a bit quite a lot!
In the next post, I will show you an example code for extracting exif metadata using Following constants!!
Fields defined in ExifTagConstants EXIF_TAG_INTEROPERABILITY_INDEX EXIF_TAG_INTEROPERABILITY_VERSION EXIF_TAG_PROCESSING_SOFTWARE EXIF_TAG_SOFTWARE EXIF_TAG_PREVIEW_IMAGE_START_IFD0 EXIF_TAG_PREVIEW_IMAGE_START_SUB_IFD1 EXIF_TAG_JPG_FROM_RAW_START_SUB_IFD2 EXIF_TAG_PREVIEW_IMAGE_LENGTH_IFD0 EXIF_TAG_PREVIEW_IMAGE_LENGTH_SUB_IFD1 EXIF_TAG_JPG_FROM_RAW_LENGTH_SUB_IFD2 EXIF_TAG_PREVIEW_IMAGE_START_MAKER_NOTES EXIF_TAG_JPG_FROM_RAW_START_SUB_IFD EXIF_TAG_JPG_FROM_RAW_START_IFD2 EXIF_TAG_OTHER_IMAGE_START EXIF_TAG_PREVIEW_IMAGE_LENGTH_MAKER_NOTES EXIF_TAG_JPG_FROM_RAW_LENGTH_SUB_IFD EXIF_TAG_JPG_FROM_RAW_LENGTH_IFD2 EXIF_TAG_OTHER_IMAGE_LENGTH EXIF_TAG_APPLICATION_NOTES EXIF_TAG_MATTEING EXIF_TAG_DATA_TYPE EXIF_TAG_IMAGE_DEPTH EXIF_TAG_TILE_DEPTH EXIF_TAG_MODEL_2 EXIF_TAG_EXPOSURE_TIME EXIF_TAG_FNUMBER EXIF_TAG_IPTC_NAA EXIF_TAG_INTERGRAPH_PACKET_DATA EXIF_TAG_INTERGRAPH_FLAG_REGISTERS EXIF_TAG_SITE EXIF_TAG_COLOR_SEQUENCE EXIF_TAG_IT8HEADER EXIF_TAG_RASTER_PADDING EXIF_TAG_BITS_PER_RUN_LENGTH EXIF_TAG_BITS_PER_EXTENDED_RUN_LENGTH EXIF_TAG_COLOR_TABLE EXIF_TAG_IMAGE_COLOR_INDICATOR EXIF_TAG_BACKGROUND_COLOR_INDICATOR EXIF_TAG_IMAGE_COLOR_VALUE EXIF_TAG_BACKGROUND_COLOR_VALUE EXIF_TAG_PIXEL_INTENSITY_RANGE EXIF_TAG_TRANSPARENCY_INDICATOR EXIF_TAG_COLOR_CHARACTERIZATION EXIF_TAG_HCUSAGE EXIF_TAG_SEMINFO EXIF_TAG_AFCP_IPTC EXIF_TAG_LEAF_DATA EXIF_TAG_PHOTOSHOP_SETTINGS EXIF_TAG_EXIF_OFFSET EXIF_TAG_EXPOSURE_PROGRAM EXIF_TAG_SPECTRAL_SENSITIVITY EXIF_TAG_GPSINFO EXIF_TAG_ISO EXIF_TAG_OPTO_ELECTRIC_CONV_FACTOR EXIF_TAG_LEAF_SUB_IFD EXIF_TAG_EXIF_VERSION EXIF_TAG_DATE_TIME_ORIGINAL EXIF_TAG_DATE_TIME_DIGITIZED EXIF_TAG_COMPONENTS_CONFIGURATION EXIF_TAG_COMPRESSED_BITS_PER_PIXEL EXIF_TAG_SHUTTER_SPEED_VALUE EXIF_TAG_APERTURE_VALUE EXIF_TAG_BRIGHTNESS_VALUE EXIF_TAG_EXPOSURE_COMPENSATION EXIF_TAG_MAX_APERTURE_VALUE EXIF_TAG_SUBJECT_DISTANCE EXIF_TAG_METERING_MODE EXIF_TAG_LIGHT_SOURCE EXIF_TAG_FLASH EXIF_TAG_FOCAL_LENGTH EXIF_TAG_SUBJECT_AREA EXIF_TAG_STO_NITS EXIF_TAG_MAKER_NOTE EXIF_TAG_USER_COMMENT EXIF_TAG_SUB_SEC_TIME EXIF_TAG_SUB_SEC_TIME_ORIGINAL EXIF_TAG_SUB_SEC_TIME_DIGITIZED EXIF_TAG_FLASHPIX_VERSION EXIF_TAG_EXIF_IMAGE_WIDTH EXIF_TAG_EXIF_IMAGE_LENGTH EXIF_TAG_RELATED_SOUND_FILE EXIF_TAG_INTEROP_OFFSET EXIF_TAG_FLASH_ENERGY_EXIF_IFD EXIF_TAG_SPATIAL_FREQUENCY_RESPONSE_2 EXIF_TAG_NOISE_2 EXIF_TAG_FOCAL_PLANE_XRESOLUTION_EXIF_IFD EXIF_TAG_FOCAL_PLANE_YRESOLUTION_EXIF_IFD EXIF_TAG_FOCAL_PLANE_RESOLUTION_UNIT_EXIF_IFD EXIF_TAG_IMAGE_NUMBER EXIF_TAG_SECURITY_CLASSIFICATION EXIF_TAG_IMAGE_HISTORY EXIF_TAG_SUBJECT_LOCATION EXIF_TAG_EXPOSURE_INDEX_EXIF_IFD EXIF_TAG_TIFF_EPSTANDARD_ID_2 EXIF_TAG_SENSING_METHOD_EXIF_IFD EXIF_TAG_FILE_SOURCE EXIF_TAG_SCENE_TYPE EXIF_TAG_CFAPATTERN EXIF_TAG_CUSTOM_RENDERED EXIF_TAG_EXPOSURE_MODE EXIF_TAG_WHITE_BALANCE_1 EXIF_TAG_DIGITAL_ZOOM_RATIO EXIF_TAG_FOCAL_LENGTH_IN_35MM_FORMAT EXIF_TAG_SCENE_CAPTURE_TYPE EXIF_TAG_GAIN_CONTROL EXIF_TAG_CONTRAST_1 EXIF_TAG_SATURATION_1 EXIF_TAG_SHARPNESS_1 EXIF_TAG_DEVICE_SETTING_DESCRIPTION EXIF_TAG_SUBJECT_DISTANCE_RANGE EXIF_TAG_IMAGE_UNIQUE_ID EXIF_TAG_GAMMA EXIF_TAG_ANNOTATIONS EXIF_TAG_PRINT_IM EXIF_TAG_OFFSET_SCHEMA EXIF_TAG_OWNER_NAME EXIF_TAG_SERIAL_NUMBER EXIF_TAG_LENS EXIF_TAG_RAW_FILE EXIF_TAG_CONVERTER EXIF_TAG_WHITE_BALANCE_2 EXIF_TAG_EXPOSURE EXIF_TAG_SHADOWS EXIF_TAG_BRIGHTNESS EXIF_TAG_CONTRAST_2 EXIF_TAG_SATURATION_2 EXIF_TAG_SHARPNESS_2 EXIF_TAG_SMOOTHNESS EXIF_TAG_MOIRE_FILTER Fields defined in TiffTagConstants TIFF_TAG_NEW_SUBFILE_TYPE TIFF_TAG_SUBFILE_TYPE TIFF_TAG_IMAGE_WIDTH TIFF_TAG_IMAGE_LENGTH TIFF_TAG_BITS_PER_SAMPLE TIFF_TAG_COMPRESSION TIFF_TAG_PHOTOMETRIC_INTERPRETATION TIFF_TAG_THRESHHOLDING TIFF_TAG_CELL_WIDTH TIFF_TAG_CELL_LENGTH TIFF_TAG_FILL_ORDER TIFF_TAG_DOCUMENT_NAME TIFF_TAG_IMAGE_DESCRIPTION TIFF_TAG_MAKE TIFF_TAG_MODEL TIFF_TAG_STRIP_OFFSETS TIFF_TAG_ORIENTATION TIFF_TAG_SAMPLES_PER_PIXEL TIFF_TAG_ROWS_PER_STRIP TIFF_TAG_STRIP_BYTE_COUNTS TIFF_TAG_MIN_SAMPLE_VALUE TIFF_TAG_MAX_SAMPLE_VALUE TIFF_TAG_XRESOLUTION TIFF_TAG_YRESOLUTION TIFF_TAG_PLANAR_CONFIGURATION TIFF_TAG_PAGE_NAME TIFF_TAG_XPOSITION TIFF_TAG_YPOSITION TIFF_TAG_FREE_OFFSETS TIFF_TAG_FREE_BYTE_COUNTS TIFF_TAG_GRAY_RESPONSE_UNIT TIFF_TAG_GRAY_RESPONSE_CURVE TIFF_TAG_T4_OPTIONS TIFF_TAG_T6_OPTIONS TIFF_TAG_RESOLUTION_UNIT TIFF_TAG_PAGE_NUMBER TIFF_TAG_TRANSFER_FUNCTION TIFF_TAG_SOFTWARE TIFF_TAG_DATE_TIME TIFF_TAG_ARTIST TIFF_TAG_HOST_COMPUTER TIFF_TAG_PREDICTOR TIFF_TAG_WHITE_POINT TIFF_TAG_PRIMARY_CHROMATICITIES TIFF_TAG_COLOR_MAP TIFF_TAG_HALFTONE_HINTS TIFF_TAG_TILE_WIDTH TIFF_TAG_TILE_LENGTH TIFF_TAG_TILE_OFFSETS TIFF_TAG_TILE_BYTE_COUNTS TIFF_TAG_INK_SET TIFF_TAG_INK_NAMES TIFF_TAG_NUMBER_OF_INKS TIFF_TAG_DOT_RANGE TIFF_TAG_TARGET_PRINTER TIFF_TAG_EXTRA_SAMPLES TIFF_TAG_SAMPLE_FORMAT TIFF_TAG_SMIN_SAMPLE_VALUE TIFF_TAG_SMAX_SAMPLE_VALUE TIFF_TAG_TRANSFER_RANGE TIFF_TAG_JPEG_PROC TIFF_TAG_JPEG_INTERCHANGE_FORMAT TIFF_TAG_JPEG_INTERCHANGE_FORMAT_LENGTH TIFF_TAG_JPEG_RESTART_INTERVAL TIFF_TAG_JPEG_LOSSLESS_PREDICTORS TIFF_TAG_JPEG_POINT_TRANSFORMS TIFF_TAG_JPEG_QTABLES TIFF_TAG_JPEG_DCTABLES TIFF_TAG_JPEG_ACTABLES TIFF_TAG_YCBCR_COEFFICIENTS TIFF_TAG_YCBCR_SUB_SAMPLING TIFF_TAG_YCBCR_POSITIONING TIFF_TAG_REFERENCE_BLACK_WHITE TIFF_TAG_COPYRIGHT TIFF_TAG_XMP TIFF_TAG_UNKNOWN Fields defined in GpsTagConstants GPS_TAG_GPS_VERSION_ID GPS_TAG_GPS_LATITUDE_REF GPS_TAG_GPS_LATITUDE GPS_TAG_GPS_LONGITUDE_REF GPS_TAG_GPS_LONGITUDE GPS_TAG_GPS_ALTITUDE_REF GPS_TAG_GPS_ALTITUDE GPS_TAG_GPS_TIME_STAMP GPS_TAG_GPS_SATELLITES GPS_TAG_GPS_STATUS GPS_TAG_GPS_MEASURE_MODE GPS_TAG_GPS_DOP GPS_TAG_GPS_SPEED_REF GPS_TAG_GPS_SPEED GPS_TAG_GPS_TRACK_REF GPS_TAG_GPS_TRACK GPS_TAG_GPS_IMG_DIRECTION_REF GPS_TAG_GPS_IMG_DIRECTION GPS_TAG_GPS_MAP_DATUM GPS_TAG_GPS_DEST_LATITUDE_REF GPS_TAG_GPS_DEST_LATITUDE GPS_TAG_GPS_DEST_LONGITUDE_REF GPS_TAG_GPS_DEST_LONGITUDE GPS_TAG_GPS_DEST_BEARING_REF GPS_TAG_GPS_DEST_BEARING GPS_TAG_GPS_DEST_DISTANCE_REF GPS_TAG_GPS_DEST_DISTANCE GPS_TAG_GPS_PROCESSING_METHOD GPS_TAG_GPS_AREA_INFORMATION GPS_TAG_GPS_DATE_STAMP GPS_TAG_GPS_DIFFERENTIAL
コメント
best java training in coimbatore
php training in coimbatore
best php training institutes in coimbatore