Example usage for javax.imageio.stream MemoryCacheImageOutputStream MemoryCacheImageOutputStream

List of usage examples for javax.imageio.stream MemoryCacheImageOutputStream MemoryCacheImageOutputStream

Introduction

In this page you can find the example usage for javax.imageio.stream MemoryCacheImageOutputStream MemoryCacheImageOutputStream.

Prototype

public MemoryCacheImageOutputStream(OutputStream stream) 

Source Link

Document

Constructs a MemoryCacheImageOutputStream that will write to a given OutputStream .

Usage

From source file:gr.iti.mklab.reveal.forensics.util.Util.java

public static BufferedImage recompressImage(BufferedImage imageIn, int quality) {
    // Apply in-memory JPEG compression to a BufferedImage given a quality setting (0-100)
    // and return the resulting BufferedImage
    float fQuality = (float) (quality / 100.0);
    BufferedImage outputImage = null;
    try {//from w  w  w.  j a  v a 2  s  . c  o  m
        ImageWriter writer;
        Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
        writer = iter.next();
        ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(fQuality);
        byte[] imageInByte;
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(baos);
            writer.setOutput(mcios);
            IIOImage tmpImage = new IIOImage(imageIn, null, null);
            writer.write(null, tmpImage, iwp);
            writer.dispose();
            baos.flush();
            imageInByte = baos.toByteArray();
        }
        InputStream in = new ByteArrayInputStream(imageInByte);
        outputImage = ImageIO.read(in);
    } catch (Exception ex) {
        Logger.getLogger(Util.class.getName()).log(Level.SEVERE, null, ex);
    }
    return outputImage;
}

From source file:org.shredzone.cilla.service.resource.ImageProcessorImpl.java

@Override
@Cacheable(value = "processedImages", key = "#id + '-' + #process")
public ImageProcessorResult process(DataSource ds, long id, ImageProcessing process) throws IOException {
    ImageType type = process.getType();/*from   w ww  .j a va2  s  . c o  m*/
    int width = process.getWidth();
    int height = process.getHeight();

    if (type == null) {
        if (width > 0 && width <= 256 && height > 0 && height <= 256) {
            type = ImageType.PNG;
        } else {
            type = ImageType.JPEG;
        }
    }

    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        Thumbnails.Builder<? extends InputStream> builder = Thumbnails.of(ds.getInputStream());
        if (width > 0 && height > 0) {
            builder.size(width, height);
        } else if (width > 0) {
            builder.width(width);
        } else if (height > 0) {
            builder.height(height);
        }

        BufferedImage scaled = builder.asBufferedImage();

        if (type == ImageType.JPEG_LOW) {
            jpegQualityWriter(scaled, new MemoryCacheImageOutputStream(out), type.getCompression());
        } else {
            ImageIO.write(scaled, type.getFormatName(), out);
        }

        ImageProcessorResult result = new ImageProcessorResult();
        result.setData(out.toByteArray());
        result.setContentType(type.getContentType());
        return result;
    }
}

From source file:de.jwic.ecolib.controls.chart.ChartControl.java

public void renderImage() throws IOException {
    // create image to draw into
    BufferedImage bi = new BufferedImage(width < 10 ? 10 : width, height < 10 ? 10 : height,
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bi.createGraphics();

    if (chart != null) {
        chart.setBackgroundPaint(Color.WHITE);
        chart.draw(g2d, new Rectangle2D.Double(0, 0, width < 10 ? 10 : width, height < 10 ? 10 : height));
    } else {//from  w w w  . j  a v a 2  s. c  o m
        g2d.setColor(Color.BLACK);
        g2d.drawString("No chart has been assigned.", 1, 20);
    }
    // finish drawing
    g2d.dispose();

    // write image data into output stream
    ByteArrayOutputStream out = getImageOutputStream();
    out.reset();
    // create a PNG image
    ImageWriter imageWriter = new PNGImageWriter(null);
    ImageWriteParam param = imageWriter.getDefaultWriteParam();
    imageWriter.setOutput(new MemoryCacheImageOutputStream(out));
    imageWriter.write(null, new IIOImage(bi, null, null), param);
    imageWriter.dispose();

    setMimeType(MIME_TYPE_PNG);
}

From source file:ar.com.zauber.common.image.impl.AbstractImage.java

/**
 * Creates a thumbnail/*from   ww  w .  jav a  2  s  .c  o  m*/
 * 
 * @param is data source
 * @param os data source
 * @throws IOException if there is a problem reading is
 */
public static void createThumbnail(final InputStream is, final OutputStream os, final int target)
        throws IOException {
    final float compression = 0.85F;
    ImageWriter writer = null;
    MemoryCacheImageOutputStream mos = null;
    Graphics2D graphics2D = null;
    try {
        final BufferedImage bi = ImageIO.read(is);
        final Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("JPG");
        if (!iter.hasNext()) {
            throw new IllegalStateException("can't find JPG subsystem");
        }

        int w = bi.getWidth(), h = bi.getHeight();
        if (w < target && h < target) {
            // nothing to recalculate, ya es chiquita.
        } else {
            if (w > h) {
                h = target * bi.getHeight() / bi.getWidth();
                w = target;
            } else {
                w = target * bi.getWidth() / bi.getHeight();
                h = target;
            }
        }
        // draw original image to thumbnail image object and
        // scale it to the new size on-the-fly
        final BufferedImage thumbImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        graphics2D = thumbImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(bi, 0, 0, w, h, null);

        writer = (ImageWriter) iter.next();
        final ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(compression);
        mos = new MemoryCacheImageOutputStream(os);
        writer.setOutput(mos);
        writer.write(null, new IIOImage(thumbImage, null, null), iwp);
    } finally {
        if (writer != null) {
            writer.dispose();
        }
        if (mos != null) {
            mos.close();
        }
        if (graphics2D != null) {
            graphics2D.dispose();
        }
        is.close();
        os.close();
    }
}

From source file:jails.http.converter.BufferedImageHttpMessageConverter.java

private ImageOutputStream createImageOutputStream(OutputStream os) throws IOException {
    if (this.cacheDir != null) {
        return new FileCacheImageOutputStream(os, this.cacheDir);
    } else {/*  w w w . j  av  a 2s  . c  o  m*/
        return new MemoryCacheImageOutputStream(os);
    }
}

From source file:com.itextpdf.text.pdf.pdfcleanup.PdfCleanUpRenderListener.java

private byte[] getJPGBytes(BufferedImage image) {
    ByteArrayOutputStream outputStream = null;

    try {/*  w w w.ja  va 2  s  .c o m*/
        ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next();
        ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam();
        jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        jpgWriteParam.setCompressionQuality(1.0f);

        outputStream = new ByteArrayOutputStream();
        jpgWriter.setOutput(new MemoryCacheImageOutputStream((outputStream)));
        IIOImage outputImage = new IIOImage(image, null, null);

        jpgWriter.write(null, outputImage, jpgWriteParam);
        jpgWriter.dispose();
        outputStream.flush();

        return outputStream.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        closeOutputStream(outputStream);
    }
}

From source file:org.apache.flex.compiler.internal.embedding.transcoders.JPEGTranscoder.java

private byte[] bufferedImageToJPEG(ImageInfo imageInfo, int[] pixels) throws Exception {
    BufferedImage bufferedImage = new BufferedImage(imageInfo.width, imageInfo.height,
            BufferedImage.TYPE_INT_ARGB);
    bufferedImage.setRGB(0, 0, imageInfo.width, imageInfo.height, pixels, 0, imageInfo.width);

    ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
    ImageWriteParam writeParam = writer.getDefaultWriteParam();
    ColorModel colorModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00, 0x000000ff);
    ImageTypeSpecifier imageTypeSpecifier = new ImageTypeSpecifier(colorModel,
            colorModel.createCompatibleSampleModel(1, 1));
    writeParam.setDestinationType(imageTypeSpecifier);
    writeParam.setSourceBands(new int[] { 0, 1, 2 });
    writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);

    float q = 1.0f;
    if (quality != null)
        q = quality.floatValue();//w w w  . ja  va2 s. c o m
    writeParam.setCompressionQuality(q);

    DAByteArrayOutputStream buffer = new DAByteArrayOutputStream();
    writer.setOutput(new MemoryCacheImageOutputStream(buffer));

    IIOImage ioImage = new IIOImage(bufferedImage, null, null);

    writer.write(null, ioImage, writeParam);
    writer.dispose();

    return buffer.getDirectByteArray();
}

From source file:org.apache.pdfbox.filter.LZWFilter.java

/**
 * {@inheritDoc}//www . j a  v  a 2  s .  c o m
 */
@Override
protected void encode(InputStream rawData, OutputStream encoded, COSDictionary parameters) throws IOException {
    List<byte[]> codeTable = createCodeTable();
    int chunk = 9;

    byte[] inputPattern = null;
    final MemoryCacheImageOutputStream out = new MemoryCacheImageOutputStream(encoded);
    out.writeBits(CLEAR_TABLE, chunk);
    int foundCode = -1;
    int r;
    while ((r = rawData.read()) != -1) {
        byte by = (byte) r;
        if (inputPattern == null) {
            inputPattern = new byte[] { by };
            foundCode = by & 0xff;
        } else {
            inputPattern = Arrays.copyOf(inputPattern, inputPattern.length + 1);
            inputPattern[inputPattern.length - 1] = by;
            int newFoundCode = findPatternCode(codeTable, inputPattern);
            if (newFoundCode == -1) {
                // use previous
                chunk = calculateChunk(codeTable.size() - 1, 1);
                out.writeBits(foundCode, chunk);
                // create new table entry
                codeTable.add(inputPattern);

                if (codeTable.size() == 4096) {
                    // code table is full
                    out.writeBits(CLEAR_TABLE, chunk);
                    codeTable = createCodeTable();
                }

                inputPattern = new byte[] { by };
                foundCode = by & 0xff;
            } else {
                foundCode = newFoundCode;
            }
        }
    }
    if (foundCode != -1) {
        chunk = calculateChunk(codeTable.size() - 1, 1);
        out.writeBits(foundCode, chunk);
    }

    // PPDFBOX-1977: the decoder wouldn't know that the encoder would output 
    // an EOD as code, so he would have increased his own code table and 
    // possibly adjusted the chunk. Therefore, the encoder must behave as 
    // if the code table had just grown and thus it must be checked it is
    // needed to adjust the chunk, based on an increased table size parameter
    chunk = calculateChunk(codeTable.size(), 1);

    out.writeBits(EOD, chunk);

    // pad with 0
    out.writeBits(0, 7);

    // must do or file will be empty :-(
    out.flush();
    out.close();
}

From source file:org.codice.alliance.imaging.chip.transformer.CatalogOutputAdapter.java

private byte[] createJpg(BufferedImage image) throws IOException {
    try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        try (ImageOutputStream imageOutputStream = new MemoryCacheImageOutputStream(os)) {
            ImageIO.write(image, JPG, imageOutputStream);
        }// w  w  w . j  a v a  2 s.  c  om
        return os.toByteArray();
    }
}