Search in sources :

Example 36 with ImageWriteParam

use of javax.imageio.ImageWriteParam in project screenbird by adamhub.

the class Frame method writeJPEG.

/** Writes a JPEG image to a given OutputStream
     * 
     * @param bi an image
     * @param quality the quality (between zero and one)
     * @return the amount of bytes written
     */
protected static long writeJPEG(OutputStream out, BufferedImage bi, float quality) throws IOException {
    MeasuredOutputStream mOut = new MeasuredOutputStream(out);
    MemoryCacheImageOutputStream iOut = null;
    iOut = new MemoryCacheImageOutputStream(mOut);
    ImageWriter iw = (ImageWriter) ImageIO.getImageWritersByMIMEType("image/jpeg").next();
    ImageWriteParam iwParam = iw.getDefaultWriteParam();
    iwParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    iwParam.setCompressionQuality(quality);
    iw.setOutput(iOut);
    IIOImage img = new IIOImage(bi, null, null);
    iw.write(null, img, iwParam);
    return mOut.getBytesWritten();
}
Also used : MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) MeasuredOutputStream(com.bric.io.MeasuredOutputStream) ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) IIOImage(javax.imageio.IIOImage)

Example 37 with ImageWriteParam

use of javax.imageio.ImageWriteParam in project spring-framework by spring-projects.

the class BufferedImageHttpMessageConverter method writeInternal.

private void writeInternal(BufferedImage image, MediaType contentType, OutputStream body) throws IOException, HttpMessageNotWritableException {
    ImageOutputStream imageOutputStream = null;
    ImageWriter imageWriter = null;
    try {
        Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByMIMEType(contentType.toString());
        if (imageWriters.hasNext()) {
            imageWriter = imageWriters.next();
            ImageWriteParam iwp = imageWriter.getDefaultWriteParam();
            process(iwp);
            imageOutputStream = createImageOutputStream(body);
            imageWriter.setOutput(imageOutputStream);
            imageWriter.write(null, new IIOImage(image, null, null), iwp);
        } else {
            throw new HttpMessageNotWritableException("Could not find javax.imageio.ImageWriter for Content-Type [" + contentType + "]");
        }
    } finally {
        if (imageWriter != null) {
            imageWriter.dispose();
        }
        if (imageOutputStream != null) {
            try {
                imageOutputStream.close();
            } catch (IOException ex) {
            // ignore
            }
        }
    }
}
Also used : ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) FileCacheImageOutputStream(javax.imageio.stream.FileCacheImageOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 38 with ImageWriteParam

use of javax.imageio.ImageWriteParam in project jdk8u_jdk by JetBrains.

the class LUTCompareTest method createTestImage.

/* Create test image with two frames:
     *  1) with {red, red} palette
     *  2) with {blue, red } palette
     */
private static Image createTestImage() throws IOException {
    BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 });
    BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 });
    ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next();
    ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif"));
    ImageWriteParam param = writer.getDefaultWriteParam();
    writer.setOutput(ios);
    writer.prepareWriteSequence(null);
    writer.writeToSequence(new IIOImage(frame1, null, null), param);
    writer.writeToSequence(new IIOImage(frame2, null, null), param);
    writer.endWriteSequence();
    writer.reset();
    writer.dispose();
    ios.flush();
    ios.close();
    return Toolkit.getDefaultToolkit().createImage("lut_test.gif");
}
Also used : ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 39 with ImageWriteParam

use of javax.imageio.ImageWriteParam in project jdk8u_jdk by JetBrains.

the class CompressionModeTest method doTest.

private static void doTest(int mode) {
    String fileFormat = "bmp";
    try {
        ImageWriter iw = (ImageWriter) ImageIO.getImageWritersBySuffix(fileFormat).next();
        if (iw == null) {
            throw new RuntimeException("No available image writer for " + fileFormat + " Test failed.");
        }
        File file = new File("image." + fileFormat);
        ImageOutputStream ios = ImageIO.createImageOutputStream(file);
        iw.setOutput(ios);
        BufferedImage bimg = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
        Graphics g = bimg.getGraphics();
        g.setColor(Color.green);
        g.fillRect(0, 0, 100, 100);
        ImageWriteParam param = iw.getDefaultWriteParam();
        param.setCompressionMode(mode);
        IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bimg), param);
        IIOImage iioImg = new IIOImage(bimg, null, meta);
        iw.write(null, iioImg, param);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Test failed.");
    }
}
Also used : Graphics(java.awt.Graphics) IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOImage(javax.imageio.IIOImage)

Example 40 with ImageWriteParam

use of javax.imageio.ImageWriteParam in project jdk8u_jdk by JetBrains.

the class AnimationTest method main.

public static void main(String[] args) {
    try {
        AnimationTest t = new AnimationTest();
        t.initFrame();
        ImageWriter w = t.initWriter();
        ImageWriteParam p = w.getDefaultWriteParam();
        IIOMetadata streamMetadata = w.getDefaultStreamMetadata(p);
        w.prepareWriteSequence(streamMetadata);
        for (int i = 0; i < 50; i++) {
            BufferedImage f = t.createNextFrame();
            ImageTypeSpecifier type = new ImageTypeSpecifier(f);
            IIOMetadata m = w.getDefaultImageMetadata(type, p);
            w.writeToSequence(new IIOImage(f, null, m), p);
        }
        w.endWriteSequence();
        t.checkAnimation();
    } catch (Exception e) {
        throw new RuntimeException("Test failed.", e);
    }
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IOException(java.io.IOException) IIOImage(javax.imageio.IIOImage)

Aggregations

ImageWriteParam (javax.imageio.ImageWriteParam)52 IIOImage (javax.imageio.IIOImage)35 ImageWriter (javax.imageio.ImageWriter)35 BufferedImage (java.awt.image.BufferedImage)28 ImageOutputStream (javax.imageio.stream.ImageOutputStream)26 ByteArrayOutputStream (java.io.ByteArrayOutputStream)16 IIOMetadata (javax.imageio.metadata.IIOMetadata)15 IOException (java.io.IOException)14 File (java.io.File)13 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 Iterator (java.util.Iterator)10 Rectangle (java.awt.Rectangle)6 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)6 ImageReader (javax.imageio.ImageReader)5 ImageInputStream (javax.imageio.stream.ImageInputStream)5 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)5 Graphics2D (java.awt.Graphics2D)3 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)3 Color (java.awt.Color)2