Store a given image to an Image output stream - Java 2D Graphics

Java examples for 2D Graphics:Image

Description

Store a given image to an Image output stream

Demo Code

/*//from ww  w.j a va2s  .  c o m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
//package com.java2s;
import javax.imageio.IIOImage;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.OutputStream;

import java.util.Iterator;

public class Main {
    /**
     * Store a given image to an Image output stream
     * @param mimeType
     * @param image
     * @param os
     * @throws Exception
     */
    public static void storeImage(String mimeType, Image image,
            OutputStream os) throws Exception {
        ImageWriter imageWriter = null;
        BufferedImage bufferedImage = (BufferedImage) image;

        Iterator iterator = javax.imageio.ImageIO
                .getImageWritersByMIMEType(mimeType);
        if (iterator.hasNext()) {
            imageWriter = (ImageWriter) iterator.next();
        }
        ImageOutputStream ios = javax.imageio.ImageIO
                .createImageOutputStream(os);
        imageWriter.setOutput(ios);

        imageWriter.write(new IIOImage(bufferedImage, null, null));
        ios.flush();
        imageWriter.dispose();
    }
}

Related Tutorials