簡體   English   中英

如何使用java從圖像中刪除白色矩形

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

我已經厭倦了以下代碼來生成拇指指甲但是當我將它放在另一個圖像上時,縮略圖有一個白色矩形。 如何使用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); 
  graphics2D.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;
  }

即使我刪除這三行,我也會得到黑色矩形。

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

如何使這個圖像透明? 請有人幫助我。

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

通過設置Composite 這里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

那應該是:

..BufferedImage.TYPE_INT_ARGB

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM