Java 将两个 BufferedImages 并排复制到一个图像中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20826216/
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
Copy two BufferedImages into one image side by side
提问by oooss
I have two images and I'd like to copy these two images to a new image where the second image is beside the first image and not on top of it.
我有两张图片,我想将这两张图片复制到一张新图片中,其中第二张图片位于第一张图片的旁边而不是上面。
BufferedImage imgb1 = img1;
BufferedImage imgb2 = img2;
BufferedImage imgResult = new BufferedImage(...);
where imgResult
contains the first and the second image next to each other.
其中imgResult
包含彼此相邻的第一张和第二张图像。
回答by wangdq
I created a demo for you and also a unit test, hope it works!
我为您创建了一个演示以及一个单元测试,希望它有效!
Code:
代码:
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;
}
}
回答by topcat3
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;
}
Modified your code to print one image on top of eachother(as in one after another)
修改您的代码以在彼此之上打印一个图像(如一个接一个)