【问题标题】:Drawing new image over old image java在旧图像上绘制新图像java
【发布时间】:2016-05-12 18:23:21
【问题描述】:

我需要在旧图像上绘制新图像。我首先在 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.drawImage(newImage, 0,0, null) 并将 TYPE_4BYTE_ABGR 更改为 TYPE_INT_ARGB,但它什么也没做。错误依然存在。

【问题讨论】:

    标签: java graphics bufferedimage graphics2d


    【解决方案1】:

    这需要改变:

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

    【讨论】:

    • 我尝试使用 graphics.drawImage(newImage, 0, 0, null) 但问题没有解决。
    • 你能解释一下 w、wc、h 和 hc 是什么意思吗?
    • 它只是宽度无关紧要,只需将其设置为 0, 0
    【解决方案2】:

    我终于得到了我的问题的答案。我所要做的就是创建一个新的 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-26
      • 2014-02-26
      • 1970-01-01
      • 2020-08-01
      • 1970-01-01
      相关资源
      最近更新 更多