Java ImageIcon getByteImage(ImageIcon icon)

Here you can find the source of getByteImage(ImageIcon icon)

Description

Convert a BufferedImage object to a byte[] stream

License

Apache License

Parameter

Parameter Description
icon a parameter

Exception

Parameter Description
IOException an exception

Return

A byte[] image

Declaration

public static byte[] getByteImage(ImageIcon icon) throws IOException 

Method Source Code

//package com.java2s;
/**/*from  w  w  w  .j ava2 s .com*/
 *  Copyright [2011] Steffen K?mpke
 *  mailto: steffen.kaempke@stud4u.de
 *
 *  Licensed 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.
 */

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class Main {
    /**
     * Convert a BufferedImage object to a byte[] stream
     * @param icon 
     * @throws IOException
     * @exception IllegalArgumentException throw new if bIResult null
     * @return A byte[] image
     */
    public static byte[] getByteImage(ImageIcon icon) throws IOException {
        if (icon == null) {
            throw new IllegalArgumentException("null 'image' argument.");
        }
        final BufferedImage bi = getBufferedImage(icon);
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(bi, "png", bos);
        return bos.toByteArray();
    }

    /**
     * Convert a ImageIcon object to a BufferedImage object
     * @param icon Image Object
     * @param additionalHeight
     * @exception IllegalArgumentException throw new if image null
     * @return A BufferedImage image
     */
    public static BufferedImage getBufferedImage(ImageIcon icon) {

        if (icon == null) {
            throw new IllegalArgumentException("null 'icon' argument.");
        }

        final int h = icon.getIconHeight();
        final int w = icon.getIconWidth();
        final BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        final Graphics2D g2d = bi.createGraphics();

        g2d.drawImage(icon.getImage(), 0, 0, w, h, null);
        g2d.dispose();
        return bi;
    }
}

Related

  1. drawImageBorder(Graphics g, ImageIcon img, Rectangle rect, Insets ins, boolean drawCenter)
  2. fitIcon(ImageIcon icon, int containerWidth, int containerHeight)
  3. fitToSquare(ImageIcon icon, int newSize)
  4. generateImageIcon(Color color, int size, Insets insets)
  5. getBorderedIcon(ImageIcon icon)
  6. getDisabledIcon(ImageIcon icon)
  7. getHeight(ImageIcon imagen)
  8. getOpenSwingImage(String name, ImageIcon defaultIcon)
  9. getRenderedImage(ImageIcon icon)