Image转BufferedImage

来源:互联网 发布:淘宝店铺转让合同 编辑:程序博客网 时间:2024/05/22 05:25
[java] view plain copy
  1. import java.awt.Graphics;  
  2. import java.awt.GraphicsConfiguration;  
  3. import java.awt.GraphicsDevice;  
  4. import java.awt.GraphicsEnvironment;  
  5. import java.awt.HeadlessException;  
  6. import java.awt.Image;  
  7. import java.awt.Toolkit;  
  8. import java.awt.Transparency;  
  9. import java.awt.image.BufferedImage;  
  10. import java.io.File;  
  11. import java.io.IOException;  
  12.   
  13. import javax.imageio.ImageIO;  
  14. import javax.swing.ImageIcon;  
  15.   
  16. public class TestImage {  
  17.       
  18.     public static void main(String[] args) throws IOException{  
  19.         Image img = Toolkit.getDefaultToolkit().getImage("C:\\google.jpg");  
  20.         BufferedImage bi_scale = toBufferedImage(img);  
  21.         ImageIO.write(bi_scale, "jpg",new File("C:\\2.jpg"));  
  22.     }  
  23.             
  24.     public static BufferedImage toBufferedImage(Image image) {  
  25.         if (image instanceof BufferedImage) {  
  26.             return (BufferedImage)image;  
  27.          }  
  28.         
  29.         // This code ensures that all the pixels in the image are loaded  
  30.          image = new ImageIcon(image).getImage();  
  31.         
  32.         // Determine if the image has transparent pixels; for this method's  
  33.         // implementation, see e661 Determining If an Image Has Transparent Pixels  
  34.         //boolean hasAlpha = hasAlpha(image);  
  35.         
  36.         // Create a buffered image with a format that's compatible with the screen  
  37.          BufferedImage bimage = null;  
  38.          GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();  
  39.         try {  
  40.             // Determine the type of transparency of the new buffered image  
  41.             int transparency = Transparency.OPAQUE;  
  42.            /* if (hasAlpha) { 
  43.              transparency = Transparency.BITMASK; 
  44.              }*/  
  45.         
  46.             // Create the buffered image  
  47.              GraphicsDevice gs = ge.getDefaultScreenDevice();  
  48.              GraphicsConfiguration gc = gs.getDefaultConfiguration();  
  49.              bimage = gc.createCompatibleImage(  
  50.              image.getWidth(null), image.getHeight(null), transparency);  
  51.          } catch (HeadlessException e) {  
  52.             // The system does not have a screen  
  53.          }  
  54.         
  55.         if (bimage == null) {  
  56.             // Create a buffered image using the default color model  
  57.             int type = BufferedImage.TYPE_INT_RGB;  
  58.             //int type = BufferedImage.TYPE_3BYTE_BGR;//by wang  
  59.             /*if (hasAlpha) { 
  60.              type = BufferedImage.TYPE_INT_ARGB; 
  61.              }*/  
  62.              bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);  
  63.          }  
  64.         
  65.         // Copy image to buffered image  
  66.          Graphics g = bimage.createGraphics();  
  67.         
  68.         // Paint the image onto the buffered image  
  69.          g.drawImage(image, 00null);  
  70.          g.dispose();  
  71.         
  72.         return bimage;  
  73.     }  
  74. }  
原创粉丝点击