我试图制作一个简单的程序,用于加载图像,使其成为蓝色效果,并使其成为半透明的。我通过在图像像素上运行并更改RGB的蓝色值和alpha值来完成此操作。我成功地为图像创造了一种不错的蓝色效果。但我无法设法改变图像不透明度。似乎无论如何改变像素的alpha值,都不会影响图像。在RGBA中使用alpha制作图像的不透明度
这里是我的代码:
try {
image1 = ImageIO.read(new File("image1.png"));
} catch(IOException e) {e.printStackTrace();}
for(int x=0;x<image1.getWidth();x++) {
for(int y=0;y<image1.getHeight();y++) {
int rgb = image1.getRGB(x, y);
Color color = new Color(rgb);
int r = color.getRed();
int g = color.getGreen();
int b = color.getBlue();
int a = color.getAlpha();
System.out.println(a);
a= 100;
if(b<155)
b+=100;
else
b=255;
color = new Color(r,g,b,a);
image1.setRGB(x, y, color.getRGB());
}
}
更新: 我也尝试过这一点。仍然不能正常工作:
for(int x=0;x<image1.getWidth();x++) {
for(int y=0;y<image1.getHeight();y++) {
int rgb = image1.getRGB(x, y);
Color color = new Color(rgb,true);
int r = color.getRed();
int g = color.getGreen();
int b = color.getBlue();
int a = color.getAlpha();
a= 100;
if(b<155)
b+=100;
else
b=255;
rgb = rgb | b;
rgb = rgb & 0x33ffffff;
image1.setRGB(x, y, rgb);
}
}
你能告诉您可以使用它定义图像1的代码? – 2013-04-22 20:48:37
我编辑代码 – Shelef 2013-04-22 20:51:22