簡體   English   中英

BufferedImage從其他BufferedImage設置alpha

[英]BufferedImage set alpha from other BufferedImage

我得到了3個BufferedImages:A,C和D。所有圖像都是帶有顏色和alpha通道的圖像。 C必須在D的Alpha之上,但要具有A的透明層。這個想法是:

  • 將C的alpha通道設置為A的alpha通道
  • 他們的阿爾法

我使用new BufferedImage().createGraphics().drawImage(D).drawImage(C);進行Alpha混合new BufferedImage().createGraphics().drawImage(D).drawImage(C); 但是,如何設置C的alpha?

我願意使用BufferedImage提出任何建議,但我更喜歡那些不會在所有像素上進行迭代並手動進行計算以提高性能的建議(希望如此)。

對於具有統一Alpha的圖像,您可以執行以下操作:

BufferedImage bim=null;
try {
  bim=ImageIO.read(new File("..."));
}
catch (Exception ex) { System.err.println("error in bim "+ex); }
int wc=bim.getWidth(), hc=bim.getHeight();
  BufferedImage b=new BufferedImage(wc, hc, BufferedImage.TYPE_INT_ARGB);
  b.getGraphics().drawImage(bim, 0, 0, null);
  BufferedImage b2=new BufferedImage(wc, hc, BufferedImage.TYPE_INT_ARGB);
  RescaleOp no=new RescaleOp(new float[]{1f, 1, 1, 1f}, new float[]{0, 0, 0, -50}, null);
  b2=no.filter(b, null);

  BufferedImage b3=new BufferedImage(wc, hc, BufferedImage.TYPE_INT_ARGB);
  b3.getGraphics().drawImage(bim, 0, 0, null);
  float offset=(b2.getRGB(0, 0)&0xff000000)>>24, a=(b3.getRGB(0, 0)&0xff000000)>>24;
  no=new RescaleOp(new float[]{1, 1, 1, 1}, new float[]{0, 0, 0, -a+offset}, null);
  b3=no.filter(b3, null);

現在b3具有b2的alpha。

對於具有不均勻Alpha的圖像,您必須每個像素工作一個像素。

暫無
暫無

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

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