Search in sources :

Example 81 with ImageWriteParam

use of javax.imageio.ImageWriteParam in project jdk8u_jdk by JetBrains.

the class EncodeSubImageTest method write.

private void write(File f, boolean subsample) throws IOException {
    ImageOutputStream ios = ImageIO.createImageOutputStream(f);
    writer.setOutput(ios);
    ImageWriteParam p = writer.getDefaultWriteParam();
    if (subsample) {
        p.setSourceSubsampling(subSampleX, subSampleY, 0, 0);
    }
    writer.write(null, new IIOImage(img, null, null), p);
    ios.close();
    writer.reset();
}
Also used : ImageWriteParam(javax.imageio.ImageWriteParam) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 82 with ImageWriteParam

use of javax.imageio.ImageWriteParam 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 83 with ImageWriteParam

use of javax.imageio.ImageWriteParam in project Lucee by lucee.

the class Image method getImageWriteParam.

private static ImageWriteParam getImageWriteParam(BufferedImage im, ImageWriter writer, float quality, String srcFormat, String trgFormat) {
    ImageWriteParam iwp;
    if ("jpg".equalsIgnoreCase(srcFormat)) {
        ColorModel cm = im.getColorModel();
        if (cm.hasAlpha())
            im = jpgImage(im);
        JPEGImageWriteParam jiwp = new JPEGImageWriteParam(Locale.getDefault());
        jiwp.setOptimizeHuffmanTables(true);
        iwp = jiwp;
    } else
        iwp = writer.getDefaultWriteParam();
    setCompressionModeEL(iwp, ImageWriteParam.MODE_EXPLICIT);
    setCompressionQualityEL(iwp, quality);
    return iwp;
}
Also used : IndexColorModel(java.awt.image.IndexColorModel) DirectColorModel(java.awt.image.DirectColorModel) ComponentColorModel(java.awt.image.ComponentColorModel) PackedColorModel(java.awt.image.PackedColorModel) ColorModel(java.awt.image.ColorModel) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) ImageWriteParam(javax.imageio.ImageWriteParam) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam)

Example 84 with ImageWriteParam

use of javax.imageio.ImageWriteParam in project litiengine by gurkenlabs.

the class ImageSerializer method saveImage.

public static void saveImage(final String fileName, final BufferedImage image, ImageFormat imageFormat) {
    try {
        final File file = new File(fileName);
        final String extension = FileUtilities.getExtension(fileName);
        Iterator<ImageWriter> iter = null;
        if (canWriteFormat(extension)) {
            iter = ImageIO.getImageWritersByFormatName(extension);
        } else {
            iter = ImageIO.getImageWritersByFormatName(imageFormat.toString());
        }
        final ImageWriter writer = iter.next();
        final ImageWriteParam iwp = writer.getDefaultWriteParam();
        file.getParentFile().mkdirs();
        try (final FileImageOutputStream output = new FileImageOutputStream(file.getAbsoluteFile())) {
            writer.setOutput(output);
            final IIOImage outimage = new IIOImage(image, null, null);
            writer.write(null, outimage, iwp);
            writer.dispose();
        }
    } catch (final IOException e) {
        log.log(Level.SEVERE, e.getMessage(), e);
    }
}
Also used : FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) IIOImage(javax.imageio.IIOImage)

Example 85 with ImageWriteParam

use of javax.imageio.ImageWriteParam in project sldeditor by robward-scisys.

the class LegendManager method saveGridImage.

/**
 * Save image.
 *
 * @param image the grid image
 * @param formatName the format name
 * @param destinationFile the output
 * @param dpi the dpi
 * @return true, if successful
 * @throws IOException Signals that an I/O exception has occurred.
 */
private boolean saveGridImage(BufferedImage image, String formatName, File destinationFile, int dpi) throws IOException {
    destinationFile.delete();
    for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName(formatName); iw.hasNext(); ) {
        ImageWriter writer = iw.next();
        ImageWriteParam writeParam = writer.getDefaultWriteParam();
        ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
        IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);
        if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) {
            continue;
        }
        setDPI(metadata, dpi);
        final ImageOutputStream stream = ImageIO.createImageOutputStream(destinationFile);
        try {
            writer.setOutput(stream);
            writer.write(metadata, new IIOImage(image, null, metadata), writeParam);
        } finally {
            stream.close();
        }
        break;
    }
    return true;
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Aggregations

ImageWriteParam (javax.imageio.ImageWriteParam)120 ImageWriter (javax.imageio.ImageWriter)100 IIOImage (javax.imageio.IIOImage)74 ImageOutputStream (javax.imageio.stream.ImageOutputStream)53 BufferedImage (java.awt.image.BufferedImage)51 File (java.io.File)51 IOException (java.io.IOException)38 Test (org.junit.Test)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)28 ImageReadParam (javax.imageio.ImageReadParam)24 ParameterBlockJAI (javax.media.jai.ParameterBlockJAI)23 RenderedOp (javax.media.jai.RenderedOp)23 IIOMetadata (javax.imageio.metadata.IIOMetadata)22 FileImageOutputStreamExtImpl (it.geosolutions.imageio.stream.output.FileImageOutputStreamExtImpl)20 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)18 ByteArrayInputStream (java.io.ByteArrayInputStream)17 Graphics2D (java.awt.Graphics2D)12 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)12 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)12 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)12