Search in sources :

Example 66 with ImageWriteParam

use of javax.imageio.ImageWriteParam in project jmonkeyengine by jMonkeyEngine.

the class JmeDesktopSystem method writeImageFile.

@Override
public void writeImageFile(OutputStream outStream, String format, ByteBuffer imageData, int width, int height) throws IOException {
    BufferedImage awtImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
    Screenshots.convertScreenShot2(imageData.asIntBuffer(), awtImage);
    ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next();
    ImageWriteParam writeParam = writer.getDefaultWriteParam();
    if (format.equals("jpg")) {
        JPEGImageWriteParam jpegParam = (JPEGImageWriteParam) writeParam;
        jpegParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        jpegParam.setCompressionQuality(0.95f);
    }
    awtImage = verticalFlip(awtImage);
    ImageOutputStream imgOut = new MemoryCacheImageOutputStream(outStream);
    writer.setOutput(imgOut);
    IIOImage outputImage = new IIOImage(awtImage, null, null);
    try {
        writer.write(null, outputImage, writeParam);
    } finally {
        imgOut.close();
        writer.dispose();
    }
}
Also used : MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) ImageWriter(javax.imageio.ImageWriter) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) ImageWriteParam(javax.imageio.ImageWriteParam) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 67 with ImageWriteParam

use of javax.imageio.ImageWriteParam in project jmonkeyengine by jMonkeyEngine.

the class MjpegFileWriter method writeImageToBytes.

public byte[] writeImageToBytes(Image image, float quality) throws Exception {
    BufferedImage bi;
    if (image instanceof BufferedImage && ((BufferedImage) image).getType() == BufferedImage.TYPE_INT_RGB) {
        bi = (BufferedImage) image;
    } else {
        bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.drawImage(image, 0, 0, width, height, null);
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageWriter imgWrtr = ImageIO.getImageWritersByFormatName("jpg").next();
    ImageOutputStream imgOutStrm = ImageIO.createImageOutputStream(baos);
    imgWrtr.setOutput(imgOutStrm);
    ImageWriteParam jpgWrtPrm = imgWrtr.getDefaultWriteParam();
    jpgWrtPrm.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    jpgWrtPrm.setCompressionQuality(quality);
    imgWrtr.write(null, new IIOImage(bi, null, null), jpgWrtPrm);
    imgOutStrm.close();
    return baos.toByteArray();
}
Also used : ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream) Graphics2D(java.awt.Graphics2D) IIOImage(javax.imageio.IIOImage)

Example 68 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 69 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 70 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)

Aggregations

ImageWriteParam (javax.imageio.ImageWriteParam)120 ImageWriter (javax.imageio.ImageWriter)100 IIOImage (javax.imageio.IIOImage)74 ImageOutputStream (javax.imageio.stream.ImageOutputStream)53 BufferedImage (java.awt.image.BufferedImage)51 File (java.io.File)51 IOException (java.io.IOException)38 Test (org.junit.Test)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)28 ImageReadParam (javax.imageio.ImageReadParam)24 ParameterBlockJAI (javax.media.jai.ParameterBlockJAI)23 RenderedOp (javax.media.jai.RenderedOp)23 IIOMetadata (javax.imageio.metadata.IIOMetadata)22 FileImageOutputStreamExtImpl (it.geosolutions.imageio.stream.output.FileImageOutputStreamExtImpl)20 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)18 ByteArrayInputStream (java.io.ByteArrayInputStream)17 Graphics2D (java.awt.Graphics2D)12 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)12 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)12 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)12