Package com.drew.metadata

Examples of com.drew.metadata.Metadata


    }

    @NotNull
    public static Metadata readMetadata(@NotNull InputStream inputStream)
    {
        Metadata metadata = new Metadata();
        new PsdReader().extract(new RandomAccessStreamReader(inputStream), metadata);
        return metadata;
    }
View Full Code Here


            File file = new File(filePath);

            if (!wikiFormat && argList.size()>1)
                System.out.printf("\n***** PROCESSING: %s\n%n", filePath);

            Metadata metadata = null;
            try {
                metadata = ImageMetadataReader.readMetadata(file);
            } catch (Exception e) {
                e.printStackTrace(System.err);
                System.exit(1);
            }
            long took = System.nanoTime() - startTime;
            if (!wikiFormat)
                System.out.printf("Processed %.3f MB file in %.2f ms%n%n", file.length() / (1024d * 1024), took / 1000000d);

            if (wikiFormat) {
                String fileName = file.getName();
                String urlName = StringUtil.urlEncode(fileName);
                ExifIFD0Directory exifIFD0Directory = metadata.getDirectory(ExifIFD0Directory.class);
                String make = exifIFD0Directory == null ? "" : StringUtil.escapeForWiki(exifIFD0Directory.getString(ExifIFD0Directory.TAG_MAKE));
                String model = exifIFD0Directory == null ? "" : StringUtil.escapeForWiki(exifIFD0Directory.getString(ExifIFD0Directory.TAG_MODEL));
                System.out.println();
                System.out.println("-----");
                System.out.println();
                System.out.printf("= %s - %s =%n", make, model);
                System.out.println();
                System.out.printf("<a href=\"http://sample-images.metadata-extractor.googlecode.com/git/%s\">%n", urlName);
                System.out.printf("<img src=\"http://sample-images.metadata-extractor.googlecode.com/git/%s\" width=\"300\"/><br/>%n", urlName);
                System.out.println(StringUtil.escapeForWiki(fileName));
                System.out.println("</a>");
                System.out.println();
                System.out.println("|| *Directory* || *Tag Id* || *Tag Name* || *Extracted Value* ||");
            }

            // iterate over the metadata and print to System.out
            for (Directory directory : metadata.getDirectories()) {
                String directoryName = directory.getName();
                for (Tag tag : directory.getTags()) {
                    String tagName = tag.getTagName();
                    String description = tag.getDescription();

                    // truncate the description if it's too long
                    if (description != null && description.length() > 1024) {
                        description = description.substring(0, 1024) + "...";
                    }

                    if (wikiFormat) {
                        System.out.printf("||%s||0x%s||%s||%s||%n",
                                StringUtil.escapeForWiki(directoryName),
                                Integer.toHexString(tag.getTagType()),
                                StringUtil.escapeForWiki(tagName),
                                StringUtil.escapeForWiki(description));
                    }
                    else
                    {
                        if (showHex) {
                            System.out.printf("[%s - %s] %s = %s%n", directoryName, tag.getTagTypeHex(), tagName, description);
                        } else {
                            System.out.printf("[%s] %s = %s%n", directoryName, tagName, description);
                        }
                    }
                }

                // print out any errors
                for (String error : directory.getErrors())
                    System.err.println("ERROR: " + error);
            }

            if (args.length > 1 && thumbRequested) {
                ExifThumbnailDirectory directory = metadata.getDirectory(ExifThumbnailDirectory.class);
                if (directory!=null && directory.hasThumbnailData()) {
                    System.out.println("Writing thumbnail...");
                    directory.writeThumbnail(args[0].trim() + ".thumb.jpg");
                } else {
                    System.out.println("No thumbnail data exists in this image");
View Full Code Here

public class TiffMetadataReader
{
    @NotNull
    public static Metadata readMetadata(@NotNull File file) throws IOException
    {
        Metadata metadata = new Metadata();
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");

        try {
            new ExifReader().extractTiff(new RandomAccessFileReader(randomAccessFile), metadata);
        } finally {
View Full Code Here

    {
        // TIFF processing requires random access, as directories can be scattered throughout the byte sequence.
        // InputStream does not support seeking backwards, and so is not a viable option for TIFF processing.
        // We use RandomAccessStreamReader, which buffers data from the stream as we seek forward.

        Metadata metadata = new Metadata();
        new ExifReader().extractTiff(new RandomAccessStreamReader(inputStream), metadata);
        return metadata;
    }
View Full Code Here

            } else if (handler.shouldProcess(file)) {

                handler.onProcessingStarting(file);

                // Read metadata
                final Metadata metadata;
                try {
                    metadata = ImageMetadataReader.readMetadata(file);
                } catch (Throwable t) {
                    handler.onException(file, t);
                    continue;
View Full Code Here

    }

    @NotNull
    public static Metadata readMetadata(@NotNull InputStream inputStream)
    {
        Metadata metadata = new Metadata();
        new GifReader().extract(new StreamReader(inputStream), metadata);
        return metadata;
    }
View Full Code Here

//        con.setConnectTimeout(connectTimeout);
//        con.setReadTimeout(readTimeout);
        InputStream in = con.getInputStream();

        // Read metadata
        final Metadata metadata;
        try {
            metadata = ImageMetadataReader.readMetadata(in);
        } catch (ImageProcessingException e) {
            // this is an error in the Jpeg segment structure.  we're looking for bad handling of
            // metadata segments.  in this case, we didn't even get a segment.
            System.err.printf("%s: %s [Error Extracting Metadata]\n\t%s%n", e.getClass().getName(), url, e.getMessage()); return;
        } catch (Throwable t) {
            // general, uncaught exception during processing of jpeg segments
            System.err.printf("%s: %s [Error Extracting Metadata]%n", t.getClass().getName(), url);
            t.printStackTrace(System.err);
            return;
        }

        if (metadata.hasErrors()) {
            System.err.println(url);
            for (Directory directory : metadata.getDirectories()) {
                if (!directory.hasErrors())
                    continue;
                for (String error : directory.getErrors()) {
                    System.err.printf("\t[%s] %s%n", directory.getName(), error);
                }
            }
        }

        // Iterate through all values
        for (Directory directory : metadata.getDirectories()) {
            for (Tag tag : directory.getTags()) {
                String tagName = tag.getTagName();
                String directoryName = directory.getName();
                String description = tag.getDescription();
View Full Code Here

    }

    @NotNull
    public static Metadata readMetadata(@NotNull InputStream inputStream)
    {
        Metadata metadata = new Metadata();
        new BmpReader().extract(new StreamReader(inputStream), metadata);
        return metadata;
    }
View Full Code Here

        desiredChunkTypes.add(PngChunkType.iTXt);
        desiredChunkTypes.add(PngChunkType.tIME);

        Iterable<PngChunk> chunks = new PngChunkReader().extract(new StreamReader(inputStream), desiredChunkTypes);

        Metadata metadata = new Metadata();
        List<KeyValuePair> textPairs = new ArrayList<KeyValuePair>();

        for (PngChunk chunk : chunks) {
            PngChunkType chunkType = chunk.getType();
            byte[] bytes = chunk.getBytes();

            if (chunkType.equals(PngChunkType.IHDR)) {
                PngHeader header = new PngHeader(bytes);
                PngDirectory directory = metadata.getOrCreateDirectory(PngDirectory.class);
                directory.setInt(PngDirectory.TAG_IMAGE_WIDTH, header.getImageWidth());
                directory.setInt(PngDirectory.TAG_IMAGE_HEIGHT, header.getImageHeight());
                directory.setInt(PngDirectory.TAG_BITS_PER_SAMPLE, header.getBitsPerSample());
                directory.setInt(PngDirectory.TAG_COLOR_TYPE, header.getColorType().getNumericValue());
                directory.setInt(PngDirectory.TAG_COMPRESSION_TYPE, header.getCompressionType());
                directory.setInt(PngDirectory.TAG_FILTER_METHOD, header.getFilterMethod());
                directory.setInt(PngDirectory.TAG_INTERLACE_METHOD, header.getInterlaceMethod());
            } else if (chunkType.equals(PngChunkType.PLTE)) {
                PngDirectory directory = metadata.getOrCreateDirectory(PngDirectory.class);
                directory.setInt(PngDirectory.TAG_PALETTE_SIZE, bytes.length / 3);
            } else if (chunkType.equals(PngChunkType.tRNS)) {
                PngDirectory directory = metadata.getOrCreateDirectory(PngDirectory.class);
                directory.setInt(PngDirectory.TAG_PALETTE_HAS_TRANSPARENCY, 1);
            } else if (chunkType.equals(PngChunkType.sRGB)) {
                int srgbRenderingIntent = new SequentialByteArrayReader(bytes).getInt8();
                PngDirectory directory = metadata.getOrCreateDirectory(PngDirectory.class);
                directory.setInt(PngDirectory.TAG_SRGB_RENDERING_INTENT, srgbRenderingIntent);
            } else if (chunkType.equals(PngChunkType.cHRM)) {
                PngChromaticities chromaticities = new PngChromaticities(bytes);
                PngChromaticitiesDirectory directory = metadata.getOrCreateDirectory(PngChromaticitiesDirectory.class);
                directory.setInt(PngChromaticitiesDirectory.TAG_WHITE_POINT_X, chromaticities.getWhitePointX());
                directory.setInt(PngChromaticitiesDirectory.TAG_WHITE_POINT_X, chromaticities.getWhitePointX());
                directory.setInt(PngChromaticitiesDirectory.TAG_RED_X, chromaticities.getRedX());
                directory.setInt(PngChromaticitiesDirectory.TAG_RED_Y, chromaticities.getRedY());
                directory.setInt(PngChromaticitiesDirectory.TAG_GREEN_X, chromaticities.getGreenX());
                directory.setInt(PngChromaticitiesDirectory.TAG_GREEN_Y, chromaticities.getGreenY());
                directory.setInt(PngChromaticitiesDirectory.TAG_BLUE_X, chromaticities.getBlueX());
                directory.setInt(PngChromaticitiesDirectory.TAG_BLUE_Y, chromaticities.getBlueY());
            } else if (chunkType.equals(PngChunkType.gAMA)) {
                int gammaInt = new SequentialByteArrayReader(bytes).getInt32();
                PngDirectory directory = metadata.getOrCreateDirectory(PngDirectory.class);
                directory.setDouble(PngDirectory.TAG_GAMMA, gammaInt / 100000.0);
            } else if (chunkType.equals(PngChunkType.iCCP)) {
                SequentialReader reader = new SequentialByteArrayReader(bytes);
                String profileName = reader.getNullTerminatedString(79);
                PngDirectory directory = metadata.getOrCreateDirectory(PngDirectory.class);
                directory.setString(PngDirectory.TAG_PROFILE_NAME, profileName);
                byte compressionMethod = reader.getInt8();
                if (compressionMethod == 0) {
                    // Only compression method allowed by the spec is zero: deflate
                    // This assumes 1-byte-per-char, which it is by spec.
                    int bytesLeft = bytes.length - profileName.length() - 2;
                    byte[] compressedProfile = reader.getBytes(bytesLeft);
                    InflaterInputStream inflateStream = new InflaterInputStream(new ByteArrayInputStream(compressedProfile));
                    new IccReader().extract(new RandomAccessStreamReader(inflateStream), metadata);
                    inflateStream.close();
                }
            } else if (chunkType.equals(PngChunkType.bKGD)) {
                PngDirectory directory = metadata.getOrCreateDirectory(PngDirectory.class);
                directory.setByteArray(PngDirectory.TAG_BACKGROUND_COLOR, bytes);
            } else if (chunkType.equals(PngChunkType.tEXt)) {
                SequentialReader reader = new SequentialByteArrayReader(bytes);
                String keyword = reader.getNullTerminatedString(79);
                int bytesLeft = bytes.length - keyword.length() - 1;
                String value = reader.getNullTerminatedString(bytesLeft);
                textPairs.add(new KeyValuePair(keyword, value));
            } else if (chunkType.equals(PngChunkType.iTXt)) {
                SequentialReader reader = new SequentialByteArrayReader(bytes);
                String keyword = reader.getNullTerminatedString(79);
                byte compressionFlag = reader.getInt8();
                byte compressionMethod = reader.getInt8();
                String languageTag = reader.getNullTerminatedString(bytes.length);
                String translatedKeyword = reader.getNullTerminatedString(bytes.length);
                int bytesLeft = bytes.length - keyword.length() - 1 - 1 - 1 - languageTag.length() - 1 - translatedKeyword.length() - 1;
                String text = null;
                if (compressionFlag == 0) {
                    text = reader.getNullTerminatedString(bytesLeft);
                } else if (compressionFlag == 1) {
                    if (compressionMethod == 0) {
                        text = StringUtil.fromStream(new InflaterInputStream(new ByteArrayInputStream(bytes, bytes.length - bytesLeft, bytesLeft)));
                    } else {
                        metadata.getOrCreateDirectory(PngDirectory.class).addError("Invalid compression method value");
                    }
                } else {
                    metadata.getOrCreateDirectory(PngDirectory.class).addError("Invalid compression flag value");
                }

                if (text != null) {
                    if (keyword.equals("XML:com.adobe.xmp")) {
                        // NOTE in testing images, the XMP has parsed successfully, but we are not extracting tags from it as necessary
                        new XmpReader().extract(text, metadata);
                    } else {
                        textPairs.add(new KeyValuePair(keyword, text));
                    }
                }
            } else if (chunkType.equals(PngChunkType.tIME)) {
                SequentialByteArrayReader reader = new SequentialByteArrayReader(bytes);
                int year = reader.getUInt16();
                int month = reader.getUInt8() - 1;
                int day = reader.getUInt8();
                int hour = reader.getUInt8();
                int minute = reader.getUInt8();
                int second = reader.getUInt8();
                Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
                //noinspection MagicConstant
                calendar.set(year, month, day, hour, minute, second);
                PngDirectory directory = metadata.getOrCreateDirectory(PngDirectory.class);
                directory.setDate(PngDirectory.TAG_LAST_MODIFICATION_TIME, calendar.getTime());
            }
        }

        if (textPairs.size() != 0) {
            PngDirectory directory = metadata.getOrCreateDirectory(PngDirectory.class);
            directory.setObject(PngDirectory.TAG_TEXTUAL_DATA, textPairs);
        }

        return metadata;
    }
View Full Code Here

            0,108,
            0,108,
            0,0
        };

        final Metadata metadata = new Metadata();
        final JfifReader reader = new JfifReader();
        reader.extract(new ByteArrayReader(jfifData), metadata);

        assertEquals(1, metadata.getDirectoryCount());
        JfifDirectory directory = metadata.getDirectory(JfifDirectory.class);
        assertNotNull(directory);
        assertFalse(directory.getErrors().toString(), directory.hasErrors());

        Tag[] tags = directory.getTags().toArray(new Tag[directory.getTagCount()]);
        assertEquals(4, tags.length);
View Full Code Here

TOP

Related Classes of com.drew.metadata.Metadata

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.