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

发布于 2024-11-25 17:17:01 字数 3451 浏览 1 评论 0原文

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

I'm trying to combine several images into a larger image using Java. The images that are passed in are all height 127 x width 293. The idea is that a number of images are passed to the method and the method takes the images and builds them into another larger image. There is going to be a layout for the larger image where a total of 12 possible images can be input to the larger image, spaced out evenly (2 rows of 6 images, none overlapping). If there are fewer than 12 images passed in, then only the first however many spaces will be filled, the rest of the image will be white because the background is to going to be white. When I run the program it prints the larger image, but it will only fill the first space showing the first image in the upper left, regardless of how many images are passed in. Also the background is a pinkish color instead of the intended white background. I'm only a beginner with Java so I'm trying to work through some of these learning pains. Any advice on how I might be able to solve my problem? (Code is copied below for reference) Thanks!

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");  
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑忘罢 2024-12-02 17:17:01

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

The first thing I notice is that you're calling dispose() on the Graphics2D after each image draw. This is probably why you're only seeing one image being drawn in the larger image. Take out that call and place it after the loop and you should start seeing more images.

As a side note, you can simplify your for-loop a lot:

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);
}
落墨 2024-12-02 17:17:01

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

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

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

You are calling the dispose method inside a for loop so when the program completes the first run of the code inside the loop it then gets rid of the paint object. When it's time to browse the loop for a second time, the program can't find the paint object so it can neither draw the next image nor fill the room with white.

Try using the dispose method right after you close your for loop.

You can also try adding some println methods to see what length does the imgs item return. You should generally check the numbers inside the variables you use in your loop to see if you are getting what you want.

又怨 2024-12-02 17:17:01

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

Create a panel that uses a GridLayout? Then you can add 12 JLabels to the panel with each label containing an ImageIcon. This way the layout manager does all the hard work of positioning and painting the images.

人疚 2024-12-02 17:17:01

这可以在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"));

    }
}

This can be done in java, below is the sample program.

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"));

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文