1 29 30 package nextapp.echo2.webcontainer.image; 31 32 import java.awt.Graphics ; 33 import java.awt.Image ; 34 import java.awt.image.PixelGrabber ; 35 import java.awt.image.BufferedImage ; 36 37 import javax.swing.ImageIcon ; 38 39 class ImageToBufferedImage { 40 41 static BufferedImage toBufferedImage(Image image) { 42 if (image instanceof BufferedImage ) { 43 return (BufferedImage ) image; 45 } 46 47 image = new ImageIcon (image).getImage(); 49 50 int type = hasAlpha(image) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; 51 BufferedImage bufferedImage = new BufferedImage (image.getWidth(null), image.getHeight(null), type); 52 Graphics g = bufferedImage.createGraphics(); 53 g.drawImage(image, 0, 0, null); 54 g.dispose(); 55 56 return bufferedImage; 57 } 58 59 static boolean hasAlpha(Image image) { 60 PixelGrabber pg = new PixelGrabber (image, 0, 0, 1, 1, false); 61 try { 62 pg.grabPixels(); 63 } catch (InterruptedException ex) { } 64 return pg.getColorModel().hasAlpha(); 65 } 66 67 70 private ImageToBufferedImage() { } 71 } 72 | Popular Tags |