Image转BufferedImage

[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. }  
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值