在java中将图像拼接在一起

时间:2011-05-13 20:37:34

标签: java image-processing javax.imageio java-io

我正在尝试使用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 个答案:

答案 0 :(得分:2)

AFAIK你在这里做的是为图像写层。但是,png格式不支持此功能。

您必须创建两倍于源图像大小的新图像(例如,对于2x 512x512,新图像应为512x1024或1024x512)。然后,您将源图像渲染到目标图像的相应区域/矩形。

答案 1 :(得分:2)

  

@Thomas:您必须创建两倍大小的源图像的新图像(例如,对于2x 512x512,新图像应为512x1024或1024x512)。然后,您将源图像渲染到目标图像的相应区域/矩形

E.G。 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("http://pscode.org/media/stromlo1.jpg");
        URL nightStromloUrl = new URL("http://pscode.org/media/stromlo2.jpg");
        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 :(得分:0)

我弄清楚为什么它变慢了。实际上,我不想将图像合并在一起,而是将一堆图像拼接在一起。当我真正想要做的就是添加它时,我正在做的是重写原始图像。现在快得多!