首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >屏蔽BufferedImage

屏蔽BufferedImage
EN

Stack Overflow用户
提问于 2020-12-13 00:44:02
回答 1查看 101关注 0票数 0

如何在特定部分上重叠两个BufferedImage对象?

假设您有两个BufferedImage对象,并且有一个Rectangle2D对象列表。您希望用矩形上的第二个BufferedImage覆盖第一个BufferedImage。你会怎么做呢?

EN

回答 1

Stack Overflow用户

发布于 2020-12-13 00:53:09

您使用TexturePaint并绘制一个矩形!

首先使用BufferedImage result = new BufferedImage(<width>,<height>,<colorMode>)创建输出图像,然后使用Graphics2D g = result.createGraphics()创建并获取Graphics2D对象。

然后使用g.drawImage(<sourceImage>, null, 0, 0)将完整的源图像(Image1)绘制到输出图像上。

为了覆盖BufferedImage,我们将使用TexturePaintg.fill(rect)方法;您希望使用如下的for循环遍历所有矩形:在TexturePaint的构造函数中,使用BufferedImage.getSubImage(int x, y, width, height)方法在矩形上裁剪图像,以获得所需的覆盖部分(Image2),然后在第二部分,使用Rectangle2D拟合图像,以确保它不会拉伸。之后,使用g.fill(rect)将(带纹理的)矩形绘制到输出图像上。

最后,您可以随心所欲地处理输出图像。在下面的示例中,我使用ImageIO将其导出到.png文件中。

示例:

Image1:https://i.stack.imgur.com/L0Z2k.jpg

Image2:https://i.stack.imgur.com/cmiAR.jpg

输出:https://i.stack.imgur.com/KgNM2.png

代码:

代码语言:javascript
复制
package test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageMasking {

    // Define rectangles
    public static Rectangle2D[] rectangles = new Rectangle[]{
            new Rectangle(0, 0, 350, 50),
            new Rectangle(0, 0, 50, 225),
            new Rectangle(0, 175, 350, 50)
    };

    public static void main(String[] args) throws IOException {
        // Load images
        BufferedImage image1 = ImageIO.read(new File("image1.jpg"));
        BufferedImage image2 = ImageIO.read(new File("image2.jpg"));

        // Create output file
        File out = new File("out.png");
        if (!out.exists()) {
            if (!out.createNewFile()) {
                throw new IOException("createNewFile() returned false");
            }
        }

        // Create output image
        BufferedImage result = new BufferedImage(image1.getWidth(), image1.getHeight(), 
            BufferedImage.TYPE_INT_RGB);
        Graphics2D g = result.createGraphics();

        // Draw image1 onto the result
        g.drawImage(image1, null, 0, 0);

        // Masking
        for (Rectangle2D rect : rectangles){
            // Extract x, y, width and height for easy access in the getSubImage method.
            int x = (int) rect.getX();
            int y = (int) rect.getY();
            int width = (int) rect.getWidth();
            int height = (int) rect.getHeight();
            
            // Setup and draw the rectangle
            TexturePaint paint = new TexturePaint(image2.getSubimage(x,y,width,height), rect);
            g.setPaint(paint);
            g.fill(rect);
        }

        // Export image
        ImageIO.write(result, "PNG", out);
    }
}

//编辑ToDo:为此创建一个简单的访问方法,但由于我现在正在使用手机,所以我不能真正编写代码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65267353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档