WesleyyC / Image-Editor

A command line image editor written in Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Combine two images with an alpha image on top of a background image

SerinaTan opened this issue · comments

Suddenly came up with a potentially new Editor function... It might not be feasible but I will open a ticket for it.

Some images support alpha so it can become transparent when we paste it on top of another image. It would be fun to combine 2+ images and see through the top layer image.

I have been working on this and the basic idea is that create a transparent overlay BufferedImage and then use Graphics to combine the two images together.

However, I am having trouble creating a transparent layer with a second image. They way I do it is:

    private BufferedImage transparentImage(BufferedImage image)
    {
        BufferedImage tmpImg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = (Graphics2D) tmpImg.getGraphics();
        g2d.setComposite(AlphaComposite.SrcOver.derive(0.1f));
        // set the transparency level in range 0.0f - 1.0f
        g2d.drawImage(image, 0, 0, null);
        try{

            ImageIO.write(tmpImg, "jpg", new File("/Users/wesley/Desktop/", "overlay.jpg "));
                        // For testing
        } catch (IOException e){
            System.out.println(e);
        }
        return tmpImg;
    }