Search in sources :

Example 61 with ImageOutputStream

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

the class WriteProgressive method main.

public static void main(String[] args) throws IOException {
    Iterator witer = ImageIO.getImageWritersByFormatName("png");
    ImageWriter w = (ImageWriter) witer.next();
    File f = File.createTempFile("WriteProgressive", ".png");
    ImageOutputStream ios = ImageIO.createImageOutputStream(f);
    w.setOutput(ios);
    BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_3BYTE_BGR);
    Graphics2D g = bi.createGraphics();
    Random r = new Random(10);
    for (int i = 0; i < 10000; i++) {
        Color c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
        g.setColor(c);
        g.fillRect(r.nextInt(100), r.nextInt(100), 1, 1);
    }
    IIOImage iioimage = new IIOImage(bi, null, null);
    ImageWriteParam param = w.getDefaultWriteParam();
    param.setProgressiveMode(ImageWriteParam.MODE_DEFAULT);
    try {
        w.write(null, iioimage, param);
    } catch (NullPointerException npe) {
        throw new RuntimeException("Got NPE during write!");
    }
    ios.close();
    BufferedImage bi2 = ImageIO.read(f);
    f.delete();
    ImageCompare.compare(bi, bi2);
}
Also used : Color(java.awt.Color) ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) IIOImage(javax.imageio.IIOImage) Random(java.util.Random) Iterator(java.util.Iterator) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 62 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream in project vcell by virtualcell.

the class ITextWriter method encodeJPEG.

public static ByteArrayOutputStream encodeJPEG(BufferedImage bufferedImage) throws Exception {
    ImageWriter imageWriter = ImageIO.getImageWritersBySuffix("jpeg").next();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream);
    imageWriter.setOutput(imageOutputStream);
    ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();
    imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    // quality 0(very compressed, lossy) -> 1.0(less compressed,loss-less)
    imageWriteParam.setCompressionQuality(1.0f);
    IIOImage iioImage = new IIOImage(bufferedImage, null, null);
    imageWriter.write(null, iioImage, imageWriteParam);
    imageOutputStream.close();
    imageWriter.dispose();
    return byteArrayOutputStream;
}
Also used : ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 63 with ImageOutputStream

use of javax.imageio.stream.ImageOutputStream in project Lucee by lucee.

the class Image method writeOut.

public void writeOut(Resource destination, final String format, boolean overwrite, float quality) throws IOException, ExpressionException {
    if (destination == null) {
        if (source != null)
            destination = source;
        else
            throw new IOException("missing destination file");
    }
    if (destination.exists()) {
        if (!overwrite)
            throw new IOException("can't overwrite existing image");
    }
    if (JAIUtil.isSupportedWriteFormat(format)) {
        JAIUtil.write(getBufferedImage(), destination, format);
        return;
    }
    OutputStream os = null;
    ImageOutputStream ios = null;
    try {
        os = destination.getOutputStream();
        ios = ImageIO.createImageOutputStream(os);
        _writeOut(ios, format, quality, false);
    } finally {
        ImageUtil.closeEL(ios);
        IOUtil.closeEL(os);
    }
}
Also used : ImageOutputStream(javax.imageio.stream.ImageOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 64 with ImageOutputStream

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

the class DynamicImage method getBytes.

/**
 * @return the bytes that make up the document content.
 */
@Override
public byte[] getBytes() {
    if (text != null) {
        try {
            // Draw the image.
            Dimension size = getSize();
            BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D graphics = image.createGraphics();
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            graphics.setColor(new Color(255, 255, 255, 0));
            graphics.fillRect(0, 0, size.width, size.height);
            graphics.setColor(Color.BLUE.darker());
            graphics.fillOval(0, 0, size.width, size.height);
            graphics.setColor(Color.WHITE);
            graphics.setFont(new Font("Arial", Font.BOLD, 24));
            Rectangle2D bounds = graphics.getFontMetrics().getStringBounds(text, graphics);
            int x = (int) (size.width - bounds.getWidth()) / 2;
            int y = (int) (size.height + bounds.getHeight() / 2) / 2;
            graphics.drawString(text, x, y);
            // Write the image to a byte array.
            Iterator<ImageWriter> writers = ImageIO.getImageWritersByMIMEType(getMimeType());
            ImageWriter writer = writers.next();
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageOutputStream ios = ImageIO.createImageOutputStream(os);
            writer.setOutput(ios);
            writer.write(image);
            return os.toByteArray();
        } catch (IOException ex) {
            LOG.error("Unable to generate client image.", ex);
        }
    }
    return new byte[0];
}
Also used : Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) ImageWriter(javax.imageio.ImageWriter) Dimension(java.awt.Dimension) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) ImageOutputStream(javax.imageio.stream.ImageOutputStream) Graphics2D(java.awt.Graphics2D)

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