Search code examples
javaimagechromakey

Easy way to remove chromakey from image Java


Is there any easy way to remove the chromakey from the image by java? I've tried to read about openCV and that's to hard to learn and use.


Solution

  • The easiest solution with would be to do the manipulation Pixel by Pixel. For that to work you need an Image with an Alpha Channel. Pass TYPE_INT_ARGB to the Constructor of BufferedImage after width and height.

    Then Copy in two nested for Loops each pixel to the new Image like this:

    BufferedImage newImg = new BufferedImage(origImg.getWidth(), origImg.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Color background = new Color(0,0,0,0); //RGBA
    for (int x = 0; x < origImg.getWidth(); x++) {          
       for (int y=0; y < origImg.getHeight(); y++) {
          Color newColor = new Color(origImg.getRGB(x, y) | 0xff000000, true);
    
          if(/*check for chromakey color*/)
            newImg.setRGB(x, y, background.getRGB())
          else
            newImg.setRGB(x, y, newColor.getRGB());            
       }      
    }
    
    

    This assumes you dont have an alpha component in the original Image, because it will be overwritten by the |0xff000000 with 255.