在旧图像java上绘制新图像



我需要在旧图像上绘制一个新图像。我首先在BufferedImage中打开了这两个图像,并将它们的白色背景更改为透明。然后我从旧图像的bufferedImage中得到了一个Graphics2D对象,并称之为Graphics2D类的drawImage方法。然后我将旧图像保存到磁盘上。当我打开保存的图像时,我发现只有白色背景的旧图像变为透明。有人能告诉我我的代码有什么错误吗?或者我如何修复我的错误?

    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);

我的makeWhiteTransparent方法如下:

    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;
    }

我尝试将graphics.drawImage(newImage,null,0,0)更改为graphics.draw Image(newImage,0,0,null),并按照建议将TYPE_4BYTE_ABGR更改为TYPE_INT_ARGB,但没有成功。错误仍然存在。

这需要更改:

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

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

您使用的drawImage版本错误-请检查http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html

--

还将类型type_4BYTE_ABGR更改为type_INT_ARGB

--

以下是它对我的作用:

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);

我终于得到了问题的答案。我所要做的就是创建一个新的BufferedImage并在上面绘制两个图像

        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);

最新更新