如何在将图像加载到面板
之前删除图像的白色像素
在面板中加载图像的方法是:
public void ajouterImage(File fichierImage) {
// desiiner une image à l'ecran
try {
monImage = ImageIO.read(fichierImage);
} catch (IOException e) {
e.printStackTrace();
}
repaint();
}
最佳答案
您无法从图像中删除像素,但您确实可以更改它的颜色,甚至使其透明。
假设您在某处有一个像素数组作为变量,您可以为其指定 BufferedImage
的 RGB 值。像素数组将被称为pixels
:
try {
monImage = ImageIO.read(fichierImage);
int width = monImage.getWidth();
int height = monImage.getHeight();
pixels = new int[width * height];
image.getRGB(0, 0, width, height, pixels, 0, width);
for (int i = 0; i < pixels.length; i++) {
// I used capital F's to indicate that it's the alpha value.
if (pixels[i] == 0xFFffffff) {
// We'll set the alpha value to 0 for to make it fully transparent.
pixels[i] = 0x00ffffff;
}
}
} catch (IOException e) {
e.printStackTrace();
}
关于java - 如何用java删除图像中的白色像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23980554/