Search in sources :

Example 91 with ImageWriter

use of javax.imageio.ImageWriter 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 92 with ImageWriter

use of javax.imageio.ImageWriter 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 93 with ImageWriter

use of javax.imageio.ImageWriter 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 94 with ImageWriter

use of javax.imageio.ImageWriter 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 95 with ImageWriter

use of javax.imageio.ImageWriter in project adempiere by adempiere.

the class ScreenShot method createJPEG.

/**
	 * 	Create JPEG file from window
	 * 	@param window window
	 * 	@param fileName optional file name
	 * 	@return true if created
	 */
public static boolean createJPEG(Window window, String fileName) {
    BufferedImage bi = null;
    if (window == null || fileName == null)
        new IllegalArgumentException("ScreenShot.createJPEG Window os NULL");
    //	Get File
    File file = getJPGFile(window);
    if (file == null)
        return false;
    log.config("File=" + file);
    if (file.exists())
        file.delete();
    //	Get Writer
    Iterator writers = ImageIO.getImageWritersByFormatName("jpg");
    ImageWriter writer = (ImageWriter) writers.next();
    if (writer == null) {
        log.log(Level.SEVERE, "no ImageWriter");
        return false;
    }
    //	Get Image
    try {
        Thread.sleep(1000);
        bi = getImage(window);
    } catch (InterruptedException ex) {
        log.log(Level.SEVERE, "ex", ex);
    }
    //	Write Image
    try {
        ImageOutputStream ios = ImageIO.createImageOutputStream(file);
        writer.setOutput(ios);
        writer.write(bi);
        ios.flush();
        ios.close();
    } catch (IOException ex) {
        log.log(Level.SEVERE, "ex", ex);
        return false;
    }
    return true;
}
Also used : Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Aggregations

ImageWriter (javax.imageio.ImageWriter)171 ImageWriteParam (javax.imageio.ImageWriteParam)103 IIOImage (javax.imageio.IIOImage)92 ImageOutputStream (javax.imageio.stream.ImageOutputStream)85 BufferedImage (java.awt.image.BufferedImage)80 File (java.io.File)73 IOException (java.io.IOException)53 ByteArrayOutputStream (java.io.ByteArrayOutputStream)44 Test (org.junit.Test)37 IIOMetadata (javax.imageio.metadata.IIOMetadata)36 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)30 ParameterBlockJAI (javax.media.jai.ParameterBlockJAI)28 RenderedOp (javax.media.jai.RenderedOp)28 ImageReadParam (javax.imageio.ImageReadParam)26 Iterator (java.util.Iterator)25 ImageReader (javax.imageio.ImageReader)24 FileImageOutputStreamExtImpl (it.geosolutions.imageio.stream.output.FileImageOutputStreamExtImpl)20 ByteArrayInputStream (java.io.ByteArrayInputStream)20 Graphics2D (java.awt.Graphics2D)19 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)17