Search in sources :

Example 1 with ExifSubIFDDirectory

use of com.drew.metadata.exif.ExifSubIFDDirectory in project tika by apache.

the class ImageMetadataExtractorTest method testExifHandlerParseDate.

@Test
public void testExifHandlerParseDate() throws MetadataException {
    ExifSubIFDDirectory exif = mock(ExifSubIFDDirectory.class);
    when(exif.containsTag(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL)).thenReturn(true);
    GregorianCalendar calendar = new GregorianCalendar(TimeZone.getDefault(), Locale.ROOT);
    calendar.setTimeInMillis(0);
    calendar.set(2000, 0, 1, 0, 0, 0);
    when(exif.getDate(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL)).thenReturn(// jvm default timezone as in Metadata Extractor
    calendar.getTime());
    Metadata metadata = new Metadata();
    new ImageMetadataExtractor.ExifHandler().handle(exif, metadata);
    assertEquals("Should be ISO date without time zone", "2000-01-01T00:00:00", metadata.get(TikaCoreProperties.CREATED));
}
Also used : ExifSubIFDDirectory(com.drew.metadata.exif.ExifSubIFDDirectory) GregorianCalendar(java.util.GregorianCalendar) Metadata(org.apache.tika.metadata.Metadata) Test(org.junit.Test)

Example 2 with ExifSubIFDDirectory

use of com.drew.metadata.exif.ExifSubIFDDirectory in project UniversalMediaServer by UniversalMediaServer.

the class ExifInfo method parseMetadata.

@SuppressFBWarnings("SF_SWITCH_NO_DEFAULT")
@Override
protected void parseMetadata(Metadata metadata) {
    if (metadata == null) {
        return;
    }
    ((ExifParseInfo) parsedInfo).hasExifThumbnail = false;
    for (Directory directory : metadata.getDirectories()) {
        if (directory instanceof ExifIFD0Directory || directory instanceof ExifSubIFDDirectory) {
            if (// Prefer Exif SubIFD Exif image width and height as they seem to be more accurate.
            directory instanceof ExifSubIFDDirectory && ((ExifSubIFDDirectory) directory).containsTag(ExifSubIFDDirectory.TAG_EXIF_IMAGE_WIDTH) && ((ExifSubIFDDirectory) directory).containsTag(ExifSubIFDDirectory.TAG_EXIF_IMAGE_HEIGHT)) {
                parsedInfo.width = ((ExifSubIFDDirectory) directory).getInteger(ExifSubIFDDirectory.TAG_EXIF_IMAGE_WIDTH);
                parsedInfo.height = ((ExifSubIFDDirectory) directory).getInteger(ExifSubIFDDirectory.TAG_EXIF_IMAGE_HEIGHT);
            } else if ((parsedInfo.width == null || parsedInfo.height == null || parsedInfo.width < 1 || parsedInfo.height < 1) && ((ExifDirectoryBase) directory).containsTag(ExifDirectoryBase.TAG_IMAGE_WIDTH) && ((ExifDirectoryBase) directory).containsTag(ExifDirectoryBase.TAG_IMAGE_HEIGHT)) {
                parsedInfo.width = ((ExifDirectoryBase) directory).getInteger(ExifDirectoryBase.TAG_IMAGE_WIDTH);
                parsedInfo.height = ((ExifDirectoryBase) directory).getInteger(ExifDirectoryBase.TAG_IMAGE_HEIGHT);
            }
            if (((ExifDirectoryBase) directory).containsTag(ExifDirectoryBase.TAG_ORIENTATION)) {
                Integer i = ((ExifDirectoryBase) directory).getInteger(ExifDirectoryBase.TAG_ORIENTATION);
                if (i != null) {
                    ((ExifParseInfo) parsedInfo).exifOrientation = ExifOrientation.typeOf(i);
                }
            }
            if (((ExifDirectoryBase) directory).containsTag(ExifDirectoryBase.TAG_COMPRESSION) && ((ExifParseInfo) parsedInfo).exifCompression == null) {
                Integer i = ((ExifDirectoryBase) directory).getInteger(ExifDirectoryBase.TAG_COMPRESSION);
                if (i != null) {
                    ((ExifParseInfo) parsedInfo).exifCompression = ExifCompression.typeOf(i);
                }
            }
            if (((ExifDirectoryBase) directory).containsTag(ExifDirectoryBase.TAG_BITS_PER_SAMPLE) && ((ExifParseInfo) parsedInfo).exifCompression == ExifCompression.UNCOMPRESSED) {
                byte[] bytes = ((ExifDirectoryBase) directory).getByteArray(ExifDirectoryBase.TAG_BITS_PER_SAMPLE);
                if (bytes != null && bytes.length > 0) {
                    Integer i;
                    try {
                        i = Integer.valueOf(ImagesUtil.getBitDepthFromArray(bytes));
                    } catch (InvalidStateException e) {
                        i = null;
                        LOGGER.trace("Unexpected bit depth array retrieved from Exif tag: {}", Arrays.toString(bytes));
                    }
                    if (i != null && i.intValue() > 0) {
                        parsedInfo.bitDepth = i;
                    }
                }
            }
            if (((ExifDirectoryBase) directory).containsTag(ExifDirectoryBase.TAG_SAMPLES_PER_PIXEL) && parsedInfo.numComponents == null) {
                Integer i = ((ExifDirectoryBase) directory).getInteger(ExifDirectoryBase.TAG_SAMPLES_PER_PIXEL);
                if (i != null) {
                    parsedInfo.numComponents = i;
                }
            }
            if (((ExifDirectoryBase) directory).containsTag(ExifDirectoryBase.TAG_PHOTOMETRIC_INTERPRETATION)) {
                Integer i = ((ExifDirectoryBase) directory).getInteger(ExifDirectoryBase.TAG_PHOTOMETRIC_INTERPRETATION);
                if (i != null) {
                    ((ExifParseInfo) parsedInfo).photometricInterpretation = PhotometricInterpretation.typeOf(i);
                    if (((ExifParseInfo) parsedInfo).photometricInterpretation != null) {
                        switch(((ExifParseInfo) parsedInfo).photometricInterpretation) {
                            case RGB:
                            case RGB_PALETTE:
                                parsedInfo.colorSpaceType = ColorSpaceType.TYPE_RGB;
                                break;
                            case Y_CB_CR:
                                parsedInfo.colorSpaceType = ColorSpaceType.TYPE_YCbCr;
                                break;
                            case CMYK:
                                parsedInfo.colorSpaceType = ColorSpaceType.TYPE_CMYK;
                                break;
                            case CIE_LAB:
                            case ICC_LAB:
                            case ITU_LAB:
                                parsedInfo.colorSpaceType = ColorSpaceType.TYPE_Lab;
                                break;
                            case PIXAR_LOG_LUV:
                                parsedInfo.colorSpaceType = ColorSpaceType.TYPE_Luv;
                                break;
                            default:
                        }
                    }
                }
            }
            if (((ExifDirectoryBase) directory).containsTag(ExifDirectoryBase.TAG_EXIF_VERSION)) {
                byte[] bytes = ((ExifDirectoryBase) directory).getByteArray(ExifDirectoryBase.TAG_EXIF_VERSION);
                if (bytes != null) {
                    ((ExifParseInfo) parsedInfo).exifVersion = ImagesUtil.parseExifVersion(bytes);
                }
            }
            if (directory instanceof ExifIFD0Directory && ((ExifDirectoryBase) directory).containsTag(ExifDirectoryBase.TAG_COLOR_SPACE)) {
                Integer i = ((ExifDirectoryBase) directory).getInteger(ExifDirectoryBase.TAG_COLOR_SPACE);
                if (i != null) {
                    ((ExifParseInfo) parsedInfo).exifColorSpace = ExifColorSpace.typeOf(i.intValue());
                }
            }
        } else if (directory instanceof ExifThumbnailDirectory && !((ExifParseInfo) parsedInfo).hasExifThumbnail.booleanValue()) {
            if (((ExifThumbnailDirectory) directory).containsTag(ExifThumbnailDirectory.TAG_THUMBNAIL_LENGTH)) {
                Integer i = ((ExifThumbnailDirectory) directory).getInteger(ExifThumbnailDirectory.TAG_THUMBNAIL_LENGTH);
                ((ExifParseInfo) parsedInfo).hasExifThumbnail = Boolean.valueOf(i != null && i.intValue() > 0);
            }
        }
    }
}
Also used : ExifSubIFDDirectory(com.drew.metadata.exif.ExifSubIFDDirectory) ExifIFD0Directory(com.drew.metadata.exif.ExifIFD0Directory) ExifThumbnailDirectory(com.drew.metadata.exif.ExifThumbnailDirectory) ExifDirectoryBase(com.drew.metadata.exif.ExifDirectoryBase) InvalidStateException(net.pms.util.InvalidStateException) ExifSubIFDDirectory(com.drew.metadata.exif.ExifSubIFDDirectory) Directory(com.drew.metadata.Directory) ExifThumbnailDirectory(com.drew.metadata.exif.ExifThumbnailDirectory) ExifIFD0Directory(com.drew.metadata.exif.ExifIFD0Directory) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 3 with ExifSubIFDDirectory

use of com.drew.metadata.exif.ExifSubIFDDirectory in project react-native-camera by react-native-community.

the class MutableImage method writeDataToFile.

public void writeDataToFile(File file, ReadableMap options, int jpegQualityPercent) throws IOException {
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(toJpeg(currentRepresentation, jpegQualityPercent));
    fos.close();
    try {
        ExifInterface exif = new ExifInterface(file.getAbsolutePath());
        // copy original exif data to the output exif...
        for (Directory directory : originalImageMetaData().getDirectories()) {
            for (Tag tag : directory.getTags()) {
                int tagType = tag.getTagType();
                Object object = directory.getObject(tagType);
                exif.setAttribute(tag.getTagName(), object.toString());
            }
        }
        // Add missing exif data from a sub directory
        ExifSubIFDDirectory directory = originalImageMetaData().getFirstDirectoryOfType(ExifSubIFDDirectory.class);
        for (Tag tag : directory.getTags()) {
            int tagType = tag.getTagType();
            // As some of exif data does not follow naming of the ExifInterface the names need
            // to be transformed into Upper camel case format.
            String tagName = tag.getTagName().replaceAll(" ", "");
            Object object = directory.getObject(tagType);
            if (tagName.equals(ExifInterface.TAG_EXPOSURE_TIME)) {
                exif.setAttribute(tagName, convertExposureTimeToDoubleFormat(object.toString()));
            } else {
                exif.setAttribute(tagName, object.toString());
            }
        }
        writeLocationExifData(options, exif);
        if (hasBeenReoriented)
            rewriteOrientation(exif);
        exif.saveAttributes();
    } catch (ImageProcessingException | IOException e) {
        Log.e(TAG, "failed to save exif data", e);
    }
}
Also used : ExifSubIFDDirectory(com.drew.metadata.exif.ExifSubIFDDirectory) ImageProcessingException(com.drew.imaging.ImageProcessingException) FileOutputStream(java.io.FileOutputStream) ExifInterface(android.media.ExifInterface) Tag(com.drew.metadata.Tag) IOException(java.io.IOException) ExifSubIFDDirectory(com.drew.metadata.exif.ExifSubIFDDirectory) Directory(com.drew.metadata.Directory) ExifIFD0Directory(com.drew.metadata.exif.ExifIFD0Directory)

Example 4 with ExifSubIFDDirectory

use of com.drew.metadata.exif.ExifSubIFDDirectory in project structr by structr.

the class ImageHelper method getExifData.

public static JSONObject getExifData(final File originalImage) {
    final JSONObject exifDataJson = new JSONObject();
    try {
        final Metadata metadata = getMetadata(originalImage);
        final ExifIFD0Directory exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
        final ExifSubIFDDirectory exifSubIFDDirectory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
        final GpsDirectory gpsDirectory = metadata.getFirstDirectoryOfType(GpsDirectory.class);
        if (exifIFD0Directory != null) {
            final JSONObject exifIFD0DataJson = new JSONObject();
            exifIFD0Directory.getTags().forEach((tag) -> {
                exifIFD0DataJson.put(tag.getTagName(), exifIFD0Directory.getDescription(tag.getTagType()));
            });
            originalImage.setProperty(StructrApp.key(Image.class, "exifIFD0Data"), exifIFD0DataJson.toString());
            exifDataJson.putOnce("exifIFD0Data", exifIFD0DataJson);
        }
        if (exifSubIFDDirectory != null) {
            final JSONObject exifSubIFDDataJson = new JSONObject();
            exifSubIFDDirectory.getTags().forEach((tag) -> {
                exifSubIFDDataJson.put(tag.getTagName(), exifSubIFDDirectory.getDescription(tag.getTagType()));
            });
            originalImage.setProperty(StructrApp.key(Image.class, "exifSubIFDData"), exifSubIFDDataJson.toString());
            exifDataJson.putOnce("exifSubIFDData", exifSubIFDDataJson);
        }
        if (gpsDirectory != null) {
            final JSONObject exifGpsDataJson = new JSONObject();
            gpsDirectory.getTags().forEach((tag) -> {
                exifGpsDataJson.put(tag.getTagName(), gpsDirectory.getDescription(tag.getTagType()));
            });
            originalImage.setProperty(StructrApp.key(Image.class, "gpsData"), exifGpsDataJson.toString());
            exifDataJson.putOnce("gpsData", exifGpsDataJson);
        }
        return exifDataJson;
    } catch (Exception ex) {
        logger.warn("Unable to extract EXIF metadata.", ex);
    }
    return null;
}
Also used : GpsDirectory(com.drew.metadata.exif.GpsDirectory) ExifSubIFDDirectory(com.drew.metadata.exif.ExifSubIFDDirectory) JSONObject(org.json.JSONObject) Metadata(com.drew.metadata.Metadata) ExifIFD0Directory(com.drew.metadata.exif.ExifIFD0Directory) BufferedImage(java.awt.image.BufferedImage) Image(org.structr.web.entity.Image) JSONException(org.json.JSONException) FrameworkException(org.structr.common.error.FrameworkException) ImageProcessingException(com.drew.imaging.ImageProcessingException) MetadataException(com.drew.metadata.MetadataException) IOException(java.io.IOException)

Aggregations

ExifSubIFDDirectory (com.drew.metadata.exif.ExifSubIFDDirectory)4 ExifIFD0Directory (com.drew.metadata.exif.ExifIFD0Directory)3 ImageProcessingException (com.drew.imaging.ImageProcessingException)2 Directory (com.drew.metadata.Directory)2 IOException (java.io.IOException)2 ExifInterface (android.media.ExifInterface)1 Metadata (com.drew.metadata.Metadata)1 MetadataException (com.drew.metadata.MetadataException)1 Tag (com.drew.metadata.Tag)1 ExifDirectoryBase (com.drew.metadata.exif.ExifDirectoryBase)1 ExifThumbnailDirectory (com.drew.metadata.exif.ExifThumbnailDirectory)1 GpsDirectory (com.drew.metadata.exif.GpsDirectory)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 BufferedImage (java.awt.image.BufferedImage)1 FileOutputStream (java.io.FileOutputStream)1 GregorianCalendar (java.util.GregorianCalendar)1 InvalidStateException (net.pms.util.InvalidStateException)1 Metadata (org.apache.tika.metadata.Metadata)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1