Search in sources :

Example 46 with IIOImage

use of javax.imageio.IIOImage 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 47 with IIOImage

use of javax.imageio.IIOImage 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)

Aggregations

IIOImage (javax.imageio.IIOImage)47 ImageWriter (javax.imageio.ImageWriter)36 ImageWriteParam (javax.imageio.ImageWriteParam)35 BufferedImage (java.awt.image.BufferedImage)34 ImageOutputStream (javax.imageio.stream.ImageOutputStream)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 File (java.io.File)18 IIOMetadata (javax.imageio.metadata.IIOMetadata)18 IOException (java.io.IOException)17 ByteArrayInputStream (java.io.ByteArrayInputStream)15 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)14 Iterator (java.util.Iterator)9 ImageReader (javax.imageio.ImageReader)9 ImageInputStream (javax.imageio.stream.ImageInputStream)7 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)6 Rectangle (java.awt.Rectangle)5 FileOutputStream (java.io.FileOutputStream)5 Graphics (java.awt.Graphics)4 IIOMetadataNode (javax.imageio.metadata.IIOMetadataNode)4 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)4