java java中的叠加图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14241944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:34:51  来源:igfitidea点击:

overlay images in java

javaimageswingoverlayjbutton

提问by Alejandro Cabano

I hope you can give me some advices to solve my problem. I need to overlay many images on a button.but the problem is, this is the base image (tooth): (http://i.imgur.com/7tIcP.gif)

我希望你能给我一些建议来解决我的问题。我需要在一个按钮上叠加许多图像。但问题是,这是基本图像(牙齿):(http://i.imgur.com/7tIcP.gif)

my first image is this: http://i.imgur.com/FYuD8.gifand then I put this: http://i.imgur.com/mWz9c.gifthe first image overlaps the second so I just can see only the second image...

我的第一张图片是这样的:http: //i.imgur.com/FYuD8.gif然后我把这个:http: //i.imgur.com/mWz9c.gif第一张图片与第二张重叠,所以我只能看到第二张图...

maybe you will tell me that one option is change the order of the image before overlay, but the user will select what will be the first, maybe just want the first image, but in other cases user will put the first AND then the second or vice versa...

也许你会告诉我,一个选项是在叠加之前更改图像的顺序,但用户将选择第一个图像,也许只想要第一个图像,但在其他情况下,用户将放置第一个然后第二个或反之亦然...

my code is this:

我的代码是这样的:

    BufferedImage large=null;
    large = ImageIO.read(new File("firstimage.gif"));

    BufferedImage small=null;

    small = ImageIO.read(new File("secondimage.gif"));

    int w = Math.max(large.getWidth(), small.getWidth());
    int h = Math.max(large.getHeight(), small.getHeight());

    BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

    // paint both images, preserving the alpha channels
    Graphics g = combined.getGraphics();
    g.drawImage(large, 0, 0, null);
    g.drawImage(small, 0, 0, null);

    ImageIO.write(combined, "PNG", new File("twoInOne.png"));

    ImageIcon icon1 = new ImageIcon(combined);
    jbutton1.setIcon(icon1);

Maybe is a format of the images issue, or my code, but I prefer that you guys can help me with this problem thank you.

也许是图像格式的问题,或者我的代码,但我更希望你们能帮我解决这个问题,谢谢。

Now I uploaded the 3 images: I skip the base image (tooth) cause I dont think it will be the problem in there.

现在我上传了 3 张图片:我跳过了基本图片(牙齿),因为我认为这不是那里的问题。

回答by trashgod

I need to edit my images to get transparent background?

我需要编辑我的图像以获得透明背景吗?

To make a particular color transparent, you can iterate through the pixels of a BufferedImageor use a suitable LookupOp. For the latter, see the articles cited here. You can then combine the images using drawImage(). The default composite rule, AlphaComposite.SRC_OVER, should be satisfactory; if not, you can change it, as shown here.

要使特定颜色透明,您可以遍历 a 的像素BufferedImage或使用合适的LookupOp. 对于后者,请参阅此处引用的文章。然后,您可以使用 组合图像drawImage()。默认的复合规则,AlphaComposite.SRC_OVER,应该是令人满意的;如果没有,你可以改变它,如这里

回答by Freezerburn

Your code should be fine for combining two images together. However, like you said, your two images are of the same size, and they do not seem to have any transparency. This will cause whatever image is drawn second to always "overwrite" the first image in the newly-combined image.

您的代码应该可以将两个图像组合在一起。但是,正如您所说,您的两个图像大小相同,并且它们似乎没有任何透明度。这将导致第二个绘制的图像总是“覆盖”新组合图像中的第一个图像。

The solution you probably want for this, is to break the various pieces you want to overlay on top of each other out into separate, smaller images. With your images, it looks like you want to have various overlays on top of a tooth to display various pieces of information. You'll want to have three things in this case: an image of a tooth, an image containing the red overlay, and an image containing the blue overlay. All three of these images should have a transparent, and not white, background so that they don't overwrite colors in any previously-drawn image. When you do this, you'll want to draw the tooth, then overlay 1 (red/blue) then overlay 2 (red/blue). This should get you the output you're looking for.

您可能需要的解决方案是将您想要叠加的各个部分分解成单独的、较小的图像。对于您的图像,您似乎希望在牙齿顶部有各种叠加层以显示各种信息。在这种情况下,您需要三样东西:牙齿图像、包含红色覆盖层的图像和包含蓝色覆盖层的图像。所有这三个图像都应该有一个透明的,而不是白色的背景,这样它们就不会覆盖任何先前绘制的图像中的颜色。执行此操作时,您需要绘制牙齿,然后叠加 1(红色/蓝色),然后叠加 2(红色/蓝色)。这应该会让你得到你正在寻找的输出。

回答by Ken Chan

The key is to set the alpha to float value, say two layer, set alpha to 0.5, three layer, set alpha 0.33, four layer, set alpha 0.25 ... Anyway, here is the code example

关键是将alpha设置为浮点值,比如说两层,设置alpha为0.5,三层,设置alpha 0.33,四层,设置alpha 0.25......无论如何,这里是代码示例

try
{
    BufferedImage imgA = ImageIO.read(new File(imgAPath, token));
    BufferedImage imgB = ImageIO.read(new File(imgBPath, token));

    if (imgA.getWidth() == imgB.getWidth() && imgA.getHeight() == imgB.getHeight()) 
    {
        float alpha = 0.5f;
        int compositeRule = AlphaComposite.SRC_OVER;
        AlphaComposite ac;
        int imgW = imgA.getWidth();
        int imgH = imgA.getHeight();
        BufferedImage overlay = new BufferedImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = overlay.createGraphics();
        ac = AlphaComposite.getInstance(compositeRule, alpha);
        g.drawImage(imgA,0,0,null);
        g.setComposite(ac);
        g.drawImage(imgB,0,0,null);
        g.setComposite(ac);
        ImageIO.write(overlay, "PNG", new File(logFolder, browser+"__"+token));
        g.dispose();
    }
    else
    {
        System.err.println(token+" dymension not match ");
    }
}
catch (IOException e)
{
}