gpt4 book ai didi

Java 将多个图像合并为一个更大的图像而不重叠

转载 作者:行者123 更新时间:2023-11-30 05:00:54 24 4
gpt4 key购买 nike

我正在尝试使用 Java 将多个图像组合成一个更大的图像。传入的图像均为高 127 x 宽 293。其想法是将多个图像传递给该方法,然后该方法获取这些图像并将它们构建为另一个更大的图像。较大图像将有一个布局,其中总共 12 个可能的图像可以输入到较大图像,均匀间隔(2 行,每行 6 个图像,无重叠)。如果传入的图像少于 12 个,则仅填充第一个空格,无论多少个空格都会被填充,图像的其余部分将为白色,因为背景将为白色。当我运行该程序时,它会打印较大的图像,但它只会填充显示左上角第一个图像的第一个空间,无论传入多少图像。而且背景是粉红色,而不是预期的白色背景。我只是 Java 的初学者,所以我正在努力解决其中的一些学习难题。关于如何解决我的问题有什么建议吗? (代码复制如下供引用)谢谢!

public class ImagesCombine {

public String BuildImgs (File[] imgs)throws IOException {
int arsize = imgs.length;
File path = new File("Z:/JAVAFiles/Images/");
BufferedImage page = new BufferedImage(620,900,BufferedImage.TYPE_INT_ARGB);
Graphics2D paint;
paint = page.createGraphics();
paint.setPaint(Color.WHITE);
paint.fillRect ( 0, 0, page.getWidth(), page.getHeight() );
paint.setBackground(Color.WHITE);
String tmpname = "";

for (int i=0;i<imgs.length;i++){

if(i==0){
Image img0 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img0,0,0,null);
paint.dispose();
}
if(i==1){
Image img1 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img1,323,0,null);
paint.dispose();
}
if(i==2){
Image img2 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img2,0,142,null);
paint.dispose();
}
if(i==3){
BufferedImage img3 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img3,323,142,null);
paint.dispose();
}
if(i==4){
BufferedImage img4 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img4,0,284,null);
paint.dispose();
}
if(i==5){
BufferedImage img5 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img5,323,284,null);
paint.dispose();
}
if(i==6){
BufferedImage img6 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img6,0,426,null);
paint.dispose();
}
if(i==7){
BufferedImage img7 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img7,323,426,null);
paint.dispose();
}
if(i==8){
BufferedImage img8 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img8,0,568,null);
paint.dispose();
}
if(i==9){
BufferedImage img9 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img9,323,568,null);
paint.dispose();
}
if(i==10){
BufferedImage img10 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img10,0,710,null);
paint.dispose();
}
if(i==11){
BufferedImage img11 = ImageIO.read(new File(path, imgs[i].getName()));
paint.drawImage(img11,323,710,null);
paint.dispose();
}

}
String outpath = "Z:\\JAVAFiles\\" + imgs[0].getName().substring(0,16) + ".jpg";

OutputStream outfile = new FileOutputStream(outpath);

JPEGImageEncoder encoder2 = JPEGCodec.createJPEGEncoder(outfile);
encoder2.encode(page);
outfile.close();
return("Success");
}
}

最佳答案

我注意到的第一件事是,在每次绘制图像后,您都会在 Graphics2D 上调用 dispose() 。这可能就是为什么您只看到大图像中绘制了一张图像的原因。取出该调用并将其放在循环之后,您应该开始看到更多图像。

顺便说一句,您可以大大简化 for 循环:

int width = 293;
int height = 127;
for (int i=0; i < Math.min(imgs.length, 12); i++){
Image image = ImageIO.read(new File(path, imgs[i].getName()));
int row = i / 6; // This will truncate to 0 or 1.
int column = i % 6; // Mod will produce the remainder of i / 6 in the range 0-5
paint.drawImage(image, column * width, row * height, null);
}

关于Java 将多个图像合并为一个更大的图像而不重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6767761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com