Java Image getInputStreamFromImage(Image imagen, String format)

Here you can find the source of getInputStreamFromImage(Image imagen, String format)

Description

Obtiene un objeto InputStream a partir de un objeto Image

License

Open Source License

Declaration

public static InputStream getInputStreamFromImage(Image imagen, String format) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.*;

import java.awt.image.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Main {
    /**//w w  w .  j  a  v  a  2s . c  om
     * Obtiene un objeto InputStream a partir de un objeto Image
     */
    public static InputStream getInputStreamFromImage(Image imagen, String format) {

        BufferedImage bi = toBufferedImage(imagen);

        ByteArrayOutputStream buffer_img = new ByteArrayOutputStream();

        try {
            ImageIO.write(bi, format, buffer_img);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        byte[] bytes = buffer_img.toByteArray();
        return new ByteArrayInputStream(bytes);

    }

    /**
     * Convierte un objeto Image a un BufferedImage
     *
     * @param img El objeto Image a convertir
     * @return El BufferedImage obtenido
     */
    public static BufferedImage toBufferedImage(Image img) {
        if (img instanceof BufferedImage) {
            return (BufferedImage) img;
        }
        // Crea un buffered image con trasparencia
        BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null),
                BufferedImage.TYPE_INT_ARGB);
        // Dibuja la imagen en el buffered image
        Graphics2D bGr = bimage.createGraphics();
        bGr.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        bGr.drawImage(img, 0, 0, null);
        bGr.dispose();
        // Retorna el buffered image
        return bimage;
    }

    /**
     * Obtiene el ancho de una Image
     */
    public static int getWidth(Image imagen) {
        int width = new ImageIcon(imagen).getIconWidth();
        return width;
    }

    /**
     * Obtiene el ancho de un IconImage
     */
    public static int getWidth(ImageIcon imagen) {
        return imagen.getIconWidth();
    }

    /**
     * Obtiene el alto de una Image
     */
    public static int getHeight(Image imagen) {
        int height = new ImageIcon(imagen).getIconHeight();
        return height;
    }

    /**
     * Obtiene el alto de un IconImage
     */
    public static int getHeight(ImageIcon imagen) {
        return imagen.getIconHeight();
    }
}

Related

  1. getHeight(Image imagen)
  2. getImageByFilename(String filename)
  3. getImageImmediate(final Image image)
  4. GetImagenConTamanioDado(File file, int ancho, int alto)
  5. getImageWithBorder(Image imagen)
  6. getLogoImage()
  7. getManagedImage(Component source, String file, float tint, Color solid)
  8. getNewImageFileChooser()
  9. getOpenSwingImage(String name)