在java中将图像拼接在一起 [英] stitch images together in java

查看:28
本文介绍了在java中将图像拼接在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 java 将一些图像拼接在一起.我有一堆我想拼接在一起的图像,它们的尺寸都相同,所以我想这实际上只是一个将它们排列在一起的问题.我有它的工作,但它很慢,而且可能非常占用内存.我想知道是否有更简单的方法:

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

推荐答案

我想出了为什么它运行缓慢.实际上,我不想将图像合并在一起,而是将一堆图像拼接在一起.我正在做的是重写原始图像,而我真正想做的就是添加它.现在快多了!

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!

这篇关于在java中将图像拼接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆