我会问,我怎样才能使图像透明?
实际上,我试图从IndexColorModel中读取RGB值
ImageProcessor ip1 = img1.getProcessor();
IndexColorModel indexColor = (IndexColorModel)ip1.getColorModel();
然后读取我需要创建新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);
并创建新的IndexColorModel并将其设置为另一个具有不同transparentIndex
的ImagePlusindexColor = new IndexColorModel(pixelBits, MapSize, reds, greens, blues, transparentIndex );
ImageProcessor ip3;
ip3.setColorModel(indexColor);
但这是错误的想法......
我应该将IndexColorModel与cmap和hasAlpha参数一起使用吗?我怎么能这样做?
提前感谢您的帮助!
答案 0 :(得分:2)
我会问,我怎样才能使图像透明?
要使图像透明,这太过分了。对于image
类型的输入BufferedImage
,我只会这样做:
BufferedImage
大小相同的新临时tmpImage
image
并输入:BufferedImage.TYPE_INT_ARGB
tmpImage
的图片并通过向上转换将其转换为Graphics2D
。例如:
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;