Search in sources :

Example 6 with MemoryCacheImageOutputStream

use of javax.imageio.stream.MemoryCacheImageOutputStream in project Openfire by igniterealtime.

the class Resizer method cropAndShrink.

public static byte[] cropAndShrink(final byte[] bytes, final int targetDimension, final ImageWriter iw) {
    Log.debug("Original image size: {} bytes.", bytes.length);
    BufferedImage avatar;
    try (final ByteArrayInputStream stream = new ByteArrayInputStream(bytes)) {
        avatar = ImageIO.read(stream);
        if (avatar.getWidth() <= targetDimension && avatar.getHeight() <= targetDimension) {
            Log.debug("Original image dimension ({}x{}) is within acceptable bounds ({}x{}). No need to resize.", avatar.getWidth(), avatar.getHeight(), targetDimension, targetDimension);
            return null;
        }
    } catch (IOException | RuntimeException ex) {
        Log.warn("Failed to resize avatar. An unexpected exception occurred while reading the original image.", ex);
        return null;
    }
    /* We're going to be resizing, let's crop the image so that it's square and figure out the new starting size. */
    Log.debug("Original image is " + avatar.getWidth() + "x" + avatar.getHeight() + " pixels");
    final int targetWidth, targetHeight;
    if (avatar.getHeight() == avatar.getWidth()) {
        Log.debug("Original image is already square ({}x{})", avatar.getWidth(), avatar.getHeight());
        targetWidth = targetHeight = avatar.getWidth();
    } else {
        final int x, y;
        if (avatar.getHeight() > avatar.getWidth()) {
            Log.debug("Original image is taller ({}) than wide ({}).", avatar.getHeight(), avatar.getWidth());
            x = 0;
            y = (avatar.getHeight() - avatar.getWidth()) / 2;
            targetWidth = targetHeight = avatar.getWidth();
        } else {
            Log.debug("Original image is wider ({}) than tall ({}).", avatar.getWidth(), avatar.getHeight());
            x = (avatar.getWidth() - avatar.getHeight()) / 2;
            y = 0;
            targetWidth = targetHeight = avatar.getHeight();
        }
        // pull out a square image, centered.
        avatar = avatar.getSubimage(x, y, targetWidth, targetHeight);
    }
    /* Let's crop/scale the image as necessary out the new dimensions. */
    final BufferedImage resizedAvatar = new BufferedImage(targetDimension, targetDimension, avatar.getType());
    final AffineTransform scale = AffineTransform.getScaleInstance((double) targetDimension / (double) targetWidth, (double) targetDimension / (double) targetHeight);
    final Graphics2D g = resizedAvatar.createGraphics();
    g.drawRenderedImage(avatar, scale);
    Log.debug("Resized image is {}x{}.", resizedAvatar.getWidth(), resizedAvatar.getHeight());
    /* Now we have to dump the new jpeg, png, etc. to a byte array */
    try (final ByteArrayOutputStream bostream = new ByteArrayOutputStream();
        final ImageOutputStream iostream = new MemoryCacheImageOutputStream(bostream)) {
        iw.setOutput(iostream);
        iw.write(resizedAvatar);
        final byte[] data = bostream.toByteArray();
        Log.debug("Resized image size: {} bytes.", data.length);
        return data;
    } catch (IOException | RuntimeException ex) {
        Log.warn("Failed to resize avatar. An unexpected exception occurred while writing the resized image.", ex);
        return null;
    }
}
Also used : MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) AffineTransform(java.awt.geom.AffineTransform) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream)

Example 7 with MemoryCacheImageOutputStream

use of javax.imageio.stream.MemoryCacheImageOutputStream 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 8 with MemoryCacheImageOutputStream

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

the class RasterWithMinXTest method main.

public static void main(String[] args) {
    String format = "jpeg";
    // Set output file.
    ImageOutputStream output = new MemoryCacheImageOutputStream(new ByteArrayOutputStream());
    // Create image.
    BufferedImage bi = new BufferedImage(256, 256, BufferedImage.TYPE_3BYTE_BGR);
    // Populate image.
    int[] rgbArray = new int[256];
    for (int i = 0; i < 256; i++) {
        Arrays.fill(rgbArray, i);
        bi.setRGB(0, i, 256, 1, rgbArray, 0, 256);
    }
    // create translated raster in order to get non-zero minX and minY
    WritableRaster r = (WritableRaster) bi.getRaster().createTranslatedChild(64, 64);
    Iterator i = ImageIO.getImageWritersByFormatName(format);
    ImageWriter iw = null;
    while (i.hasNext() && iw == null) {
        Object o = i.next();
        if (o instanceof com.sun.imageio.plugins.jpeg.JPEGImageWriter) {
            iw = (ImageWriter) o;
        }
    }
    if (iw == null) {
        throw new RuntimeException("No available image writer");
    }
    ImageWriteParam iwp = iw.getDefaultWriteParam();
    IIOMetadata metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bi.getColorModel(), r.getSampleModel()), iwp);
    IIOImage img = new IIOImage(r, null, metadata);
    iw.setOutput(output);
    try {
        iw.write(img);
    } catch (RasterFormatException e) {
        e.printStackTrace();
        throw new RuntimeException("RasterException occurs. Test Failed!");
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException("Unexpected Exception");
    }
    // test case of theImageWriteParam with non-null sourceRegion
    iwp.setSourceRegion(new Rectangle(32, 32, 192, 192));
    metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bi.getColorModel(), r.getSampleModel()), iwp);
    try {
        iw.write(metadata, img, iwp);
    } catch (RasterFormatException e) {
        e.printStackTrace();
        throw new RuntimeException("SetSourceRegion causes the RasterException. Test Failed!");
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException("Unexpected Exception");
    }
}
Also used : ImageWriter(javax.imageio.ImageWriter) Rectangle(java.awt.Rectangle) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) RasterFormatException(java.awt.image.RasterFormatException) IIOImage(javax.imageio.IIOImage) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) IIOMetadata(javax.imageio.metadata.IIOMetadata) WritableRaster(java.awt.image.WritableRaster) Iterator(java.util.Iterator) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) RasterFormatException(java.awt.image.RasterFormatException)

Example 9 with MemoryCacheImageOutputStream

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

the class WriteBitsTest method main.

public static void main(String[] argv) throws RuntimeException {
    ByteArrayOutputStream ostream = new ByteArrayOutputStream();
    MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(ostream);
    try {
        // verify correct writeBits() functionality
        long streampos = 0;
        int bitoffset = 0;
        mcios.setBitOffset(bitoffset);
        verify(mcios, streampos, bitoffset);
        bitoffset = 3;
        mcios.setBitOffset(bitoffset);
        verify(mcios, streampos, bitoffset);
        for (int incr = 3; incr <= 15; incr += 12) {
            for (int i = 0; i < 64; i += incr) {
                mcios.writeBits(10, incr);
                bitoffset += incr;
                if (bitoffset > 7) {
                    int stroffset = bitoffset / 8;
                    bitoffset = bitoffset % 8;
                    streampos += stroffset;
                }
                verify(mcios, streampos, bitoffset);
            }
        }
        // verify correct read(byte[], int, int) functionality
        byte[] bytearr = new byte[2];
        mcios.seek(2);
        mcios.setBitOffset(3);
        int numread = mcios.read(bytearr, 0, 2);
        if (numread != 2) {
            throw new RuntimeException("Error in mcios.read([BII)I");
        }
        verify(mcios, 4, 0);
        // verify correct read() functionality
        mcios.setBitOffset(3);
        mcios.read();
        verify(mcios, 5, 0);
    } catch (IOException e) {
        throw new RuntimeException("Unexpected IOException: " + e);
    }
}
Also used : MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 10 with MemoryCacheImageOutputStream

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

the class FlushBefore method main.

public static void main(String[] args) throws IOException {
    OutputStream ostream = new ByteArrayOutputStream();
    FileCacheImageOutputStream fcios = new FileCacheImageOutputStream(ostream, null);
    test(fcios);
    MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(ostream);
    test(mcios);
}
Also used : MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileCacheImageOutputStream(javax.imageio.stream.FileCacheImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileCacheImageOutputStream(javax.imageio.stream.FileCacheImageOutputStream)

Aggregations

MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 BufferedImage (java.awt.image.BufferedImage)7 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