Search in sources :

Example 81 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 82 with IIOImage

use of javax.imageio.IIOImage in project freeplane by freeplane.

the class ExportToImage method exportToImage.

/**
 * Export image.
 * @param toFile
 */
public boolean exportToImage(final RenderedImage image, File chosenFile) {
    try {
        Controller.getCurrentController().getViewController().setWaitingCursor(true);
        Iterator<ImageWriter> imageWritersByFormatName = ImageIO.getImageWritersByFormatName(imageType);
        for (; ; ) {
            ImageWriter writer = imageWritersByFormatName.next();
            ImageWriteParam writeParam = writer.getDefaultWriteParam();
            ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
            IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);
            if ((metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) && imageWritersByFormatName.hasNext()) {
                continue;
            }
            addDpiToMetadata(metadata);
            final FileOutputStream outFile = new FileOutputStream(chosenFile);
            final ImageOutputStream stream = ImageIO.createImageOutputStream(outFile);
            try {
                writer.setOutput(ImageIO.createImageOutputStream(outFile));
                writer.write(metadata, new IIOImage(image, null, metadata), writeParam);
                break;
            } finally {
                stream.close();
                outFile.close();
            }
        }
    } catch (final IOException e1) {
        LogUtils.warn(e1);
        UITools.errorMessage(TextUtils.getText("export_failed"));
    } finally {
        Controller.getCurrentController().getViewController().setWaitingCursor(false);
    }
    return true;
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) FileOutputStream(java.io.FileOutputStream) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 83 with IIOImage

use of javax.imageio.IIOImage in project artisynth_core by artisynth.

the class AnimatedGifWriter method write.

/**
 * Writes a sequence of images to an animated GIF
 * @param file output file
 * @param frames input image sequence
 * @param delayTime time between frames (s)
 * @param loopCount number of times to loop (-1 for infinite)
 * @throws IOException if cannot write to the output file
 */
public static void write(File file, List<? extends BufferedImage> frames, double delayTime, int loopCount) throws IOException {
    ImageWriter iw;
    try {
        iw = ImageIO.getImageWritersByFormatName("gif").next();
    } catch (Exception e) {
        throw new IOException("Cannot write GIF format", e);
    }
    ImageOutputStream ios = ImageIO.createImageOutputStream(file);
    iw.setOutput(ios);
    iw.prepareWriteSequence(null);
    int count = loopCount + 1;
    if (count < 0) {
        count = 0;
    }
    for (int i = 0; i < frames.size(); i++) {
        BufferedImage src = frames.get(i);
        ImageWriteParam iwp = iw.getDefaultWriteParam();
        IIOMetadata metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(src), iwp);
        configure(metadata, delayTime, count, i);
        IIOImage ii = new IIOImage(src, null, metadata);
        iw.writeToSequence(ii, null);
    }
    iw.endWriteSequence();
    ios.close();
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) IIOInvalidTreeException(javax.imageio.metadata.IIOInvalidTreeException) IOException(java.io.IOException) ImageOutputStream(javax.imageio.stream.ImageOutputStream) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOImage(javax.imageio.IIOImage)

Example 84 with IIOImage

use of javax.imageio.IIOImage in project imageio-ext by geosolutions-it.

the class EmptyImage method prepareWriteEmpty.

public void prepareWriteEmpty(IIOMetadata streamMetadata, ImageTypeSpecifier imageType, int width, int height, IIOMetadata imageMetadata, List thumbnails, ImageWriteParam param) throws IOException {
    checkParamsEmpty(imageType, width, height, thumbnails);
    this.isWritingEmpty = true;
    SampleModel emptySM = imageType.getSampleModel();
    RenderedImage emptyImage = new EmptyImage(0, 0, width, height, 0, 0, emptySM.getWidth(), emptySM.getHeight(), emptySM, imageType.getColorModel());
    write(streamMetadata, new IIOImage(emptyImage, null, imageMetadata), param, true, false);
}
Also used : ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) SimpleRenderedImage(com.sun.media.imageioimpl.common.SimpleRenderedImage) RenderedImage(java.awt.image.RenderedImage) SingleTileRenderedImage(com.sun.media.imageioimpl.common.SingleTileRenderedImage) IIOImage(javax.imageio.IIOImage)

Example 85 with IIOImage

use of javax.imageio.IIOImage in project imageio-ext by geosolutions-it.

the class JPEGWriterCompareTest method writeJPEG.

/**
 * Writes outs the image contained into this {@link ImageWorker} as a JPEG using the provided
 * destination , compression and compression rate.
 * <p>
 * The destination object can be anything providing that we have an {@link ImageOutputStreamSpi}
 * that recognizes it.
 *
 * @param destination
 *            where to write the internal {@link #image} as a JPEG.
 * @param compression
 *            algorithm.
 * @param compressionRate
 *            percentage of compression.
 * @param nativeAcc
 *            should we use native acceleration.
 * @return this {@link ImageWorker}.
 * @throws IOException
 *             In case an error occurs during the search for an {@link ImageOutputStream} or
 *             during the eoncding process.
 */
public static final void writeJPEG(final RenderedImage image, final Object destination, final String compression, final float compressionRate, final boolean nativeAcc) throws IOException {
    ImageWriterSpi spi = nativeAcc ? clibSPI : turboSPI;
    ImageWriter writer = spi.createWriterInstance();
    // Compression is available on both lib
    final ImageWriteParam iwp = writer.getDefaultWriteParam();
    final ImageOutputStream outStream = nativeAcc ? new MemoryCacheImageOutputStream((OutputStream) destination) : new ImageOutputStreamAdapter((OutputStream) destination);
    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    iwp.setCompressionType("JPEG");
    // We can control quality here.
    iwp.setCompressionQuality(compressionRate);
    if (nativeAcc) {
        // Lossy compression.
        iwp.setCompressionType(compression);
    }
    try {
        if (iwp instanceof JPEGImageWriteParam) {
            final JPEGImageWriteParam param = (JPEGImageWriteParam) iwp;
            param.setOptimizeHuffmanTables(true);
            param.setProgressiveMode(JPEGImageWriteParam.MODE_DEFAULT);
        }
        writer.setOutput(outStream);
        writer.write(null, new IIOImage(image, null, null), iwp);
    } finally {
        if (writer != null) {
            try {
                writer.dispose();
            } catch (Throwable e) {
                System.out.println(e.getLocalizedMessage());
            }
        }
        if (outStream != null) {
            try {
                ((ImageOutputStream) outStream).close();
            } catch (Throwable e) {
                System.out.println(e.getLocalizedMessage());
            }
        }
    }
}
Also used : MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) ImageWriter(javax.imageio.ImageWriter) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi) ImageWriteParam(javax.imageio.ImageWriteParam) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) ImageOutputStreamAdapter(it.geosolutions.imageio.stream.output.ImageOutputStreamAdapter) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) IIOImage(javax.imageio.IIOImage)

Aggregations

IIOImage (javax.imageio.IIOImage)123 ImageWriter (javax.imageio.ImageWriter)92 ImageWriteParam (javax.imageio.ImageWriteParam)79 ImageOutputStream (javax.imageio.stream.ImageOutputStream)79 BufferedImage (java.awt.image.BufferedImage)66 File (java.io.File)45 IOException (java.io.IOException)41 ByteArrayOutputStream (java.io.ByteArrayOutputStream)37 IIOMetadata (javax.imageio.metadata.IIOMetadata)32 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)25 ByteArrayInputStream (java.io.ByteArrayInputStream)22 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)17 ImageReader (javax.imageio.ImageReader)16 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)16 Graphics2D (java.awt.Graphics2D)14 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)14 Test (org.junit.Test)14 FileOutputStream (java.io.FileOutputStream)13 Iterator (java.util.Iterator)11 RenderedImage (java.awt.image.RenderedImage)10