Search in sources :

Example 31 with ImageWriteParam

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

the class BMPImageWriter method writeEmbedded.

private void writeEmbedded(IIOImage image, ImageWriteParam bmpParam) throws IOException {
    String format = compressionType == BI_JPEG ? "jpeg" : "png";
    Iterator iterator = ImageIO.getImageWritersByFormatName(format);
    ImageWriter writer = null;
    if (iterator.hasNext())
        writer = (ImageWriter) iterator.next();
    if (writer != null) {
        if (embedded_stream == null) {
            throw new RuntimeException("No stream for writing embedded image!");
        }
        writer.addIIOWriteProgressListener(new IIOWriteProgressAdapter() {

            public void imageProgress(ImageWriter source, float percentageDone) {
                processImageProgress(percentageDone);
            }
        });
        writer.addIIOWriteWarningListener(new IIOWriteWarningListener() {

            public void warningOccurred(ImageWriter source, int imageIndex, String warning) {
                processWarningOccurred(imageIndex, warning);
            }
        });
        writer.setOutput(ImageIO.createImageOutputStream(embedded_stream));
        ImageWriteParam param = writer.getDefaultWriteParam();
        //param.setDestinationBands(bmpParam.getDestinationBands());
        param.setDestinationOffset(bmpParam.getDestinationOffset());
        param.setSourceBands(bmpParam.getSourceBands());
        param.setSourceRegion(bmpParam.getSourceRegion());
        param.setSourceSubsampling(bmpParam.getSourceXSubsampling(), bmpParam.getSourceYSubsampling(), bmpParam.getSubsamplingXOffset(), bmpParam.getSubsamplingYOffset());
        writer.write(null, image, param);
    } else
        throw new RuntimeException(I18N.getString("BMPImageWrite5") + " " + format);
}
Also used : Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) IIOWriteWarningListener(javax.imageio.event.IIOWriteWarningListener) ImageWriteParam(javax.imageio.ImageWriteParam) BMPImageWriteParam(javax.imageio.plugins.bmp.BMPImageWriteParam)

Example 32 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 33 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 34 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 35 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)

Aggregations

ImageWriteParam (javax.imageio.ImageWriteParam)48 IIOImage (javax.imageio.IIOImage)32 ImageWriter (javax.imageio.ImageWriter)32 BufferedImage (java.awt.image.BufferedImage)27 ImageOutputStream (javax.imageio.stream.ImageOutputStream)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 IIOMetadata (javax.imageio.metadata.IIOMetadata)14 File (java.io.File)13 IOException (java.io.IOException)13 ByteArrayInputStream (java.io.ByteArrayInputStream)10 Iterator (java.util.Iterator)10 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)10 Rectangle (java.awt.Rectangle)6 ImageReader (javax.imageio.ImageReader)5 ImageInputStream (javax.imageio.stream.ImageInputStream)5 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)5 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)4 Graphics2D (java.awt.Graphics2D)3 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)3 Color (java.awt.Color)2