Java 将两个BuffereImage并排复制到一个映像中

Java 将两个BuffereImage并排复制到一个映像中,java,image,image-processing,bufferedimage,Java,Image,Image Processing,Bufferedimage,我有两个图像,我想将这两个图像复制到一个新图像中,其中第二个图像位于第一个图像的旁边,而不是其顶部 BufferedImage imgb1 = img1; BufferedImage imgb2 = img2; BufferedImage imgResult = new BufferedImage(...); 其中imgResult包含相邻的第一个和第二个图像。我为您创建了一个演示和一个单元测试,希望它能工作 代码: 修改您的代码,将一幅图像打印在另一幅图像的顶部(如一幅接一幅)1)我的意思是

我有两个图像,我想将这两个图像复制到一个新图像中,其中第二个图像位于第一个图像的旁边,而不是其顶部

BufferedImage imgb1 = img1;
BufferedImage imgb2 = img2;
BufferedImage imgResult = new BufferedImage(...);

其中
imgResult
包含相邻的第一个和第二个图像。

我为您创建了一个演示和一个单元测试,希望它能工作

代码:


修改您的代码,将一幅图像打印在另一幅图像的顶部(如一幅接一幅)

1)我的意思是除了询问我们之外。2) 为了更快地获得更好的帮助,请发布一个。3) 例如,获取图像的一种方法是热链接到中看到的图像。4) 请不要忘记在问题中添加“?”!有些人在页面中搜索“?”,如果“问题”中不存在,则直接转到行中的下一个(实际)问题。您使用
图形2d
有什么原因吗?为什么不仅仅是
图形
图形
没有
设置画图
。有没有任何方法可以更改为彼此重叠而不是并排side@topcat3,就像这样,只需做一点数学运算,然后布局图片。@wangdq是的,我现在已经做了。如果有人需要解决方案,我可以在下面发布?
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
 * This code try to join two BufferedImage
 * @author wangdq
 * 2013-12-29
 */
public class JoinImage {
    public static void main(String args[])
    {   
        String filename = System.getProperty("user.home")+File.separator;
        try {
            BufferedImage img1 = ImageIO.read(new File(filename+"1.png"));
            BufferedImage img2=ImageIO.read(new File(filename+"2.png"));
            BufferedImage joinedImg = joinBufferedImage(img1,img2);
            boolean success = ImageIO.write(joinedImg, "png", new File(filename+"joined.png"));
            System.out.println("saved success? "+success);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * join two BufferedImage
     * you can add a orientation parameter to control direction
     * you can use a array to join more BufferedImage
     */

    public static BufferedImage joinBufferedImage(BufferedImage img1,BufferedImage img2) {

        //do some calculate first
        int offset  = 5;
        int wid = img1.getWidth()+img2.getWidth()+offset;
        int height = Math.max(img1.getHeight(),img2.getHeight())+offset;
        //create a new buffer and draw two image into the new image
        BufferedImage newImage = new BufferedImage(wid,height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = newImage.createGraphics();
        Color oldColor = g2.getColor();
        //fill background
        g2.setPaint(Color.WHITE);
        g2.fillRect(0, 0, wid, height);
        //draw image
        g2.setColor(oldColor);
        g2.drawImage(img1, null, 0, 0);
        g2.drawImage(img2, null, img1.getWidth()+offset, 0);
        g2.dispose();
        return newImage;
    }
}
public static BufferedImage joinBufferedImage(BufferedImage img1,
      BufferedImage img2) {
    //int offset = 2;
    int width = img1.getWidth();
    int height = img1.getHeight() + img2.getHeight();
    BufferedImage newImage = new BufferedImage(width, height,
        BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = newImage.createGraphics();
    Color oldColor = g2.getColor();
    g2.setPaint(Color.WHITE);
    g2.fillRect(0, 0, width, height);
    g2.setColor(oldColor);
    g2.drawImage(img1, null, 0, 0);
    g2.drawImage(img2, null, 0, img1.getHeight());
    g2.dispose();
    return newImage;
  }