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:org.codice.alliance.plugin.nitf.NitfPostIngestPlugin.java

private byte[] renderToJpeg2k(final BufferedImage bufferedImage) throws IOException {

    BufferedImage imageToCompress = bufferedImage;

    if (bufferedImage.getColorModel().getNumComponents() == ARGB_COMPONENT_COUNT) {

        imageToCompress = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(),
                BufferedImage.TYPE_3BYTE_BGR);

        Graphics2D g = imageToCompress.createGraphics();

        g.drawImage(bufferedImage, 0, 0, null);
    }/* w w  w. ja va 2  s .  com*/

    ByteArrayOutputStream os = new ByteArrayOutputStream();

    J2KImageWriter writer = new J2KImageWriter(new J2KImageWriterSpi());
    J2KImageWriteParam writeParams = (J2KImageWriteParam) writer.getDefaultWriteParam();
    writeParams.setLossless(false);
    writeParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    writeParams.setCompressionType("JPEG2000");
    writeParams.setCompressionQuality(0.0f);

    ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
    writer.setOutput(ios);
    writer.write(null, new IIOImage(imageToCompress, null, null), writeParams);
    writer.dispose();
    ios.close();

    return os.toByteArray();
}

From source file:org.geowebcache.layer.MetaTile.java

/**
 * Outputs one tile from the internal array of tiles to a provided stream
 * // ww  w. j  ava  2  s  .c  o m
 * @param tileIdx
 *            the index of the tile relative to the internal array
 * @param format
 *            the Java name for the format
 * @param resource
 *            the outputstream
 * @return true if no error was encountered
 * @throws IOException
 */
public boolean writeTileToStream(final int tileIdx, Resource target) throws IOException {
    if (tiles == null) {
        return false;
    }
    String format = responseFormat.getInternalName();

    if (log.isDebugEnabled()) {
        log.debug("Thread: " + Thread.currentThread().getName() + " writing: " + tileIdx);
    }

    // TODO should we recycle the writers ?
    // GR: it'd be only a 2% perf gain according to profile   
    Iterator<ImageWriter> it = javax.imageio.ImageIO.getImageWritersByFormatName(format);
    ImageWriter writer = it.next();
    ImageWriteParam param = writer.getDefaultWriteParam();

    if (this.formatModifier != null) {
        param = formatModifier.adjustImageWriteParam(param);
    }

    Rectangle tileRegion = tiles[tileIdx];
    RenderedImage tile = createTile(tileRegion.x, tileRegion.y, tileRegion.width, tileRegion.height);
    disposeLater(tile);
    OutputStream outputStream = target.getOutputStream();
    ImageOutputStream imgOut = new MemoryCacheImageOutputStream(outputStream);
    writer.setOutput(imgOut);
    IIOImage image = new IIOImage(tile, null, null);
    try {
        writer.write(null, image, param);
    } finally {
        imgOut.close();
        writer.dispose();
    }

    return true;
}

From source file:org.mrgeo.services.ServletUtils.java

/**
 * Writes an image to a servlet response
 * @param response servlet response//from ww w. j  a v  a2  s. co m
 * @param image image bytes
 * @throws IOException
 */
public static void writeImageToResponse(HttpServletResponse response, byte[] image) throws IOException {
    try {
        ImageOutputStream imageStream = new MemoryCacheImageOutputStream(response.getOutputStream());
        imageStream.write(image, 0, image.length);
        response.setContentLength((int) imageStream.length());
        imageStream.close();
    }
    // TODO: remove these catches - This is to prevent seeing the broken pipes exception
    // over and over again in the trace...leave catch in until issue is fixed (#1122)
    catch (IIOException e) {
    } catch (IndexOutOfBoundsException e) {
    }
}

From source file:org.olat.core.commons.services.image.spi.ImageHelperImpl.java

/**
 * Can change this to choose a better compression level as the default
 * @param image/*from   www .java2 s .co m*/
 * @param scaledImage
 * @return
 */
private static boolean writeTo(BufferedImage image, OutputStream scaledImage, Size scaledSize,
        String outputFormat) {
    try {
        if (!StringHelper.containsNonWhitespace(outputFormat)) {
            outputFormat = OUTPUT_FORMAT;
        }

        Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(outputFormat);
        if (writers.hasNext()) {
            ImageWriter writer = writers.next();
            ImageWriteParam iwp = getOptimizedImageWriteParam(writer, scaledSize);
            IIOImage iiOImage = new IIOImage(image, null, null);
            ImageOutputStream iOut = new MemoryCacheImageOutputStream(scaledImage);
            writer.setOutput(iOut);
            writer.write(null, iiOImage, iwp);
            writer.dispose();
            iOut.flush();
            iOut.close();
            return true;
        } else {
            return ImageIO.write(image, outputFormat, scaledImage);
        }
    } catch (IOException e) {
        return false;
    }
}