Java Code Examples for com.drew.metadata.Metadata#addDirectory()
The following examples show how to use
com.drew.metadata.Metadata#addDirectory() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageMetadataReader.java From metadata-extractor with Apache License 2.0 | 6 votes |
/** * Reads metadata from an {@link InputStream} of known length. * * @param inputStream a stream from which the file data may be read. The stream must be positioned at the * beginning of the file's data. * @param streamLength the length of the stream, if known, otherwise -1. * @return a populated {@link Metadata} object containing directories of tags with values and any processing errors. * @throws ImageProcessingException if the file type is unknown, or for general processing errors. */ @NotNull public static Metadata readMetadata(@NotNull final InputStream inputStream, final long streamLength) throws ImageProcessingException, IOException { BufferedInputStream bufferedInputStream = inputStream instanceof BufferedInputStream ? (BufferedInputStream)inputStream : new BufferedInputStream(inputStream); FileType fileType = FileTypeDetector.detectFileType(bufferedInputStream); Metadata metadata = readMetadata(bufferedInputStream, streamLength, fileType); metadata.addDirectory(new FileTypeDirectory(fileType)); return metadata; }
Example 2
Source File: FileSystemMetadataReader.java From metadata-extractor with Apache License 2.0 | 6 votes |
public void read(@NotNull File file, @NotNull Metadata metadata) throws IOException { if (!file.isFile()) throw new IOException("File object must reference a file"); if (!file.exists()) throw new IOException("File does not exist"); if (!file.canRead()) throw new IOException("File is not readable"); FileSystemDirectory directory = metadata.getFirstDirectoryOfType(FileSystemDirectory.class); if (directory == null) { directory = new FileSystemDirectory(); metadata.addDirectory(directory); } directory.setString(FileSystemDirectory.TAG_FILE_NAME, file.getName()); directory.setLong(FileSystemDirectory.TAG_FILE_SIZE, file.length()); directory.setDate(FileSystemDirectory.TAG_FILE_MODIFIED_DATE, new Date(file.lastModified())); }
Example 3
Source File: PngMetadataReader.java From metadata-extractor with Apache License 2.0 | 6 votes |
@NotNull public static Metadata readMetadata(@NotNull InputStream inputStream) throws PngProcessingException, IOException { Iterable<PngChunk> chunks = new PngChunkReader().extract(new StreamReader(inputStream), _desiredChunkTypes); Metadata metadata = new Metadata(); for (PngChunk chunk : chunks) { try { processChunk(metadata, chunk); } catch (Exception e) { metadata.addDirectory(new ErrorDirectory("Exception reading PNG chunk: " + e.getMessage())); } } return metadata; }
Example 4
Source File: AdobeJpegReader.java From metadata-extractor with Apache License 2.0 | 6 votes |
public void extract(@NotNull SequentialReader reader, @NotNull Metadata metadata) { Directory directory = new AdobeJpegDirectory(); metadata.addDirectory(directory); try { reader.setMotorolaByteOrder(false); if (!reader.getString(PREAMBLE.length()).equals(PREAMBLE)) { directory.addError("Invalid Adobe JPEG data header."); return; } directory.setInt(AdobeJpegDirectory.TAG_DCT_ENCODE_VERSION, reader.getUInt16()); directory.setInt(AdobeJpegDirectory.TAG_APP14_FLAGS0, reader.getUInt16()); directory.setInt(AdobeJpegDirectory.TAG_APP14_FLAGS1, reader.getUInt16()); directory.setInt(AdobeJpegDirectory.TAG_COLOR_TRANSFORM, reader.getInt8()); } catch (IOException ex) { directory.addError("IO exception processing data: " + ex.getMessage()); } }
Example 5
Source File: JpegDnlReader.java From metadata-extractor with Apache License 2.0 | 6 votes |
public void extract(byte[] segmentBytes, Metadata metadata, JpegSegmentType segmentType) { JpegDirectory directory = metadata.getFirstDirectoryOfType(JpegDirectory.class); if (directory == null) { ErrorDirectory errorDirectory = new ErrorDirectory(); metadata.addDirectory(errorDirectory); errorDirectory.addError("DNL segment found without SOFx - illegal JPEG format"); return; } SequentialReader reader = new SequentialByteArrayReader(segmentBytes); try { // Only set height from DNL if it's not already defined Integer i = directory.getInteger(JpegDirectory.TAG_IMAGE_HEIGHT); if (i == null || i == 0) { directory.setInt(JpegDirectory.TAG_IMAGE_HEIGHT, reader.getUInt16()); } } catch (IOException ex) { directory.addError(ex.getMessage()); } }
Example 6
Source File: JfifReader.java From metadata-extractor with Apache License 2.0 | 6 votes |
/** * Performs the Jfif data extraction, adding found values to the specified * instance of {@link Metadata}. */ public void extract(@NotNull final RandomAccessReader reader, @NotNull final Metadata metadata) { JfifDirectory directory = new JfifDirectory(); metadata.addDirectory(directory); try { // For JFIF, the tag number is also the offset into the segment directory.setInt(JfifDirectory.TAG_VERSION, reader.getUInt16(JfifDirectory.TAG_VERSION)); directory.setInt(JfifDirectory.TAG_UNITS, reader.getUInt8(JfifDirectory.TAG_UNITS)); directory.setInt(JfifDirectory.TAG_RESX, reader.getUInt16(JfifDirectory.TAG_RESX)); directory.setInt(JfifDirectory.TAG_RESY, reader.getUInt16(JfifDirectory.TAG_RESY)); directory.setInt(JfifDirectory.TAG_THUMB_WIDTH, reader.getUInt8(JfifDirectory.TAG_THUMB_WIDTH)); directory.setInt(JfifDirectory.TAG_THUMB_HEIGHT, reader.getUInt8(JfifDirectory.TAG_THUMB_HEIGHT)); } catch (IOException me) { directory.addError(me.getMessage()); } }
Example 7
Source File: XmpReader.java From metadata-extractor with Apache License 2.0 | 6 votes |
/** * Performs the XMP data extraction, adding found values to the specified instance of {@link Metadata}. * <p> * The extraction is done with Adobe's XMPCore library. */ public void extract(@NotNull final String xmpString, @NotNull Metadata metadata, @Nullable Directory parentDirectory) { XmpDirectory directory = new XmpDirectory(); if (parentDirectory != null) directory.setParent(parentDirectory); try { XMPMeta xmpMeta = XMPMetaFactory.parseFromString(xmpString, PARSE_OPTIONS); directory.setXMPMeta(xmpMeta); } catch (XMPException e) { directory.addError("Error processing XMP data: " + e.getMessage()); } if (!directory.isEmpty()) metadata.addDirectory(directory); }
Example 8
Source File: JpegDhtReader.java From metadata-extractor with Apache License 2.0 | 5 votes |
/** * Performs the DHT tables extraction, adding found tables to the specified * instance of {@link Metadata}. */ public void extract(@NotNull final SequentialReader reader, @NotNull final Metadata metadata) { HuffmanTablesDirectory directory = metadata.getFirstDirectoryOfType(HuffmanTablesDirectory.class); if (directory == null) { directory = new HuffmanTablesDirectory(); metadata.addDirectory(directory); } try { while (reader.available() > 0) { byte header = reader.getByte(); HuffmanTableClass tableClass = HuffmanTableClass.typeOf((header & 0xF0) >> 4); int tableDestinationId = header & 0xF; byte[] lBytes = getBytes(reader, 16); int vCount = 0; for (byte b : lBytes) { vCount += (b & 0xFF); } byte[] vBytes = getBytes(reader, vCount); directory.getTables().add(new HuffmanTable(tableClass, tableDestinationId, lBytes, vBytes)); } } catch (IOException me) { directory.addError(me.getMessage()); } directory.setInt(HuffmanTablesDirectory.TAG_NUMBER_OF_TABLES, directory.getTables().size()); }
Example 9
Source File: JpegReader.java From metadata-extractor with Apache License 2.0 | 5 votes |
public void extract(byte[] segmentBytes, Metadata metadata, JpegSegmentType segmentType) { JpegDirectory directory = new JpegDirectory(); metadata.addDirectory(directory); // The value of TAG_COMPRESSION_TYPE is determined by the segment type found directory.setInt(JpegDirectory.TAG_COMPRESSION_TYPE, segmentType.byteValue - JpegSegmentType.SOF0.byteValue); SequentialReader reader = new SequentialByteArrayReader(segmentBytes); try { directory.setInt(JpegDirectory.TAG_DATA_PRECISION, reader.getUInt8()); directory.setInt(JpegDirectory.TAG_IMAGE_HEIGHT, reader.getUInt16()); directory.setInt(JpegDirectory.TAG_IMAGE_WIDTH, reader.getUInt16()); short componentCount = reader.getUInt8(); directory.setInt(JpegDirectory.TAG_NUMBER_OF_COMPONENTS, componentCount); // for each component, there are three bytes of data: // 1 - Component ID: 1 = Y, 2 = Cb, 3 = Cr, 4 = I, 5 = Q // 2 - Sampling factors: bit 0-3 vertical, 4-7 horizontal // 3 - Quantization table number for (int i = 0; i < (int)componentCount; i++) { final int componentId = reader.getUInt8(); final int samplingFactorByte = reader.getUInt8(); final int quantizationTableNumber = reader.getUInt8(); final JpegComponent component = new JpegComponent(componentId, samplingFactorByte, quantizationTableNumber); directory.setObject(JpegDirectory.TAG_COMPONENT_DATA_1 + i, component); } } catch (IOException ex) { directory.addError(ex.getMessage()); } }
Example 10
Source File: BmpReader.java From metadata-extractor with Apache License 2.0 | 5 votes |
protected void addError(@NotNull String errorMessage, final @NotNull Metadata metadata) { ErrorDirectory directory = metadata.getFirstDirectoryOfType(ErrorDirectory.class); if (directory == null) { metadata.addDirectory(new ErrorDirectory(errorMessage)); } else { directory.addError(errorMessage); } }
Example 11
Source File: PhotoshopReader.java From metadata-extractor with Apache License 2.0 | 4 votes |
public void extract(@NotNull final SequentialReader reader, int length, @NotNull final Metadata metadata, @Nullable final Directory parentDirectory) { PhotoshopDirectory directory = new PhotoshopDirectory(); metadata.addDirectory(directory); if (parentDirectory != null) directory.setParent(parentDirectory); // Data contains a sequence of Image Resource Blocks (IRBs): // // 4 bytes - Signature; mostly "8BIM" but "PHUT", "AgHg" and "DCSR" are also found // 2 bytes - Resource identifier // String - Pascal string, padded to make length even // 4 bytes - Size of resource data which follows // Data - The resource data, padded to make size even // // http://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577409_pgfId-1037504 int pos = 0; int clippingPathCount = 0; while (pos < length) { try { // 4 bytes for the signature ("8BIM", "PHUT", etc.) String signature = reader.getString(4); pos += 4; // 2 bytes for the resource identifier (tag type). int tagType = reader.getUInt16(); // segment type pos += 2; // A variable number of bytes holding a pascal string (two leading bytes for length). short descriptionLength = reader.getUInt8(); pos += 1; // Some basic bounds checking if (descriptionLength < 0 || descriptionLength + pos > length) throw new ImageProcessingException("Invalid string length"); // Get name (important for paths) StringBuilder description = new StringBuilder(); descriptionLength += pos; // Loop through each byte and append to string while (pos < descriptionLength) { description.append((char)reader.getUInt8()); pos ++; } // The number of bytes is padded with a trailing zero, if needed, to make the size even. if (pos % 2 != 0) { reader.skip(1); pos++; } // 4 bytes for the size of the resource data that follows. int byteCount = reader.getInt32(); pos += 4; // The resource data. byte[] tagBytes = reader.getBytes(byteCount); pos += byteCount; // The number of bytes is padded with a trailing zero, if needed, to make the size even. if (pos % 2 != 0) { reader.skip(1); pos++; } if (signature.equals("8BIM")) { if (tagType == PhotoshopDirectory.TAG_IPTC) new IptcReader().extract(new SequentialByteArrayReader(tagBytes), metadata, tagBytes.length, directory); else if (tagType == PhotoshopDirectory.TAG_ICC_PROFILE_BYTES) new IccReader().extract(new ByteArrayReader(tagBytes), metadata, directory); else if (tagType == PhotoshopDirectory.TAG_EXIF_DATA_1 || tagType == PhotoshopDirectory.TAG_EXIF_DATA_3) new ExifReader().extract(new ByteArrayReader(tagBytes), metadata, 0, directory); else if (tagType == PhotoshopDirectory.TAG_XMP_DATA) new XmpReader().extract(tagBytes, metadata, directory); else if (tagType >= 0x07D0 && tagType <= 0x0BB6) { clippingPathCount++; tagBytes = Arrays.copyOf(tagBytes, tagBytes.length + description.length() + 1); // Append description(name) to end of byte array with 1 byte before the description representing the length for (int i = tagBytes.length - description.length() - 1; i < tagBytes.length; i++) { if (i % (tagBytes.length - description.length() - 1 + description.length()) == 0) tagBytes[i] = (byte)description.length(); else tagBytes[i] = (byte)description.charAt(i - (tagBytes.length - description.length() - 1)); } PhotoshopDirectory._tagNameMap.put(0x07CF + clippingPathCount, "Path Info " + clippingPathCount); directory.setByteArray(0x07CF + clippingPathCount, tagBytes); } else directory.setByteArray(tagType, tagBytes); if (tagType >= 0x0fa0 && tagType <= 0x1387) PhotoshopDirectory._tagNameMap.put(tagType, String.format("Plug-in %d Data", tagType - 0x0fa0 + 1)); } } catch (Exception ex) { directory.addError(ex.getMessage()); return; } } }
Example 12
Source File: PcxReader.java From metadata-extractor with Apache License 2.0 | 4 votes |
public void extract(@NotNull final SequentialReader reader, @NotNull final Metadata metadata) { reader.setMotorolaByteOrder(false); PcxDirectory directory = new PcxDirectory(); metadata.addDirectory(directory); try { byte identifier = reader.getInt8(); if (identifier != 0x0A) throw new ImageProcessingException("Invalid PCX identifier byte"); directory.setInt(PcxDirectory.TAG_VERSION, reader.getInt8()); byte encoding = reader.getInt8(); if (encoding != 0x01) throw new ImageProcessingException("Invalid PCX encoding byte"); directory.setInt(PcxDirectory.TAG_BITS_PER_PIXEL, reader.getUInt8()); directory.setInt(PcxDirectory.TAG_XMIN, reader.getUInt16()); directory.setInt(PcxDirectory.TAG_YMIN, reader.getUInt16()); directory.setInt(PcxDirectory.TAG_XMAX, reader.getUInt16()); directory.setInt(PcxDirectory.TAG_YMAX, reader.getUInt16()); directory.setInt(PcxDirectory.TAG_HORIZONTAL_DPI, reader.getUInt16()); directory.setInt(PcxDirectory.TAG_VERTICAL_DPI, reader.getUInt16()); directory.setByteArray(PcxDirectory.TAG_PALETTE, reader.getBytes(48)); reader.skip(1); directory.setInt(PcxDirectory.TAG_COLOR_PLANES, reader.getUInt8()); directory.setInt(PcxDirectory.TAG_BYTES_PER_LINE, reader.getUInt16()); int paletteType = reader.getUInt16(); if (paletteType != 0) directory.setInt(PcxDirectory.TAG_PALETTE_TYPE, paletteType); int hScrSize = reader.getUInt16(); if (hScrSize != 0) directory.setInt(PcxDirectory.TAG_HSCR_SIZE, hScrSize); int vScrSize = reader.getUInt16(); if (vScrSize != 0) directory.setInt(PcxDirectory.TAG_VSCR_SIZE, vScrSize); } catch (Exception ex) { directory.addError("Exception reading PCX file metadata: " + ex.getMessage()); } }
Example 13
Source File: EpsReader.java From metadata-extractor with Apache License 2.0 | 4 votes |
/** * Filter method that determines if file will contain an EPS Header. If it does, it will read the necessary * data and then set the position to the beginning of the PostScript data. If it does not, the position will not * be changed. After both scenarios, the main extract method is called. * * @param inputStream InputStream containing file * @param metadata Metadata to add directory to and extracted data */ public void extract(@NotNull final InputStream inputStream, @NotNull final Metadata metadata) throws IOException { RandomAccessStreamReader reader = new RandomAccessStreamReader(inputStream); EpsDirectory directory = new EpsDirectory(); metadata.addDirectory(directory); /* * 0xC5D0D3C6 signifies an EPS Header block which contains 32-bytes of basic information * * 0x25215053 (%!PS) signifies an EPS File and leads straight into the PostScript */ switch (reader.getInt32(0)) { case 0xC5D0D3C6: reader.setMotorolaByteOrder(false); int postScriptOffset = reader.getInt32(4); int postScriptLength = reader.getInt32(8); int wmfOffset = reader.getInt32(12); int wmfSize = reader.getInt32(16); int tifOffset = reader.getInt32(20); int tifSize = reader.getInt32(24); //int checkSum = reader.getInt32(28); // Get Tiff/WMF preview data if applicable if (tifSize != 0) { directory.setInt(EpsDirectory.TAG_TIFF_PREVIEW_SIZE, tifSize); directory.setInt(EpsDirectory.TAG_TIFF_PREVIEW_OFFSET, tifOffset); // Get Tiff metadata try { ByteArrayReader byteArrayReader = new ByteArrayReader(reader.getBytes(tifOffset, tifSize)); new TiffReader().processTiff(byteArrayReader, new PhotoshopTiffHandler(metadata, null), 0); } catch (TiffProcessingException ex) { directory.addError("Unable to process TIFF data: " + ex.getMessage()); } } else if (wmfSize != 0) { directory.setInt(EpsDirectory.TAG_WMF_PREVIEW_SIZE, wmfSize); directory.setInt(EpsDirectory.TAG_WMF_PREVIEW_OFFSET, wmfOffset); } // TODO avoid allocating byte array here -- read directly from InputStream extract(directory, metadata, new SequentialByteArrayReader(reader.getBytes(postScriptOffset, postScriptLength))); break; case 0x25215053: inputStream.reset(); extract(directory, metadata, new StreamReader(inputStream)); break; default: directory.addError("File type not supported."); break; } }
Example 14
Source File: WavRiffHandler.java From metadata-extractor with Apache License 2.0 | 4 votes |
public WavRiffHandler(@NotNull Metadata metadata) { _directory = new WavDirectory(); metadata.addDirectory(_directory); }
Example 15
Source File: AviRiffHandler.java From metadata-extractor with Apache License 2.0 | 4 votes |
public AviRiffHandler(@NotNull Metadata metadata) { _directory = new AviDirectory(); metadata.addDirectory(_directory); }
Example 16
Source File: DuckyReader.java From metadata-extractor with Apache License 2.0 | 4 votes |
public void extract(@NotNull final SequentialReader reader, @NotNull final Metadata metadata) { DuckyDirectory directory = new DuckyDirectory(); metadata.addDirectory(directory); try { while (true) { int tag = reader.getUInt16(); // End of Segment is marked with zero if (tag == 0) break; int length = reader.getUInt16(); switch (tag) { case DuckyDirectory.TAG_QUALITY: { if (length != 4) { directory.addError("Unexpected length for the quality tag"); return; } directory.setInt(tag, reader.getInt32()); break; } case DuckyDirectory.TAG_COMMENT: case DuckyDirectory.TAG_COPYRIGHT: { reader.skip(4); directory.setStringValue(tag, reader.getStringValue(length - 4, Charsets.UTF_16BE)); break; } default: { // Unexpected tag directory.setByteArray(tag, reader.getBytes(length)); break; } } } } catch (IOException e) { directory.addError(e.getMessage()); } }
Example 17
Source File: IccReader.java From metadata-extractor with Apache License 2.0 | 4 votes |
public void extract(@NotNull final RandomAccessReader reader, @NotNull final Metadata metadata, @Nullable Directory parentDirectory) { // TODO review whether the 'tagPtr' values below really do require RandomAccessReader or whether SequentialReader may be used instead IccDirectory directory = new IccDirectory(); if (parentDirectory != null) directory.setParent(parentDirectory); try { int profileByteCount = reader.getInt32(IccDirectory.TAG_PROFILE_BYTE_COUNT); directory.setInt(IccDirectory.TAG_PROFILE_BYTE_COUNT, profileByteCount); // For these tags, the int value of the tag is in fact it's offset within the buffer. set4ByteString(directory, IccDirectory.TAG_CMM_TYPE, reader); setInt32(directory, IccDirectory.TAG_PROFILE_VERSION, reader); set4ByteString(directory, IccDirectory.TAG_PROFILE_CLASS, reader); set4ByteString(directory, IccDirectory.TAG_COLOR_SPACE, reader); set4ByteString(directory, IccDirectory.TAG_PROFILE_CONNECTION_SPACE, reader); setDate(directory, IccDirectory.TAG_PROFILE_DATETIME, reader); set4ByteString(directory, IccDirectory.TAG_SIGNATURE, reader); set4ByteString(directory, IccDirectory.TAG_PLATFORM, reader); setInt32(directory, IccDirectory.TAG_CMM_FLAGS, reader); set4ByteString(directory, IccDirectory.TAG_DEVICE_MAKE, reader); int temp = reader.getInt32(IccDirectory.TAG_DEVICE_MODEL); if (temp != 0) { if (temp <= 0x20202020) { directory.setInt(IccDirectory.TAG_DEVICE_MODEL, temp); } else { directory.setString(IccDirectory.TAG_DEVICE_MODEL, getStringFromInt32(temp)); } } setInt32(directory, IccDirectory.TAG_RENDERING_INTENT, reader); setInt64(directory, IccDirectory.TAG_DEVICE_ATTR, reader); float[] xyz = new float[]{ reader.getS15Fixed16(IccDirectory.TAG_XYZ_VALUES), reader.getS15Fixed16(IccDirectory.TAG_XYZ_VALUES + 4), reader.getS15Fixed16(IccDirectory.TAG_XYZ_VALUES + 8) }; directory.setObject(IccDirectory.TAG_XYZ_VALUES, xyz); // Process 'ICC tags' int tagCount = reader.getInt32(IccDirectory.TAG_TAG_COUNT); directory.setInt(IccDirectory.TAG_TAG_COUNT, tagCount); for (int i = 0; i < tagCount; i++) { int pos = IccDirectory.TAG_TAG_COUNT + 4 + i * 12; int tagType = reader.getInt32(pos); int tagPtr = reader.getInt32(pos + 4); int tagLen = reader.getInt32(pos + 8); byte[] b = reader.getBytes(tagPtr, tagLen); directory.setByteArray(tagType, b); } } catch (IOException ex) { directory.addError("Exception reading ICC profile: " + ex.getMessage()); } metadata.addDirectory(directory); }
Example 18
Source File: QuickTimeHandler.java From metadata-extractor with Apache License 2.0 | 4 votes |
public QuickTimeHandler(@NotNull Metadata metadata) { this.metadata = metadata; this.directory = getDirectory(); metadata.addDirectory(directory); }
Example 19
Source File: Mp4Handler.java From metadata-extractor with Apache License 2.0 | 4 votes |
public Mp4Handler(@NotNull Metadata metadata) { this.metadata = metadata; this.directory = getDirectory(); metadata.addDirectory(directory); }
Example 20
Source File: HeifHandler.java From metadata-extractor with Apache License 2.0 | 4 votes |
public HeifHandler(Metadata metadata) { this.metadata = metadata; this.directory = getDirectory(); metadata.addDirectory(directory); }