在Java中拼接图像

3

我正在尝试使用Java拼接一些图像。 我有许多要拼接在一起的图像,它们都具有相同的尺寸,所以实际上只需要将它们排成一行即可。我已经做到了,但速度非常慢,可能会占用大量内存。我想知道是否有更简单的方法:

public static void main(String[] args) throws IOException
    {
        int dim = 256;
        BufferedImage merged = null;
        for(int y = 0; y<10;y++)
        {
            for(int x = 0; x<10;x++)
            {
                URL url = new URL(someURL);
                BufferedImage nextImage = ImageIO.read(url);
                if(merged==null)
                    merged=nextImage;
                else
                {
                    BufferedImage tempMerged;
                    tempMerged = new BufferedImage(10*dim,10*dim,merged.getType());
                    //Write first image
                    for(int xx=0;xx<merged.getWidth();xx++)
                        for(int yy=0;yy<merged.getHeight();yy++)
                            tempMerged.setRGB(xx,yy,merged.getRGB(xx,yy));
                    //Write img2
                    for(int xx=0;xx<dim;xx++)
                    {
                        for(int yy=0;yy<dim;yy++)
                        {
                            int destX = (x*dim)+xx;
                            int destY = (y*dim)+yy;
                            tempMerged.setRGB(destX,destY,nextImage.getRGB(xx,yy));
                        }
                    }
                    merged=tempMerged;
                }
                System.out.println("Stitched image at "+x+","+y);
            }
        }
        ImageIO.write(merged, "png", new File("merged.png"));
    }
3个回答

3
@Thomas:您需要创建一个新的图片,其大小是原始图片的两倍(例如,对于2个512x512的图像,新图像应为512x1024或1024x512)。然后,您将呈现源图像到目标图像的相应区域/矩形中。

例如 TiledImageWrite.java

import java.awt.image.BufferedImage;
import java.awt.*;
import javax.swing.*;
import java.net.URL;
import java.io.File;
import javax.imageio.ImageIO;

class TiledImageWrite {

    public static void main(String[] args) throws Exception {

        URL dayStromloUrl = new URL("https://istack.dev59.com/OVOg3.webp");
        URL nightStromloUrl = new URL("https://istack.dev59.com/lxthA.webp");
        final BufferedImage dayStromloImage = ImageIO.read(dayStromloUrl);
        final BufferedImage nightStromloImage = ImageIO.read(nightStromloUrl);

        final int width = dayStromloImage.getWidth();
        final int height = dayStromloImage.getHeight();;

        final BufferedImage columnImage =
            new BufferedImage(width,2*height,BufferedImage.TYPE_INT_RGB);
        final BufferedImage rowImage =
        new BufferedImage(2*width,height,BufferedImage.TYPE_INT_RGB);

        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new BorderLayout(3,3));

                Graphics2D g2dColumn = columnImage.createGraphics();
                g2dColumn.drawImage(dayStromloImage,0,0, null);
                // start this one at 'height' down the final image
                g2dColumn.drawImage(nightStromloImage,0,height, null);

                Graphics2D g2dRow = rowImage.createGraphics();
                g2dRow.drawImage(dayStromloImage,0,0, null);
                // start this one at 'width' across the final image
                g2dRow.drawImage(nightStromloImage,width,0, null);

                gui.add(new JLabel(new ImageIcon(columnImage)),BorderLayout.CENTER);
                gui.add(new JLabel(new ImageIcon(rowImage)),BorderLayout.SOUTH);

                JOptionPane.showMessageDialog(null, gui);
            }
        } );

        ImageIO.write(columnImage, "png", new File("column.png"));
        ImageIO.write(rowImage, "png", new File("row.png"));
    }
}

column.png

Images in a column


2
据我所知,您在这里的做法是将图层写入图像中。然而,PNG格式不支持此操作。
您需要创建一个新图像,其大小是源图像的两倍(例如,对于2个512x512的源图像,新图像应为512x1024或1024x512)。然后,您需要将源图像渲染到目标图像的相应区域/矩形中。

我明白了,那我该怎么做呢?我不确定要搜索什么术语来开始。 - JPC
更新帖子以反映更改。 - JPC

0
我找到了为什么它变慢的原因。实际上,我不想把图像合并在一起,而是要将一堆图像拼接在一起。我所做的是每次都重写原始图像,而我真正想做的只是添加到它上面。现在快多了!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接