🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.lenguajes.java
Article #639

Re: ¿Obtener un byte[] desde un ImageIcon?

#639
From: Fede
Date: Thu, 11 Jan 2007 20:31
119 lines
4767 bytes
Hola... esta clase administra imagenes e iconos.... fijate los metodos
"crearImagen" y "bytesDesdeInputStream" quizas te sirvan de inspiracion
para resolver tu problema....
el segundo metodo mencionado anteriormente codifica un InputStream en
un byte[] y luego a partir de este arreglo construyo
las imagenes utilizando la libreria "java.awt.Toolkit". Se que no es
una solucion exacta de tu problema pero espero te sirva de algo.
Suerte. Federico

--------------------------------------------------------------------------------------------------------

/*
 * @(#)ImgAdmin.java
 */

import java.util.HashMap;
import javax.swing.ImageIcon;
import java.awt.Image;
import java.awt.MediaTracker;
import java.io.IOException;


/**
 * Administrador deimagenes.
 *
 */
public class ImgAdmin {

	/** Guarda instancias de los iconos que se estan utilizando. */
	private static HashMap iconos = new HashMap();

	/** Guarda instancias de las imagenes que se estan utilizando. */
	private static HashMap imagenes = new HashMap();

	/** El mobre del directorio base donde estan las imagenes. */
	private static final String DIR_BASE = "recursos/";

	/** La extension de todas las imagenes. */
	private static final String EXTENSION = ".png";

	/**
	 * Constructor.
	 */
	public ImgAdmin(){
	}

	/**
	 * Devuelve un icono.
	 *
	 * @param NOMBRE Es el nombre del archivo de imagen que se
	 * 				 utiliza para crear el icono, se asume que
	 * 				 el formato del archivo es PNG.
	 **/
	public static ImageIcon crearIcono(String NOMBRE) {
		String url = DIR_BASE+NOMBRE+EXTENSION;
		ImageIcon i = (ImageIcon) iconos.get(url);
		if(i != null) return i;
		i = new ImageIcon(url);
		if (i.getImageLoadStatus()==MediaTracker.COMPLETE){
		    iconos.put(url, i);
		}else{
		    System.out.println("Error cargando el archivo "+url);
		}
		return i;
	}

    /**
	 * Devuelve una imagen.

	 * @param NOMBRE Es el nombre del archivo que se
	 * 				 utiliza para crear la imagen, se
	 * 				 asume que el formato del archivo
	 *               es PNG.
     */
    public Image crearImagen(String NOMBRE) {
		String url = DIR_BASE+NOMBRE+EXTENSION;
		Image i = (Image) imagenes.get(url);
		if(i != null) return i;
        try {
            ClassLoader cl = this.getClass().getClassLoader();
            java.io.InputStream in = cl.getResourceAsStream(url);
            byte[] data = bytesDesdeInputStream(in, in.available());
            i = java.awt.Toolkit.getDefaultToolkit().createImage(data);
            imagenes.put(url, i);
            return i;
        } catch (Exception ex){
        	ex.getMessage();
        	ex.printStackTrace();
        }
        return null;
    }

    /**
     * Codifica un archivo de imagen en
     * un arreglo de bytes, el archivo
     * de imagen debe estar en alguno de
     * los formatos soportados por
     * java.awt.Toolkit, como ser GIF
     * JPEG o PNG.
     */
    private static byte[] bytesDesdeInputStream(java.io.InputStream in,
int length){
        java.io.DataInputStream din = new java.io.DataInputStream(in);
        byte[] bytecodes = new byte[length];
        try {
            din.readFully(bytecodes);
            if (din != null) din.close();
        }catch (IOException ex){
        	ex.getMessage();
        	ex.printStackTrace();
        }
        return bytecodes;

    }

}

--------------------------------------------------------------------------------------------------------
Technical Details
Message-ID: <1168547488.770049.20600@k58g2000hse.googlegroups.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!postnews.google.com!k58g2000hse.googlegroups.com!not-for-mail
References: <emv2t5$6bo$1@cagarruta.escomposlinux.org>

Show Full Headers
Complete Article Headers
X-Google-Thread: 1116f3,ab4a5db2417b8aad
X-Google-Attributes: gid1116f3,public
X-Google-Language: SPANISH,ASCII-7-bit
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!postnews.google.com!k58g2000hse.googlegroups.com!not-for-mail
From: "Fede" <fedevg@gmail.com>
Newsgroups: es.comp.lenguajes.java
Subject: Re: ¿Obtener un byte[] desde un ImageIcon?
Date: 11 Jan 2007 12:31:29 -0800
Organization: http://groups.google.com
Lines: 120
Message-ID: <1168547488.770049.20600@k58g2000hse.googlegroups.com>
References: <emv2t5$6bo$1@cagarruta.escomposlinux.org>
NNTP-Posting-Host: 200.59.137.190
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
X-Trace: posting.google.com 1168547491 1719 127.0.0.1 (11 Jan 2007 20:31:31 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Thu, 11 Jan 2007 20:31:31 +0000 (UTC)
In-Reply-To: <emv2t5$6bo$1@cagarruta.escomposlinux.org>
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Win98; es-AR; rv:1.7.12) Gecko/20050919 Firefox/1.0.7,gzip(gfe),gzip(gfe)
Complaints-To: groups-abuse@google.com
Injection-Info: k58g2000hse.googlegroups.com; posting-host=200.59.137.190;
   posting-account=L6lWgg0AAACbnoEm_6857TD0vEKHbVpy