Réponse acceptée !
Salut:
(Solution de twints)
Pour convetir une image vers un objet BufferedImage tu peux faire (solution complexe mais qui gère casi tout):
public BufferedImage createBufferedImage(Image image) { if(image == null) return null; if (image instanceof BufferedImage) return (BufferedImage) image; image = new ImageIcon(image).getImage(); boolean hasAlpha = hasAlpha(image); BufferedImage bimage = null; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); try { int transparency = Transparency.OPAQUE; if (hasAlpha) transparency = Transparency.BITMASK; GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency); } catch (HeadlessException e) { // erreur pas d'ecran } if (bimage == null) { int type = BufferedImage.TYPE_INT_RGB; if (hasAlpha) type = BufferedImage.TYPE_INT_ARGB; bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); } Graphics g = bimage.createGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); return bimage; }
public boolean hasAlpha(Image image) { if (image instanceof BufferedImage) { BufferedImage bimage = (BufferedImage) image; return bimage.getColorModel().hasAlpha(); } PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false); try { pg.grabPixels(); } catch (InterruptedException e) { return false;} ColorModel cm = pg.getColorModel(); return cm.hasAlpha(); }
|