用java将图像拼接在一起

发布于 2024-11-07 07:18:58 字数 1590 浏览 5 评论 0原文

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

I'm trying to stitch some images together using java. I have a bunch of images I'd like to stitch together and they are all the same dimensions so it's really just a question of lining them up next to each other I suppose. I have it working but it's very slow and probably very memory intensive. I'm wondering if there's an easier way:

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

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

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

发布评论

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

评论(3

审判长 2024-11-14 07:18:58

@Thomas:您必须创建一个两倍于源图像大小的新图像(例如,对于 2x 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://i.sstatic.net/OVOg3.jpg");
        URL nightStromloUrl = new URL("https://i.sstatic.net/lxthA.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

列中的图像

@Thomas: You'd have to create a new image of twice the size of the source images (e.g. for 2x 512x512 the new image should be 512x1024 or 1024x512). Then you'd render the source images to the respective area/rectangle of the target image

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("https://i.sstatic.net/OVOg3.jpg");
        URL nightStromloUrl = new URL("https://i.sstatic.net/lxthA.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

半岛未凉 2024-11-14 07:18:58

据我所知,您在这里所做的是将图层写入图像。然而,png 格式不支持这一点。

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

AFAIK what you're doing here is to write layers to a image. However, the png format doesn't support this.

You'd have to create a new image of twice the size of the source images (e.g. for 2x 512x512 the new image should be 512x1024 or 1024x512). Then you'd render the source images to the respective area/rectangle of the target image.

盛夏尉蓝 2024-11-14 07:18:58

我明白了为什么进展缓慢。实际上,我不想将图像合并在一起,而是将一堆图像缝合在一起。我所做的就是重写原始图像中的所有内容,而我真正想做的就是添加到其中。现在快多了!

I figured out why it was going slow. In reality, I didn't want to merge images together, but rather stitch together a bunch of images. What I was doing was rewriting the original image everything when all I really want to do is add to it. Much faster now!

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