Example usage for javax.imageio ImageWriter setOutput

List of usage examples for javax.imageio ImageWriter setOutput

Introduction

In this page you can find the example usage for javax.imageio ImageWriter setOutput.

Prototype

public void setOutput(Object output) 

Source Link

Document

Sets the destination to the given ImageOutputStream or other Object .

Usage

From source file:Main.java

static public void main(String args[]) throws Exception {
    int width = 200, height = 200;
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    Graphics2D ig2 = bi.createGraphics();
    ig2.fillRect(0, 0, width - 1, height - 1);

    Iterator imageWriters = ImageIO.getImageWritersByFormatName("GIF");
    ImageWriter imageWriter = (ImageWriter) imageWriters.next();
    File file = new File("filename.gif");
    ImageOutputStream ios = ImageIO.createImageOutputStream(file);
    imageWriter.setOutput(ios);
    imageWriter.write(bi);/*from www .  java 2s .c o m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com/style/download.png");
    BufferedImage bi = ImageIO.read(url);
    for (float q = 0.2f; q < .9f; q += .2f) {
        OutputStream outStream = new FileOutputStream(new File("c:/Java_Dev/Image-" + q + ".jpg"));
        ImageWriter imgWriter = ImageIO.getImageWritersByFormatName("jpg").next();
        ImageOutputStream ioStream = ImageIO.createImageOutputStream(outStream);
        imgWriter.setOutput(ioStream);

        JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(Locale.getDefault());
        jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        jpegParams.setCompressionQuality(q);

        imgWriter.write(null, new IIOImage(bi, null, null), jpegParams);

        ioStream.flush();/*  ww w  .  ja  v  a  2 s  .  c o m*/
        ioStream.close();
        imgWriter.dispose();
    }
}

From source file:Main.java

static public void main(String args[]) throws Exception {
    FileInputStream fin = new FileInputStream("a.gif");
    Iterator readers = ImageIO.getImageReadersBySuffix("GIF");
    ImageReader imageReader = (ImageReader) readers.next();
    ImageInputStream iis = ImageIO.createImageInputStream(fin);
    imageReader.setInput(iis, false);/*from  w  w w. j av a 2  s.co m*/

    imageReader.addIIOReadProgressListener(new IIOReadProgressListener() {
        public void imageComplete(ImageReader source) {
            System.out.println("image complete " + source);
        }

        public void imageProgress(ImageReader source, float percentageDone) {
            System.out.println("image progress " + source + ": " + percentageDone + "%");
        }

        public void imageStarted(ImageReader source, int imageIndex) {
            System.out.println("image #" + imageIndex + " started " + source);
        }

        public void readAborted(ImageReader source) {
            System.out.println("read aborted " + source);
        }

        public void sequenceComplete(ImageReader source) {
            System.out.println("sequence complete " + source);
        }

        public void sequenceStarted(ImageReader source, int minIndex) {
            System.out.println("sequence started " + source + ": " + minIndex);
        }

        public void thumbnailComplete(ImageReader source) {
            System.out.println("thumbnail complete " + source);
        }

        public void thumbnailProgress(ImageReader source, float percentageDone) {
            System.out.println("thumbnail started " + source + ": " + percentageDone + "%");
        }

        public void thumbnailStarted(ImageReader source, int imageIndex, int thumbnailIndex) {
            System.out.println("thumbnail progress " + source + ", " + thumbnailIndex + " of " + imageIndex);
        }
    });

    BufferedImage image = imageReader.read(0);

    Iterator imageWriters = ImageIO.getImageWritersBySuffix("JPG");
    ImageWriter imageWriter = (ImageWriter) imageWriters.next();
    File file = new File("b.JPG");
    ImageOutputStream ios = ImageIO.createImageOutputStream(file);
    imageWriter.setOutput(ios);
    imageWriter.write(image);
}

From source file:org.modelibra.util.ImageHandler.java

/**
 * Saves an image to a jpeg file./*from  w  ww . j av  a2  s.com*/
 * 
 * @param bi
 *            buffered image
 * @param file
 *            jpeg file
 * @param quality
 *            image quality
 */
public static void saveImageToJPEGFile14(BufferedImage bi, File file, float quality) throws IOException {

    JPEGImageWriteParam param = new JPEGImageWriteParam(null);
    ImageTypeSpecifier type = new ImageTypeSpecifier(bi);
    param.setDestinationType(type);
    ImageOutputStream imgos = ImageIO.createImageOutputStream(file);
    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpeg");
    ImageWriter writer = writers.next();
    writer.setOutput(imgos);
    writer.write(bi);
    imgos.close();
}

From source file:Main.java

private static void writeJpegCompressedImage(BufferedImage image, String outFile) throws IOException {
    float qualityFloat = 1f;
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();

    ImageWriter imgWriter = ImageIO.getImageWritersByFormatName("jpg").next();
    ImageOutputStream ioStream = ImageIO.createImageOutputStream(outStream);
    imgWriter.setOutput(ioStream);

    JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(Locale.getDefault());
    jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    jpegParams.setCompressionQuality(qualityFloat);

    imgWriter.write(null, new IIOImage(image, null, null), jpegParams);

    ioStream.flush();//from  www.j av a  2s .com
    ioStream.close();
    imgWriter.dispose();

    OutputStream outputStream = new FileOutputStream(outFile);
    outStream.writeTo(outputStream);

}

From source file:org.egov.infra.utils.ImageUtils.java

public static File compressImage(final InputStream imageStream, String imageFileName, boolean closeStream)
        throws IOException {
    File compressedImage = Paths.get(imageFileName).toFile();
    try (final ImageOutputStream imageOutput = createImageOutputStream(compressedImage)) {
        ImageWriter writer = getImageWritersByFormatName(
                defaultString(getExtension(imageFileName), JPG_FORMAT_NAME)).next();
        writer.setOutput(imageOutput);
        ImageWriteParam writeParam = writer.getDefaultWriteParam();
        if (writeParam.canWriteCompressed()) {
            writeParam.setCompressionMode(MODE_EXPLICIT);
            writeParam.setCompressionType(writeParam.getCompressionTypes()[0]);
            writeParam.setCompressionQuality(0.05F);
        }//w w w.j a v  a2  s  .c o  m
        writer.write(null, new IIOImage(read(imageStream), null, null), writeParam);
        writer.dispose();
        if (closeStream)
            imageStream.close();
    }
    return compressedImage;
}

From source file:com.tomtom.speedtools.json.ImageSerializer.java

@Nonnull
private static byte[] writeAsBytes(@Nonnull final Image v) throws IOException {
    assert v != null;

    /**/*www  .ja v a2 s  .  c  om*/
     * Create a PNG output stream.
     */
    final String mimeType = "image/png";
    try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
        final Iterator<ImageWriter> it = ImageIO.getImageWritersByMIMEType(mimeType);
        if (it.hasNext()) {
            final ImageWriter w = it.next();
            final ImageOutputStream os = ImageIO.createImageOutputStream(stream);
            w.setOutput(os);
            w.write(convertToBufferedImage(v));
            os.close();
            w.dispose();
        } else {
            LOG.info("writeAsBytes: No encoder for MIME type " + mimeType);
            throw new IOException("No encoder for MIME type " + mimeType);
        }
        return stream.toByteArray();
    }
}

From source file:com.mirth.connect.server.util.DICOMUtil.java

private static byte[] saveAsJpeg(ImagePlus imagePlug, int quality) {
    int imageType = BufferedImage.TYPE_INT_RGB;

    if (imagePlug.getProcessor().isDefaultLut()) {
        imageType = BufferedImage.TYPE_BYTE_GRAY;
    }/* w  ww.ja  v a2  s . c o m*/

    BufferedImage bufferedImage = new BufferedImage(imagePlug.getWidth(), imagePlug.getHeight(), imageType);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    try {
        Graphics graphics = bufferedImage.createGraphics();
        graphics.drawImage(imagePlug.getImage(), 0, 0, null);
        graphics.dispose();
        ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
        writer.setOutput(ImageIO.createImageOutputStream(baos));
        ImageWriteParam param = writer.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(quality / 100f);

        if (quality == 100) {
            param.setSourceSubsampling(1, 1, 0, 0);
        }

        IIOImage iioImage = new IIOImage(bufferedImage, null, null);
        writer.write(null, iioImage, param);
        return baos.toByteArray();
    } catch (Exception e) {
        logger.error("Error converting dcm file", e);
    } finally {
        IOUtils.closeQuietly(baos);
    }

    return null;
}

From source file:com.mirth.connect.server.util.DICOMMessageUtil.java

private static byte[] saveAsJpeg(ImagePlus imagePlug, int quality) {
    int imageType = BufferedImage.TYPE_INT_RGB;

    if (imagePlug.getProcessor().isDefaultLut()) {
        imageType = BufferedImage.TYPE_BYTE_GRAY;
    }//from   w ww  . ja v  a 2s .c  o  m

    BufferedImage bufferedImage = new BufferedImage(imagePlug.getWidth(), imagePlug.getHeight(), imageType);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    try {
        Graphics graphics = bufferedImage.createGraphics();
        graphics.drawImage(imagePlug.getImage(), 0, 0, null);
        graphics.dispose();
        ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
        writer.setOutput(ImageIO.createImageOutputStream(baos));
        ImageWriteParam param = writer.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(quality / 100f);

        if (quality == 100) {
            param.setSourceSubsampling(1, 1, 0, 0);
        }

        IIOImage iioImage = new IIOImage(bufferedImage, null, null);
        writer.write(null, iioImage, param);
        return baos.toByteArray();
    } catch (Exception e) {
        logger.error("Error Converting DICOM image", e);
    } finally {
        IOUtils.closeQuietly(baos);
    }

    return null;
}

From source file:de.bamamoto.mactools.png2icns.Scaler.java

public static void save(BufferedImage image, String filename) throws IOException {
    Iterator writers = ImageIO.getImageWritersByFormatName("PNG");
    if (writers.hasNext()) {
        ImageWriter imageWriter = (ImageWriter) writers.next();
        ImageWriteParam params = imageWriter.getDefaultWriteParam();

        File outFile = new File(filename);
        try (FileImageOutputStream output = new FileImageOutputStream(outFile)) {
            imageWriter.setOutput(output);
            IIOImage outImage = new IIOImage(image, null, null);
            imageWriter.write(null, outImage, params);
        }//w  w  w.ja  v  a 2  s  .co m
    }
}