Search in sources :

Example 11 with MemoryCacheImageOutputStream

use of javax.imageio.stream.MemoryCacheImageOutputStream in project jdk8u_jdk by JetBrains.

the class MemoryCacheImageOutputStreamTest method main.

public static void main(String[] args) throws IOException {
    try {
        MemoryCacheImageOutputStream stream = new MemoryCacheImageOutputStream(new ByteArrayOutputStream());
        // or write anything, for that matter
        stream.write(0);
        stream.flush();
    } catch (Exception e) {
        throw new RuntimeException("Error flushing stream: " + e);
    }
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
    byte[] b = new byte[30 * 256];
    byte byteVal = (byte) 0;
    for (int i = 0; i < b.length; i++) {
        b[i] = byteVal++;
    }
    // Write 261,120 bytes
    for (int i = 0; i < 34; i++) {
        ios.write(b);
    }
    // Scatter 256 values at positions 1000, 2000, ...
    // Using both write(int) and write(byte[])
    byte[] buf = new byte[1];
    for (int i = 0; i < 256; i += 2) {
        ios.seek(1000 * i);
        ios.write(i);
        ios.seek(1000 * (i + 1));
        buf[0] = (byte) (i + 1);
        ios.write(buf);
    }
    // Re-read scattered values
    for (int i = 0; i < 256; i++) {
        ios.seek(1000 * i);
        int val = ios.read();
        if (val != i) {
            System.out.println("Got bad value (1) at pos = " + (1000 * i));
        }
    }
    // Discard two buffers and re-read scattered values
    ios.flushBefore(2 * 8192);
    for (int i = 0; i < 256; i++) {
        long pos = 1000 * i;
        if (pos >= 2 * 8192) {
            ios.seek(pos);
            int val = ios.read();
            if (val != i) {
                System.out.println("Got bad value (2) at pos = " + (1000 * i));
            }
        }
    }
    ios.close();
    byte[] data = os.toByteArray();
    for (int i = 0; i < data.length; i++) {
        byte val = data[i];
        if ((i < 256000) && (i % 1000) == 0) {
            if (val != (byte) (i / 1000)) {
                System.out.println("Got bad value (3) at pos = " + i);
            }
        } else {
            byte gval = (byte) ((i % (30 * 256)) % 256);
            if (val != gval) {
                System.out.println("Got bad value (4) at pos = " + i + "(got " + (val & 0xff) + " wanted " + (gval & 0xff) + ")");
            }
        }
    }
}
Also used : MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 12 with MemoryCacheImageOutputStream

use of javax.imageio.stream.MemoryCacheImageOutputStream in project wcomponents by BorderTech.

the class ThumbnailUtil method createScaledJPEG.

/**
 * This method creates an array of bytes representing a JPEG image that is a "scaled" version of the given
 * {@link Image}.
 *
 * @param image The image to be turned into a scaled JPEG.
 * @param scaledSize The size to which the given <em>image</em> is to be scaled.
 * @return A byte[] representing the JPEG image containing the scaled {@link Image}.
 * @throws IOException on any sort of error.
 */
private static byte[] createScaledJPEG(final Image image, final Dimension scaledSize) throws IOException {
    // Scale the image.
    Image scaledImage = image.getScaledInstance(scaledSize.width, scaledSize.height, Image.SCALE_SMOOTH);
    // Create a BufferedImage copy of the scaledImage.
    BufferedImage bufferedImage = new BufferedImage(scaledImage.getWidth(null), scaledImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = bufferedImage.createGraphics();
    graphics.drawImage(scaledImage, 0, 0, null);
    scaledImage.flush();
    graphics.dispose();
    // Convert the scaled image to a JPEG byte array.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    MemoryCacheImageOutputStream mciis = new MemoryCacheImageOutputStream(baos);
    ImageIO.write(bufferedImage, IMAGE_JPEG_FORMAT, mciis);
    mciis.flush();
    bufferedImage.flush();
    byte[] jpeg = baos.toByteArray();
    mciis.close();
    return jpeg;
}
Also used : MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 BufferedImage (java.awt.image.BufferedImage)8 ImageWriter (javax.imageio.ImageWriter)7 ImageOutputStream (javax.imageio.stream.ImageOutputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IIOImage (javax.imageio.IIOImage)5 ImageWriteParam (javax.imageio.ImageWriteParam)4 IOException (java.io.IOException)3 ImageReader (javax.imageio.ImageReader)3 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)3 IIOMetadata (javax.imageio.metadata.IIOMetadata)3 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)3 InputStream (java.io.InputStream)2 ImageInputStream (javax.imageio.stream.ImageInputStream)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 MeasuredOutputStream (com.bric.io.MeasuredOutputStream)1 CacheControl (com.yammer.dropwizard.jersey.caching.CacheControl)1 Timed (com.yammer.metrics.annotation.Timed)1