Search in sources :

Example 36 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream 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 37 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream 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 38 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream 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 39 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream 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 40 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream in project android by JetBrains.

the class WebpImageWriterSpi method writeImage.

public static void writeImage(@NotNull BufferedImage image, @NotNull OutputStream outputStream, boolean lossless, int quality) throws IOException {
    // Instead of
    //   ImageIO.write(image, WebpMetadata.WEBP_FORMAT_LOWER_CASE, stream);
    // which will pass null as the parameters to the ImageWriter, call through to the ImageWriter
    // more directly so we can set up the proper parameters:
    ImageTypeSpecifier type = ImageTypeSpecifier.createFromRenderedImage(image);
    Iterator<ImageWriter> iterator = ImageIO.getImageWriters(type, WebpMetadata.WEBP_FORMAT_LOWER_CASE);
    if (iterator.hasNext()) {
        ImageWriter writer = iterator.next();
        try (ImageOutputStream stream = ImageIO.createImageOutputStream(outputStream)) {
            writer.setOutput(stream);
            try {
                ImageWriteParam param = new WebpImageWriteParam(writer.getLocale(), !lossless, quality / 100f);
                writer.write(null, new IIOImage(image, null, null), param);
            } finally {
                writer.dispose();
                stream.flush();
            }
        }
    }
}
Also used : ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Aggregations

ImageOutputStream (javax.imageio.stream.ImageOutputStream)64 ImageWriter (javax.imageio.ImageWriter)41 BufferedImage (java.awt.image.BufferedImage)37 ByteArrayOutputStream (java.io.ByteArrayOutputStream)36 IIOImage (javax.imageio.IIOImage)33 IOException (java.io.IOException)26 ImageWriteParam (javax.imageio.ImageWriteParam)26 File (java.io.File)24 IIOMetadata (javax.imageio.metadata.IIOMetadata)15 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)12 Iterator (java.util.Iterator)9 ImageInputStream (javax.imageio.stream.ImageInputStream)9 ImageReader (javax.imageio.ImageReader)7 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)7 Graphics2D (java.awt.Graphics2D)5 FileOutputStream (java.io.FileOutputStream)5 Color (java.awt.Color)4 Graphics (java.awt.Graphics)4 OutputStream (java.io.OutputStream)4