Search in sources :

Example 96 with IIOImage

use of javax.imageio.IIOImage in project basicv2 by EgonOlsen71.

the class GraphicsDevice method save.

/**
 * Saves the current image into a PNG file.
 *
 * @param machine
 *            the machine
 * @param name
 *            the name of the target file
 */
public void save(Machine machine, String name) {
    try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(name));
        ImageOutputStream ios = ImageIO.createImageOutputStream(bos)) {
        Iterator<ImageWriter> itty = ImageIO.getImageWritersBySuffix("png");
        if (itty.hasNext()) {
            ImageWriter iw = (ImageWriter) itty.next();
            ImageWriteParam iwp = iw.getDefaultWriteParam();
            iw.setOutput(ios);
            iw.write(null, new IIOImage((RenderedImage) getScreen(), null, null), iwp);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) RenderedImage(java.awt.image.RenderedImage) BufferedOutputStream(java.io.BufferedOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 97 with IIOImage

use of javax.imageio.IIOImage in project chordatlas by twak.

the class Imagez method writeJPG.

public static void writeJPG(BufferedImage rendered, float quality, File f) {
    try {
        JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null);
        jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        jpegParams.setCompressionQuality(1f);
        ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
        ImageWriteParam param = writer.getDefaultWriteParam();
        // Needed see javadoc
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        // Highest quality
        param.setCompressionQuality(quality);
        writer.setOutput(new FileImageOutputStream(f));
        writer.write(null, new IIOImage(rendered, null, null), jpegParams);
    } catch (Throwable th) {
        th.printStackTrace();
    }
}
Also used : FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) IIOImage(javax.imageio.IIOImage)

Example 98 with IIOImage

use of javax.imageio.IIOImage in project OpenOLAT by OpenOLAT.

the class ImageHelperImpl method writeTo.

/**
 * Can change this to choose a better compression level as the default
 * @param image
 * @param scaledImage
 * @return
 */
private static boolean writeTo(BufferedImage image, OutputStream scaledImage, Size scaledSize, String outputFormat) {
    try {
        if (!StringHelper.containsNonWhitespace(outputFormat)) {
            outputFormat = OUTPUT_FORMAT;
        }
        Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(outputFormat);
        if (writers.hasNext()) {
            ImageWriter writer = writers.next();
            ImageWriteParam iwp = getOptimizedImageWriteParam(writer, scaledSize);
            IIOImage iiOImage = new IIOImage(image, null, null);
            ImageOutputStream iOut = new MemoryCacheImageOutputStream(scaledImage);
            writer.setOutput(iOut);
            writer.write(null, iiOImage, iwp);
            writer.dispose();
            iOut.flush();
            iOut.close();
            return true;
        } else {
            return ImageIO.write(image, outputFormat, scaledImage);
        }
    } catch (IOException e) {
        return false;
    }
}
Also used : MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 99 with IIOImage

use of javax.imageio.IIOImage in project xwiki-platform by xwiki.

the class DefaultImageProcessor method writeImage.

@Override
public void writeImage(RenderedImage image, String mimeType, float quality, OutputStream out) throws IOException {
    if ("image/jpeg".equals(mimeType)) {
        // Find a JPEG writer.
        ImageWriter writer = null;
        Iterator<ImageWriter> iter = ImageIO.getImageWritersByMIMEType(mimeType);
        if (iter.hasNext()) {
            writer = iter.next();
        }
        JPEGImageWriteParam iwp = new JPEGImageWriteParam(null);
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(quality);
        // Prepare output file.
        ImageOutputStream ios = ImageIO.createImageOutputStream(out);
        writer.setOutput(ios);
        // Write the image.
        writer.write(null, new IIOImage(image, null, null), iwp);
        // Cleanup.
        ios.flush();
        writer.dispose();
        ios.close();
    } else {
        ImageIO.write(image, "png", out);
    }
}
Also used : ImageWriter(javax.imageio.ImageWriter) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 100 with IIOImage

use of javax.imageio.IIOImage in project polymap4-core by Polymap4.

the class ImageEncodeProcessor method imageioEncodeJPEG.

private void imageioEncodeJPEG(Image image, ChunkedResponseOutputStream out) throws IOException {
    // this code is from http://forums.sun.com/thread.jspa?threadID=5197061
    ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
    ImageWriteParam param = writer.getDefaultWriteParam();
    param.setSourceBands(new int[] { 0, 1, 2 });
    ColorModel cm = new DirectColorModel(24, // Red
    0x00ff0000, // Green
    0x0000ff00, // Blue
    0x000000ff, // Alpha
    0x0);
    param.setDestinationType(new ImageTypeSpecifier(cm, cm.createCompatibleSampleModel(1, 1)));
    ImageOutputStream imageOut = ImageIO.createImageOutputStream(out);
    writer.setOutput(imageOut);
    writer.write(null, new IIOImage((RenderedImage) image, null, null), param);
    writer.dispose();
    imageOut.close();
}
Also used : DirectColorModel(java.awt.image.DirectColorModel) ColorModel(java.awt.image.ColorModel) ImageWriter(javax.imageio.ImageWriter) DirectColorModel(java.awt.image.DirectColorModel) ImageWriteParam(javax.imageio.ImageWriteParam) RenderedImage(java.awt.image.RenderedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Aggregations

IIOImage (javax.imageio.IIOImage)123 ImageWriter (javax.imageio.ImageWriter)92 ImageWriteParam (javax.imageio.ImageWriteParam)79 ImageOutputStream (javax.imageio.stream.ImageOutputStream)79 BufferedImage (java.awt.image.BufferedImage)66 File (java.io.File)45 IOException (java.io.IOException)41 ByteArrayOutputStream (java.io.ByteArrayOutputStream)37 IIOMetadata (javax.imageio.metadata.IIOMetadata)32 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)25 ByteArrayInputStream (java.io.ByteArrayInputStream)22 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)17 ImageReader (javax.imageio.ImageReader)16 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)16 Graphics2D (java.awt.Graphics2D)14 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)14 Test (org.junit.Test)14 FileOutputStream (java.io.FileOutputStream)13 Iterator (java.util.Iterator)11 RenderedImage (java.awt.image.RenderedImage)10