如何将透明度应用于bufferedimage java

时间:2012-11-17 17:33:23

标签: java transparency bufferedimage

我正在尝试绘制一个采用bufferedImage的方法,使其半透明,然后返回生成的bufferedImage。从那以后我可以画它。但是我的代码无法绘制任何内容。认为我很接近......

public BufferedImage ApplyTransparency(BufferedImage image)
{
    BufferedImage dest = new BufferedImage(overlay1.getWidth(), overlay1.getHeight(),
            BufferedImage.TYPE_INT_ARGB);

    Graphics2D g3 = dest.createGraphics(); 
    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.DST_IN, 0.9F);
    g3.setComposite(ac);
    g3.drawImage(overlay1, 0, 0, null);

    return dest;
}

1 个答案:

答案 0 :(得分:0)

您可以将图像绘制到新图像上,调整其透明度,如下所示

public Raster transform(Image img, float alpha) {
    BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    bi.getGraphics().drawImage(img, 0, 0, null);
    RasterOp rop = new RescaleOp(new float[] { 1.0f, 1.0f, 1.0f, alpha }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, null);
    Raster result = rop.filter(bi.getData(), null);
    return result;
}