Java Code Examples for javax.imageio.ImageTypeSpecifier#createFromRenderedImage()

The following examples show how to use javax.imageio.ImageTypeSpecifier#createFromRenderedImage() . 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: CanEncodeShort.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
    for (int i = 0; i < types.length; i++) {
        BufferedImage img = new BufferedImage(32, 32, types[i]);

        ImageTypeSpecifier spec =
            ImageTypeSpecifier.createFromRenderedImage(img);

        Iterator writers = ImageIO.getImageWriters(spec, "png");

        if (!writers.hasNext()) {
            throw new RuntimeException("Test failed: " +
                                       "no PNG writer found for type " +
                                       typeNames[i]);
        }
    }
}
 
Example 2
Source File: CanEncodeShort.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
    for (int i = 0; i < types.length; i++) {
        BufferedImage img = new BufferedImage(32, 32, types[i]);

        ImageTypeSpecifier spec =
            ImageTypeSpecifier.createFromRenderedImage(img);

        Iterator writers = ImageIO.getImageWriters(spec, "png");

        if (!writers.hasNext()) {
            throw new RuntimeException("Test failed: " +
                                       "no PNG writer found for type " +
                                       typeNames[i]);
        }
    }
}
 
Example 3
Source File: CanEncodeIndexed.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    BufferedImage img = new BufferedImage(32, 32,
                                          BufferedImage.TYPE_BYTE_INDEXED);

    ImageTypeSpecifier spec =
        ImageTypeSpecifier.createFromRenderedImage(img);

    Iterator writers = ImageIO.getImageWriters(spec, "jpeg");

    if (!writers.hasNext()) {
        throw new RuntimeException("Test failed: " +
                                   "no JPEG writer found for " +
                                   "image with IndexColorModel");
    }
}
 
Example 4
Source File: CanEncodeIndexed.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    BufferedImage img = new BufferedImage(32, 32,
                                          BufferedImage.TYPE_BYTE_INDEXED);

    ImageTypeSpecifier spec =
        ImageTypeSpecifier.createFromRenderedImage(img);

    Iterator writers = ImageIO.getImageWriters(spec, "jpeg");

    if (!writers.hasNext()) {
        throw new RuntimeException("Test failed: " +
                                   "no JPEG writer found for " +
                                   "image with IndexColorModel");
    }
}
 
Example 5
Source File: ImageIOHelper.java    From tess4j with Apache License 2.0 5 votes vote down vote up
public static List<File> createTiffFiles(List<IIOImage> imageList, int index, int dpiX, int dpiY) throws IOException {
    List<File> tiffFiles = new ArrayList<File>();

    //Set up the writeParam
    TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
    tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED);

    //Get tiff 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);

    // all if index == -1; otherwise, only index-th
    for (IIOImage oimage : (index == -1 ? imageList : imageList.subList(index, index + 1))) {
        if (dpiX != 0 && dpiY != 0) {
            // Get the default image metadata.
            ImageTypeSpecifier imageType = ImageTypeSpecifier.createFromRenderedImage(oimage.getRenderedImage());
            ImageWriteParam param = writer.getDefaultWriteParam();
            IIOMetadata imageMetadata = writer.getDefaultImageMetadata(imageType, param);
            imageMetadata = setDPIViaAPI(imageMetadata, dpiX, dpiY);
            oimage.setMetadata(imageMetadata);
        }

        File tiffFile = File.createTempFile(OUTPUT_FILE_NAME, TIFF_EXT);
        try (ImageOutputStream ios = ImageIO.createImageOutputStream(tiffFile)) {
            writer.setOutput(ios);
            writer.write(streamMetadata, oimage, tiffWriteParam);
            tiffFiles.add(tiffFile);
        }
    }
    writer.dispose();

    return tiffFiles;
}
 
Example 6
Source File: LogicalScreenDimensionTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {
    String format = "GIF";
    ImageWriter writer =
            ImageIO.getImageWritersByFormatName(format).next();
    if (writer == null) {
        throw new RuntimeException("No available writers for " + format);
    }

    BufferedImage img = createTestImage(100, 100, BufferedImage.TYPE_BYTE_GRAY);

    ImageWriteParam p = writer.getDefaultWriteParam();
    ImageTypeSpecifier type =
        ImageTypeSpecifier.createFromRenderedImage(img);
    IIOMetadata inImageMetadata =
        writer.getDefaultImageMetadata(type, p);

    IIOMetadata inStreamMetadata = writer.getDefaultStreamMetadata(p);

    // write and read image
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    writer.setOutput(ios);

    writer.write(inStreamMetadata, new IIOImage(img, null, inImageMetadata), p);

    ios.flush();
    ios.close();

    // read result
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ImageInputStream iis = ImageIO.createImageInputStream(bais);
    ImageReader reader = ImageIO.getImageReader(writer);
    reader.setInput(iis);

    IIOMetadata outStreamMetadata = reader.getStreamMetadata();

    GIFStreamMetadata gifStreamMetadata = (GIFStreamMetadata)outStreamMetadata;

    if (gifStreamMetadata.logicalScreenWidth != img.getWidth() ||
            gifStreamMetadata.logicalScreenHeight != img.getHeight()) {
        throw new RuntimeException("Test failed due to wrong logical screen dimension.");
    }
}
 
Example 7
Source File: LogicalScreenDimensionTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {
    String format = "GIF";
    ImageWriter writer =
            ImageIO.getImageWritersByFormatName(format).next();
    if (writer == null) {
        throw new RuntimeException("No available writers for " + format);
    }

    BufferedImage img = createTestImage(100, 100, BufferedImage.TYPE_BYTE_GRAY);

    ImageWriteParam p = writer.getDefaultWriteParam();
    ImageTypeSpecifier type =
        ImageTypeSpecifier.createFromRenderedImage(img);
    IIOMetadata inImageMetadata =
        writer.getDefaultImageMetadata(type, p);

    IIOMetadata inStreamMetadata = writer.getDefaultStreamMetadata(p);

    // write and read image
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    writer.setOutput(ios);

    writer.write(inStreamMetadata, new IIOImage(img, null, inImageMetadata), p);

    ios.flush();
    ios.close();

    // read result
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ImageInputStream iis = ImageIO.createImageInputStream(bais);
    ImageReader reader = ImageIO.getImageReader(writer);
    reader.setInput(iis);

    IIOMetadata outStreamMetadata = reader.getStreamMetadata();

    GIFStreamMetadata gifStreamMetadata = (GIFStreamMetadata)outStreamMetadata;

    if (gifStreamMetadata.logicalScreenWidth != img.getWidth() ||
            gifStreamMetadata.logicalScreenHeight != img.getHeight()) {
        throw new RuntimeException("Test failed due to wrong logical screen dimension.");
    }
}
 
Example 8
Source File: ImageIOImageWriter.java    From victor with Apache License 2.0 4 votes vote down vote up
/**
 * @see ImageWriter#writeImage(RenderedImage, OutputStream, ImageWriterParams)
 */
public void writeImage(RenderedImage image, OutputStream out, 
        ImageWriterParams params) 
            throws IOException {
    Iterator iter;
    iter = ImageIO.getImageWritersByMIMEType(getMIMEType());
    javax.imageio.ImageWriter iiowriter = null;
    try {
        iiowriter = (javax.imageio.ImageWriter)iter.next();
        if (iiowriter != null) {
            iiowriter.addIIOWriteWarningListener(this);

            ImageOutputStream imgout = null;
            try {
                imgout = ImageIO.createImageOutputStream(out);
                ImageWriteParam iwParam = getDefaultWriteParam(iiowriter, image, params);

                ImageTypeSpecifier type;
                if (iwParam.getDestinationType() != null) {
                    type = iwParam.getDestinationType();
                } else {
                    type = ImageTypeSpecifier.createFromRenderedImage(image);
                }

                //Handle metadata
                IIOMetadata meta = iiowriter.getDefaultImageMetadata(
                        type, iwParam);
                //meta might be null for some JAI codecs as they don't support metadata
                if (params != null && meta != null) {
                    meta = updateMetadata(meta, params); 
                }

                //Write image
                iiowriter.setOutput(imgout);
                IIOImage iioimg = new IIOImage(image, null, meta);
                iiowriter.write(null, iioimg, iwParam);
            } finally {
                if (imgout != null) {
                    imgout.close();
                }
            }
        } else {
            throw new UnsupportedOperationException("No ImageIO codec for writing " 
                    + getMIMEType() + " is available!");
        }
    } finally {
        if (iiowriter != null) {
            iiowriter.dispose();
        }
    }
}
 
Example 9
Source File: ReadMalformedPngTest.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
private static byte[] createTestPng() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    BufferedImage img = createTestImage();

    try {
        ImageOutputStream ios = ImageIO.createImageOutputStream(baos);

        ImageWriter w = ImageIO.getImageWritersByFormatName("PNG").next();

        w.setOutput(ios);

        ImageWriteParam p = w.getDefaultWriteParam();

        ImageTypeSpecifier t = ImageTypeSpecifier.createFromRenderedImage(img);

        IIOMetadata m = w.getDefaultImageMetadata(t, p);

        String nativeMetadataFormat = m.getNativeMetadataFormatName();

        Node root = m.getAsTree(nativeMetadataFormat);

        IIOMetadataNode textEntry = new IIOMetadataNode("tEXtEntry");
        textEntry.setAttribute("keyword", "comment");
        textEntry.setAttribute("value", "This is a test image for JDK-6945174");

        IIOMetadataNode text = new IIOMetadataNode("tEXt");
        text.appendChild(textEntry);

        root.appendChild(text);

        m.mergeTree(nativeMetadataFormat, root);

        IIOImage iio_img = new IIOImage(img, null, m);

        w.write(iio_img);

        w.dispose();
        ios.flush();
        ios.close();
    } catch (IOException e) {
        throw new RuntimeException("Test failed.", e);
    }

    baos.flush();

    byte[] data = baos.toByteArray();

    adjustCommentLength(Integer.MAX_VALUE + 0x1000, data);

    return data;
}