Search in sources :

Example 1 with TIFFDirectory

use of com.github.jaiimageio.plugins.tiff.TIFFDirectory in project tess4j by nguyenq.

the class ImageIOHelper method setDPIViaAPI.

/**
 * Set DPI using API.
 *
 * @param imageMetadata original IIOMetadata
 * @param dpiX horizontal resolution
 * @param dpiY vertical resolution
 * @return modified IIOMetadata
 * @throws IIOInvalidTreeException
 */
private static IIOMetadata setDPIViaAPI(IIOMetadata imageMetadata, int dpiX, int dpiY) throws IIOInvalidTreeException {
    // Derive the TIFFDirectory from the metadata.
    TIFFDirectory dir = TIFFDirectory.createFromMetadata(imageMetadata);
    // Get {X,Y}Resolution tags.
    BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
    TIFFTag tagXRes = base.getTag(BaselineTIFFTagSet.TAG_X_RESOLUTION);
    TIFFTag tagYRes = base.getTag(BaselineTIFFTagSet.TAG_Y_RESOLUTION);
    // Create {X,Y}Resolution fields.
    TIFFField fieldXRes = new TIFFField(tagXRes, TIFFTag.TIFF_RATIONAL, 1, new long[][] { { dpiX, 1 } });
    TIFFField fieldYRes = new TIFFField(tagYRes, TIFFTag.TIFF_RATIONAL, 1, new long[][] { { dpiY, 1 } });
    // Append {X,Y}Resolution fields to directory.
    dir.addTIFFField(fieldXRes);
    dir.addTIFFField(fieldYRes);
    // Convert to metadata object.
    IIOMetadata metadata = dir.getAsMetadata();
    // Add other metadata.
    IIOMetadataNode root = new IIOMetadataNode("javax_imageio_1.0");
    IIOMetadataNode horiz = new IIOMetadataNode("HorizontalPixelSize");
    horiz.setAttribute("value", Double.toString(25.4f / dpiX));
    IIOMetadataNode vert = new IIOMetadataNode("VerticalPixelSize");
    vert.setAttribute("value", Double.toString(25.4f / dpiY));
    IIOMetadataNode dim = new IIOMetadataNode("Dimension");
    dim.appendChild(horiz);
    dim.appendChild(vert);
    root.appendChild(dim);
    metadata.mergeTree("javax_imageio_1.0", root);
    return metadata;
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) BaselineTIFFTagSet(com.github.jaiimageio.plugins.tiff.BaselineTIFFTagSet) TIFFField(com.github.jaiimageio.plugins.tiff.TIFFField) TIFFTag(com.github.jaiimageio.plugins.tiff.TIFFTag) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) TIFFDirectory(com.github.jaiimageio.plugins.tiff.TIFFDirectory)

Aggregations

BaselineTIFFTagSet (com.github.jaiimageio.plugins.tiff.BaselineTIFFTagSet)1 TIFFDirectory (com.github.jaiimageio.plugins.tiff.TIFFDirectory)1 TIFFField (com.github.jaiimageio.plugins.tiff.TIFFField)1 TIFFTag (com.github.jaiimageio.plugins.tiff.TIFFTag)1 IIOMetadata (javax.imageio.metadata.IIOMetadata)1 IIOMetadataNode (javax.imageio.metadata.IIOMetadataNode)1