Store transparent image indexed in a BufferedImage

0

Please I need to know how I can create a BufferedImage that stores a transparent image with color% co_of% of 256x256 pixels.

Please, if someone can enlighten me with this doubt, I would really appreciate it.

Greetings.

    
asked by Adriana Hernández 01.08.2017 в 19:35
source

2 answers

0

With the function that I am going to share, you can upload an image of your resources from your JAVA project and give it the size you want:

public Image obtenerImagen(String rutaImagen, int alto, int ancho) throws IOException{
    BufferedImage perfil = null;
    Image imagen = null;
    try{
        perfil = ImageIO.read(getClass().getResource(rutaImagen));
        imagen = perfil.getScaledInstance(alto, ancho, Image.SCALE_SMOOTH);
    }catch(IOException e){
        perfil = ImageIO.read(getClass().getResource("assets/FotoErrorOporDefecto.png"));
        imagen = perfil.getScaledInstance(alto, ancho, Image.SCALE_SMOOTH);
    }
    return imagen;
}  

How would you use it? ... for example:

JLabel example = new JLabel(new ImageIcon(obtenerImagen("/assets/chichas.png",250,250)));

Just make sure you create an additional package (you can call it assets, as is usually done) within the project, and save all your images there.

    
answered by 02.08.2017 в 04:21
0

Just specify the type of image

BufferedImage(int width, int height, int imageType)

width , and height are measured in pixels independently of the internal representation (eg two pixels could be packed in the same byte), imageType is the type of image, Java supports images of indexed type

  

public static final int TYPE_BYTE_INDEXED

     

Represents an indexed byte image. When this type is used as the   imageType argument to the BufferedImage constructor that takes an   imageType argument but not ColorModel argument, an IndexColorModel is   created with a 256-color 6/6/6 color cube palette with the rest of the   colors from 216-255 populated by grayscale values in the default sRGB   ColorSpace.

So to create a BufferedImage of 256x256

BufferedImage bf = new BufferedImage(256, 256, BufferedImage.TYPE_BYTE_INDEXED)
    
answered by 02.08.2017 в 05:11