0

I need to draw a new image over old image. I first opened both images in BufferedImage and changed their white background to transparent. Then I got a Graphics2D object from the bufferedImage of old image and called drawImage method of Graphics2D class. I then saved the old image to disk. When I open the saved image I find only the old image with white background changed to transparent. Can anyone suggest me what is error with my code or how can I get to fix my error ?

    BufferedImage newImage = ImageIO.read(new File("new.png"));
    BufferedImage oldImage = ImageIO.read(new File("old.png"));

    newImage = makeWhiteTransparent(newImage);
    oldImage = makeWhiteTransparent(oldImage);

    Graphics2D graphics = (Graphics2D) oldImage.getGraphics();
    graphics.drawImage(newImage,null, 0,0);

    File outputImage = new File("merged.png");
    ImageIO.write(oldImage, "png", outputImage);

My makeWhiteTransparent method goes like this:

    public static BufferedImage makeWhiteTransparent(BufferedImage img){
        BufferedImage dst = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
        dst.getGraphics().drawImage(img, 0, 0, null);
        int markerRGB = Color.WHITE.getRGB() | 0xFF000000;
        int width = dst.getWidth();
        int height = dst.getHeight();
        for(int x = 0; x < width; x++){
            for(int y = 0; y < height; y++){
                int rgb = dst.getRGB(x, y);
                if ( ( rgb | 0xFF000000 ) == markerRGB ) {
                    int value = 0x00FFFFFF & rgb;
                    dst.setRGB(x, y, value); 
                }
            }
        }
        return dst;
    }

I tried changing graphics.drawImage(newImage, null,0,0) to graphics.drawImage(newImage, 0,0, null) and also changing TYPE_4BYTE_ABGR to TYPE_INT_ARGB as suggested but it did nothing. The error still exists.

Bijay Regmi
  • 90
  • 1
  • 15

2 Answers2

1

This needs to be changed:

graphics.drawImage(newImage,null, 0,0);

to

graphics.drawImage(newImage, 0,0, null);

you are using the wrong version of drawImage - check http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html

--

Change also the type TYPE_4BYTE_ABGR to TYPE_INT_ARGB

--

Here's how it works for me:

public BufferedImage makeWhiteTransparent(BufferedImage img){
    BufferedImage dst = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
    dst.getGraphics().drawImage(img, 0, 0, null);
    int markerRGB = 0x00ffffff; // Color.WHITE.getRGB() | 0xFF000000;
    int width = dst.getWidth();
    int height = dst.getHeight();
    for(int x = 0; x < width; x++){
        for(int y = 0; y < height; y++){
            int rgb = dst.getRGB(x, y)&0x00ffffff;
            if ( rgb  == markerRGB ) {
                int value = 0x00FFFFFF & rgb;
                dst.setRGB(x, y, value); 
            }
        }
    }
    return dst;
}

bim = makeWhiteTransparent(bim);
bim2 = makeWhiteTransparent(bim2);

Graphics2D graphics = (Graphics2D) bim.getGraphics();
graphics.drawImage(bim2,0,0, null);

g2.drawImage(bim, w/2-wc/2, h/2-hc/2, null);
gpasch
  • 2,672
  • 3
  • 10
  • 12
0

I got the answer to my question finally. All I had to do was create a new BufferedImage and draw two images over it. Below is the code that works as expected:

        BufferedImage newImage = ImageIO.read(new File("new.png"));
        BufferedImage oldImage = ImageIO.read(new File("old.png"));


        oldImage = makeWhiteTransparent(oldImage);
        newImage = makeWhiteTransparent(newImage);

        int width = Math.max(newImage.getWidth(),  oldImage.getWidth());
        int height = Math.max(newImage.getHeight(), oldImage.getHeight());
        BufferedImage combined = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        Graphics graphics = combined.getGraphics();

        graphics.drawImage(oldImage, 0, 0, null);
        graphics.drawImage(newImage, 0, 0, null);

        File outputImage = new File("merged.png");
        ImageIO.write(combined, "PNG", outputImage);
Bijay Regmi
  • 90
  • 1
  • 15