【发布时间】:2023-03-02 22:05:01
【问题描述】:
我尝试制作一个简单的程序来加载图像,使其具有蓝色效果,并使其半透明。我通过在图像像素上运行并更改 RGB 的蓝色值和 alpha 值来做到这一点。我成功地为图像制作了漂亮的蓝色效果。但我无法改变图像的不透明度。看来无论我如何改变像素的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);
}
}
【问题讨论】:
-
你能展示你用来定义image1的代码吗?
标签: java image opacity rgb rgba