Example usage for javax.imageio.metadata IIOMetadata isReadOnly

List of usage examples for javax.imageio.metadata IIOMetadata isReadOnly

Introduction

In this page you can find the example usage for javax.imageio.metadata IIOMetadata isReadOnly.

Prototype

public abstract boolean isReadOnly();

Source Link

Document

Returns true if this object does not support the mergeTree , setFromTree , and reset methods.

Usage

From source file:com.ackpdfbox.app.imageio.ImageIOUtil.java

/**
 * Writes a buffered image to a file using the given image format.
 * Compression is fixed for PNG, GIF, BMP and WBMP, dependent of the quality
 * parameter for JPG, and dependent of bit count for TIFF (a bitonal image
 * will be compressed with CCITT G4, a color image with LZW). Creating a
 * TIFF image is only supported if the jai_imageio library is in the class
 * path./*from  w  w  w. ja  v a 2s  . c o m*/
 *
 * @param image the image to be written
 * @param formatName the target format (ex. "png")
 * @param output the output stream to be used for writing
 * @param dpi the resolution in dpi (dots per inch) to be used in metadata
 * @param quality quality to be used when compressing the image (0 <
 * quality < 1.0f)
 * @return true if the image file was produced, false if there was an error.
 * @throws IOException if an I/O error occurs
 */
public static boolean writeImage(BufferedImage image, String formatName, OutputStream output, int dpi,
        float quality) throws IOException {
    ImageOutputStream imageOutput = null;
    ImageWriter writer = null;
    try {
        // find suitable image writer
        Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(formatName);
        ImageWriteParam param = null;
        IIOMetadata metadata = null;
        // Loop until we get the best driver, i.e. one that supports
        // setting dpi in the standard metadata format; however we'd also 
        // accept a driver that can't, if a better one can't be found
        while (writers.hasNext()) {
            if (writer != null) {
                writer.dispose();
            }
            writer = writers.next();
            param = writer.getDefaultWriteParam();
            metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(image), param);
            if (metadata != null && !metadata.isReadOnly() && metadata.isStandardMetadataFormatSupported()) {
                break;
            }
        }
        if (writer == null) {
            LOG.error("No ImageWriter found for '" + formatName + "' format");
            StringBuilder sb = new StringBuilder();
            String[] writerFormatNames = ImageIO.getWriterFormatNames();
            for (String fmt : writerFormatNames) {
                sb.append(fmt);
                sb.append(' ');
            }
            LOG.error("Supported formats: " + sb);
            return false;
        }

        // compression
        if (param != null && param.canWriteCompressed()) {
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            if (formatName.toLowerCase().startsWith("tif")) {
                // TIFF compression
                TIFFUtil.setCompressionType(param, image);
            } else {
                param.setCompressionType(param.getCompressionTypes()[0]);
                param.setCompressionQuality(quality);
            }
        }

        if (formatName.toLowerCase().startsWith("tif")) {
            // TIFF metadata
            TIFFUtil.updateMetadata(metadata, image, dpi);
        } else if ("jpeg".equals(formatName.toLowerCase()) || "jpg".equals(formatName.toLowerCase())) {
            // This segment must be run before other meta operations,
            // or else "IIOInvalidTreeException: Invalid node: app0JFIF"
            // The other (general) "meta" methods may not be used, because
            // this will break the reading of the meta data in tests
            JPEGUtil.updateMetadata(metadata, dpi);
        } else {
            // write metadata is possible
            if (metadata != null && !metadata.isReadOnly() && metadata.isStandardMetadataFormatSupported()) {
                setDPI(metadata, dpi, formatName);
            }
        }

        // write
        imageOutput = ImageIO.createImageOutputStream(output);
        writer.setOutput(imageOutput);
        writer.write(null, new IIOImage(image, null, metadata), param);
    } finally {
        if (writer != null) {
            writer.dispose();
        }
        if (imageOutput != null) {
            imageOutput.close();
        }
    }
    return true;
}

From source file:org.forgerock.doc.maven.PNGUtils.java

private static void saveBufferedImage(final BufferedImage bufferedImage, final File outputFile,
        final int dotsPerInch) throws IOException {
    for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName("png"); iw.hasNext();) {
        ImageWriter writer = iw.next();
        ImageWriteParam writeParam = writer.getDefaultWriteParam();
        ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier
                .createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
        IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);
        if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) {
            continue;
        }// www.  jav a 2 s.  c om

        setDPI(metadata, dotsPerInch);

        final ImageOutputStream stream = ImageIO.createImageOutputStream(outputFile);
        try {
            writer.setOutput(stream);
            writer.write(metadata, new IIOImage(bufferedImage, null, metadata), writeParam);
        } finally {
            stream.close();
        }
        break;
    }
}