Search in sources :

Example 71 with IIOMetadata

use of javax.imageio.metadata.IIOMetadata 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)

Example 72 with IIOMetadata

use of javax.imageio.metadata.IIOMetadata in project tess4j by nguyenq.

the class ImageIOHelper method getImageByteBuffer.

/**
 * Gets pixel data of an <code>RenderedImage</code> object.
 *
 * @param image an <code>RenderedImage</code> object
 * @return a byte buffer of pixel data
 * @throws IOException
 */
public static ByteBuffer getImageByteBuffer(RenderedImage image) throws IOException {
    // Set up the writeParam
    TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
    tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED);
    // Get tif writer and set output to file
    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(TIFF_FORMAT);
    if (!writers.hasNext()) {
        throw new RuntimeException(JAI_IMAGE_WRITER_MESSAGE);
    }
    ImageWriter writer = writers.next();
    // Get the stream metadata
    IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(tiffWriteParam);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream);
    writer.setOutput(ios);
    writer.write(streamMetadata, new IIOImage(image, null, null), tiffWriteParam);
    // writer.write(image);
    writer.dispose();
    // ImageIO.write(image, "tiff", ios); // this can be used in lieu of writer
    ios.seek(0);
    BufferedImage bi = ImageIO.read(ios);
    return convertImageData(bi);
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) TIFFImageWriteParam(com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Example 73 with IIOMetadata

use of javax.imageio.metadata.IIOMetadata in project tess4j by nguyenq.

the class ImageIOHelper method mergeTiff.

/**
 * Merges multiple images into one multi-page TIFF image.
 *
 * @param imageList a list of <code>IIOImage</code> objects
 * @param outputTiff the output TIFF file
 * @param compressionType valid values: LZW, CCITT T.6, PackBits
 * @throws IOException
 */
public static void mergeTiff(List<IIOImage> imageList, File outputTiff, String compressionType) throws IOException {
    if (imageList == null || imageList.isEmpty()) {
        // if no image
        return;
    }
    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(TIFF_FORMAT);
    if (!writers.hasNext()) {
        throw new RuntimeException(JAI_IMAGE_WRITER_MESSAGE);
    }
    ImageWriter writer = writers.next();
    // Set up the writeParam
    TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
    // tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED); // comment out to preserve original sizes
    if (compressionType != null) {
        tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        tiffWriteParam.setCompressionType(compressionType);
    }
    // Get the stream metadata
    IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(tiffWriteParam);
    ImageOutputStream ios = ImageIO.createImageOutputStream(outputTiff);
    writer.setOutput(ios);
    int dpiX = 300;
    int dpiY = 300;
    for (IIOImage iioImage : imageList) {
        // Get the default image metadata.
        ImageTypeSpecifier imageType = ImageTypeSpecifier.createFromRenderedImage(iioImage.getRenderedImage());
        IIOMetadata imageMetadata = writer.getDefaultImageMetadata(imageType, null);
        imageMetadata = setDPIViaAPI(imageMetadata, dpiX, dpiY);
        iioImage.setMetadata(imageMetadata);
    }
    IIOImage firstIioImage = imageList.remove(0);
    writer.write(streamMetadata, firstIioImage, tiffWriteParam);
    int i = 1;
    for (IIOImage iioImage : imageList) {
        writer.writeInsert(i++, iioImage, tiffWriteParam);
    }
    ios.close();
    writer.dispose();
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageWriter(javax.imageio.ImageWriter) TIFFImageWriteParam(com.github.jaiimageio.plugins.tiff.TIFFImageWriteParam) ImageOutputStream(javax.imageio.stream.ImageOutputStream) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOImage(javax.imageio.IIOImage)

Example 74 with IIOMetadata

use of javax.imageio.metadata.IIOMetadata in project CodenameOne by codenameone.

the class AnimationImpl method getPixelOffsets.

public static Point getPixelOffsets(ImageReader reader, int num) throws IOException {
    IIOMetadata meta = reader.getImageMetadata(num);
    Point point = new Point(-1, -1);
    Node root = meta.getAsTree("javax_imageio_1.0");
    // printNodeTree(root);
    for (Node c = root.getFirstChild(); c != null; c = c.getNextSibling()) {
        String name = c.getNodeName();
        if ("Dimension".equals(name)) {
            for (c = c.getFirstChild(); c != null; c = c.getNextSibling()) {
                name = c.getNodeName();
                if ("HorizontalPixelOffset".equals(name))
                    point.x = getValueAttribute(c);
                else if ("VerticalPixelOffset".equals(name))
                    point.y = getValueAttribute(c);
            }
            return point;
        }
    }
    return point;
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) Node(org.w3c.dom.Node) Point(java.awt.Point)

Example 75 with IIOMetadata

use of javax.imageio.metadata.IIOMetadata in project CodenameOne by codenameone.

the class TimelineEditor method getDisposalMethod.

static String getDisposalMethod(ImageReader reader, int num) throws IOException {
    IIOMetadata meta = reader.getImageMetadata(num);
    Node parent = meta.getAsTree("javax_imageio_gif_image_1.0");
    // printNodeTree(parent);
    NodeList root = parent.getChildNodes();
    for (int iter = 0; iter < root.getLength(); iter++) {
        Node n = root.item(iter);
        if (n.getNodeName().equals("GraphicControlExtension")) {
            return n.getAttributes().getNamedItem("disposalMethod").getNodeValue();
        }
    }
    return null;
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Point(java.awt.Point)

Aggregations

IIOMetadata (javax.imageio.metadata.IIOMetadata)81 BufferedImage (java.awt.image.BufferedImage)36 ImageWriter (javax.imageio.ImageWriter)35 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)32 IIOImage (javax.imageio.IIOImage)30 ImageOutputStream (javax.imageio.stream.ImageOutputStream)30 File (java.io.File)27 ImageWriteParam (javax.imageio.ImageWriteParam)22 Node (org.w3c.dom.Node)22 IOException (java.io.IOException)20 ImageReader (javax.imageio.ImageReader)19 IIOMetadataNode (javax.imageio.metadata.IIOMetadataNode)17 Rectangle (java.awt.Rectangle)13 NodeList (org.w3c.dom.NodeList)13 Point (java.awt.Point)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ImageInputStream (javax.imageio.stream.ImageInputStream)10 Test (org.junit.Test)9 ColorModel (java.awt.image.ColorModel)7