简体   繁体   English

Java ImageJ - 如何使图像透明?

[英]Java ImageJ - how to make an image transparent?

I'd ask, how can I make an image transparent? 我问,我怎样才能使图像透明?

Actually, I tried to read RGB Values from IndexColorModel 实际上,我试图从IndexColorModel读取RGB值

ImageProcessor ip1 = img1.getProcessor();
IndexColorModel indexColor = (IndexColorModel)ip1.getColorModel();

then read a parameters wich i need to create new IndexColorModel 然后读取我需要创建新IndexColorModel的参数

int pixelBits = indexColor .getPixelSize();

int mapSize = cm.getMapSize();
byte[] reds = new byte[mapSize];
cm.getReds(reds);

byte[] greens = new byte[mapSize];
cm.getGreens(greens);

byte[] blues = new byte[mapSize];
cm.getBlues(blues);

and create new IndexColorModel and set it to another ImagePlus with different transparentIndex 并创建新的IndexColorModel并将其设置为另一个具有不同transparentIndex的ImagePlus

indexColor = new IndexColorModel(pixelBits, MapSize, reds, greens, blues, transparentIndex );
ImageProcessor ip3;
ip3.setColorModel(indexColor);

but this is wrong idea... 但这是错误的想法......

Should I use IndexColorModel with cmap and hasAlpha parameters? 我应该使用带有cmap和hasAlpha参数的IndexColorModel吗? How can I do this? 我怎样才能做到这一点?

Thank you in advance for help! 提前感谢您的帮助!

I'd ask, how can I make an image transparent? 我问,我怎样才能使图像透明?

That is too much work to make an image transparent. 要使图像透明,这太过分了。 For a input image of type BufferedImage I would just do: 对于BufferedImage类型的输入image ,我会这样做:

  1. Create a new temporary BufferedImage tmpImage with the same size to image and type: BufferedImage.TYPE_INT_ARGB 创建一个新的临时BufferedImage tmpImage ,其大小与image和类型相同: BufferedImage.TYPE_INT_ARGB
  2. Get it's graphics of the tmpImage and convert it to Graphics2D by up-casting. 获取它的tmpImage Graphics2D通过向上转换将其转换为Graphics2D
  3. set alpha composite to the Graphics2D instance with the transparency level i need 使用我需要的透明度级别将alpha复合设置为Graphics2D实例
  4. Draw the image and get done with it. 绘制图像并完成它。

For example: 例如:

   BufferedImage tmpImg = new BufferedImage(image.getWidth(), image.getHeight(), 
                                                  BufferedImage.TYPE_INT_ARGB);
   Graphics2D g2d = (Graphics2D) tmpImg.getGraphics();
   g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f)); 
            // set the transparency level in range 0.0f - 1.0f 
   g2d.drawImage(image, 0, 0, null);
   image = tmpImg;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM