在java中叠加图像 [英] overlay images in java

查看:350
本文介绍了在java中叠加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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/FYuD8.gif
然后我把它:
http://i.imgur.com/mWz9c.gif
第一张图片与第二张图片重叠,所以我只能看到第二张图片...

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

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

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...

我的代码是这样的:

    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.

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

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

推荐答案


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

I need to edit my images to get transparent background?

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

To make a particular color transparent, you can iterate through the pixels of a BufferedImage or 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.

这篇关于在java中叠加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆