How to remove white pixels in an image in java

How do I remove the white pixels of the image before loading it into Panel
a way to load the image into the panel:

public  void ajouterImage(File fichierImage) {   
    // desiiner une image à l'ecran 
    try {
        monImage = ImageIO.read(fichierImage);
    } catch (IOException e) {
        e.printStackTrace();
    }
    repaint(); 
}  
+1
source share
2 answers

You cannot remove a pixel for an image from an image, but you can change its color or even make it transparent.

Let's say you have a pixel array as a variable somewhere, and you can set it to your RGB value BufferedImage. An array of pixels will be called 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 to indicate that it 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();
}
+3
source

, , , , , - . colorToAlpha(BufferedImage, Color), BufferedImage a Color BufferedImage, Color .

public static BufferedImage colorToAlpha(BufferedImage raw, Color remove)
{
    int WIDTH = raw.getWidth();
    int HEIGHT = raw.getHeight();
    BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_ARGB);
    int pixels[]=new int[WIDTH*HEIGHT];
    raw.getRGB(0, 0, WIDTH, HEIGHT, pixels, 0, WIDTH);
    for(int i=0; i<pixels.length;i++)
    {
        if (pixels[i] == remove.getRGB()) 
        {
        pixels[i] = 0x00ffffff;
        }
    }
    image.setRGB(0, 0, WIDTH, HEIGHT, pixels, 0, WIDTH);
    return image;
}  

:

BufferedImage processed = colorToAlpha(rawImage, Color.WHITE)
+2

Source: https://habr.com/ru/post/1017184/


All Articles