3

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

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;
}
4

1 回答 1

-1

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

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;
}
于 2012-11-17T17:49:00.853 回答