开发者

How to get rid of White rectangle from an image using java

I had tired the following code to generate the thumb nail but when I place it on another image, the thumbnail has a white rectangle. How can I remove it using java graphics 2D?

  Image image = javax.imageio.ImageIO.read(new File(originalFile));       
  BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
  Graphics2D graphics2D = thumbImage.createGraphics();
  graphics2D.setBackground(Color.WHITE);
  graphics2D.setPaint(Color.WHITE); 
  graph开发者_如何学JAVAics2D.fillRect(0, 0, thumbWidth, thumbHeight);       
  graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);   
  File file = new File(thumbnailFile);
  if(javax.imageio.ImageIO.write(thumbImage, "png", file))
      return file;
  }

Even if i remove these three lines I am getting black rectangle.

 graphics2D.setBackground(Color.WHITE);
 graphics2D.setPaint(Color.WHITE); 
 graphics2D.fillRect(0, 0, thumbWidth, thumbHeight);   

How to get this image as transparent? Please some one help me out.


 Color transparent = new Color(0,0,0,0)

by setting Composite . here : AlphaComposite

 // Create an image that supports arbitrary levels of transparency
 Graphics2D g2d = (Graphics2D) image.getGraphics();
 BufferedImage thumbImage = g2d.getDeviceConfiguration().createCompatibleImage(
    thumbWidth, thumbHeight, Transparency.TRANSLUCENT);


..BufferedImage.TYPE_INT_RGB

That should be:

..BufferedImage.TYPE_INT_ARGB
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜