Transparence

Résolu/Fermé
Ombitious_Developper Messages postés 2333 Date d'inscription samedi 28 février 2004 Statut Membre Dernière intervention 26 juillet 2013 - 8 sept. 2006 à 22:50
cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 - 9 sept. 2006 à 09:45
Salut

J'ai image que je veux rendre son fond transparent. Comment faire?

Et Merci ...

3 réponses

cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
9 sept. 2006 à 09:45
Code trouvé sur le forum de developpez (à tester !). Ce code permet de remplacer les pixels de couleur blanches par des pixels de zone alpha (zone transparente de l'image) :


import java.awt.*;

import java.awt.image.*;

import java.io.*;

import java.net.*;

import javax.imageio.*;

import javax.swing.*;


class Transparent

{

public static void main(String[] args) throws IOException

{

//-------------------------variables--------------------------

URL url = new URL("http://www.google.fr/images/logo_sm.gif");

BufferedImage image = ImageIO.read(url);

image = convert(image);

//---------------------------body-----------------------------

JFrame $f = new JFrame("Transparence");

JLabel label = new JLabel(new ImageIcon(image));

label.setOpaque(true);

$f.getContentPane().add(label);

$f.pack();

$f.setLocationRelativeTo(null);

$f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

$f.setVisible(true);

}


static BufferedImage convert(BufferedImage image)

{

ColorModel cm = image.getColorModel();

if (cm instanceof IndexColorModel) {

IndexColorModel icm = (IndexColorModel) cm;

int[] cmap = new int[icm.getMapSize()];

icm.getRGBs(cmap); //recup la couleur en format RGB

int white = 0xFFFFFFFF; //RGBA format

int i;

for(i=0; i<cmap.length; ++i)

if (cmap[i] == white)

break;

if (i != cmap.length)

{

cmap[i] = 0; //transparent

IndexColorModel newicm = new IndexColorModel(8, cmap.length, cmap,0, true,


Transparency.BITMASK, DataBuffer.TYPE_BYTE);

image = new BufferedImage(newicm, image.getRaster(), false, null);

}

else

System.out.println("couleur blanche pas trouvee");

} else

System.out.println("ce n'est pas un model de couleusr indexe");

return image;

}

}
3
cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
9 sept. 2006 à 08:45
Tout dépend le format de ton image, par exemple, s'il s'agit d'une image au format png avec fond transparent, la transparence se fait toute seule...
0
Ombitious_Developper Messages postés 2333 Date d'inscription samedi 28 février 2004 Statut Membre Dernière intervention 26 juillet 2013 38
9 sept. 2006 à 08:57
Salut :

Comme la transparence est automatique avec les images PNG.

Comment faire avec les autres formats supportés par Java (GIF, JPEG)?

Et Merci ... 
0
Rejoignez-nous