Java将多个图像组合成一个较大的图像而不重叠

时间:2011-07-20 20:01:20

标签: java bufferedreader javax.imageio

我正在尝试使用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");  
}
}

4 个答案:

答案 0 :(得分:2)

我注意到的第一件事是你在每次图像绘制后在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);
}

答案 1 :(得分:0)

您正在for循环中调用dispose方法,因此当程序完成循环内部代码的第一次运行时,它就会删除paint对象。当第二次浏览循环时,程序找不到绘制对象,因此既不能绘制下一个图像也不能用白色填充房间。

请在关闭for循环后立即尝试使用dispose方法。

您还可以尝试添加一些println方法来查看imgs项返回的长度。您通常应该检查循环中使用的变量中的数字,看看是否得到了你想要的。

答案 2 :(得分:0)

创建一个使用GridLayout的面板?然后,您可以向面板添加12个JLabel,每个标签包含一个ImageIcon。通过这种方式,布局管理器可以完成定位和绘制图像的所有艰苦工作。

答案 3 :(得分:0)

这可以在Java中完成,下面是示例程序。

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

public class CombineImages {
    public static void main(String[] args) throws IOException {
        int imagesCount = 4;
        BufferedImage images[] = new BufferedImage[imagesCount];
        for(int j = 0; j < images.length; j++) {
            images[j] = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = images[j].createGraphics();
            g2d.drawRect(10, 10, 80, 80);
            g2d.dispose();
        }

        int heightTotal = 0;
        for(int j = 0; j < images.length; j++) {
            heightTotal += images[j].getHeight();
        }

        int heightCurr = 0;
        BufferedImage concatImage = new BufferedImage(100, heightTotal, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = concatImage.createGraphics();
        for(int j = 0; j < images.length; j++) {
            g2d.drawImage(images[j], 0, heightCurr, null);
            heightCurr += images[j].getHeight();
        }
        g2d.dispose();

        ImageIO.write(concatImage, "png", new File("/Users/kumarabhishek/Downloads/downloadedFiles/concat.png")); // export concat image
        ImageIO.write(images[0], "png", new File("/Users/kumarabhishek/Downloads/downloadedFiles/single.png"));

    }
}
相关问题