我試圖製作一個簡單的程序,用於加載圖像,使其成爲藍色效果,並使其成爲半透明的。我通過在圖像像素上運行並更改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