如何在 Java 中拼接图像对象

发布于 2024-08-30 05:03:43 字数 4665 浏览 2 评论 0原文

我有一个场景,我从地图服务器获取许多图块(例如 12)。现在,对于缓冲和离线功能,我需要将它们全部重新连接起来,这样我们就必须处理 1 个单个图像对象而不是 12 个。我尝试在没有 JAI 的情况下做到这一点,我的代码如下。

package imagemerge;


import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

public class ImageSticher extends WindowAdapter {

    Image tile1;
    Image tile2;
    Image result;
    ColorModel colorModel;
    int width,height,widthr,heightr;
    //int t1,t2;
    int t12[];


    public ImageSticher()
    {

    }

    public ImageSticher (Image img1,Image img2,int w,int h)
    {
        tile1=img1;
        tile2=img2;

        width=w;
        height=h;
        colorModel=ColorModel.getRGBdefault();

    }

    public Image horizontalStich() throws Exception
    {

        widthr=width*2;
        heightr=height;

        t12=new int[widthr *  heightr];

        int t1[]=new int[width*height];
        PixelGrabber p1 =new PixelGrabber(tile1, 0, 0, width, height, t1, 0, width);
        p1.grabPixels();

        int t2[]=new int[width*height];
        PixelGrabber p2 =new PixelGrabber(tile2, 0, 0, width, height, t1, 0, width);
        p2.grabPixels();

        int y, x, rp, rpi;
        int red1, red2, redr;
        int green1, green2, greenr;
        int blue1, blue2, bluer;
        int alpha1, alpha2, alphar;

        for(y=0;y<heightr;y++)
        {
            for(x=0;x<widthr;x++)
            {
                //System.out.println(x);
                rpi=y*widthr+x; // index of resulting pixel;
                rp=0;           //initializing resulting pixel
                System.out.println(rpi);

                if(x<(widthr/2)) // x is less than width , copy first tile
                {
                    //System.out.println("tile1="+x);
                    blue1 = t1[rpi] & 0x00ff; // ERROR occurs here
                    green1=(t1[rpi] >> 8) & 0x00ff;
                    red1=(t1[rpi] >> 16) & 0x00ff;
                    alpha1 = (t1[rpi] >> 24) & 0x00ff;

                    redr = (int)(red1 * 1.0); // copying red band pixel into redresult,,,,1.0 is the alpha valye
                    redr = (redr < 0)?(0):((redr>255)?(255):(redr));
                    greenr = (int)(green1 * 1.0); //
                    redr = (int)(red1 * 1.0); //
                    greenr = (greenr < 0)?(0):((greenr>255)?(255):(greenr));
                    bluer = (int)(blue1 * 1.0);
                    bluer =  (bluer < 0)?(0):((bluer>255)?(255):(bluer));
                    alphar = 255;

                    //resulting pixel computed
                    rp = (((((alphar << 8) + (redr & 0x0ff)) << 8) + (greenr & 0x0ff)) << 8) + (bluer & 0x0ff);


                }
                else // index is ahead of half way...copy second tile
                {

                    blue2 = t2[rpi] & 0x00ff; // blue band bit of first tile
                    green2=(t2[rpi] >> 8) & 0x00ff;
                    red2=(t2[rpi] >> 16) & 0x00ff;
                    alpha2 = (t2[rpi] >> 24) & 0x00ff;

                    redr = (int)(red2 * 1.0); // copying red band pixel into redresult,,,,1.0 is the alpha valye
                    redr = (redr < 0)?(0):((redr>255)?(255):(redr));
                    greenr = (int)(green2 * 1.0); //
                    redr = (int)(red2 * 1.0); //
                    greenr = (greenr < 0)?(0):((greenr>255)?(255):(greenr));
                    bluer = (int)(blue2 * 1.0);
                    bluer =  (bluer < 0)?(0):((bluer>255)?(255):(bluer));
                    alphar = 255;

                    //resulting pixel computed
                    rp = (((((alphar << 8) + (redr & 0x0ff)) << 8) + (greenr & 0x0ff)) << 8) + (bluer & 0x0ff);


                }
                t12[rpi] = rp; // copying resulting pixel in the result int array which will be converted to image

            }
        }

        MemoryImageSource mis;
        if (t12!=null) {
            mis = new MemoryImageSource(widthr, heightr, colorModel, t12, 0, widthr);
            result = Toolkit.getDefaultToolkit().createImage(mis);
            return result;

        }
        return null;

    }





}

现在检查我的理论,我正在尝试水平连接或缝合两个图块,但我收到错误:

java.lang.ArrayIndexOutOfBoundsException:90000 在 imagemerge.ImageSticher.horizo​​ntalStich(ImageSticher.java:69) 在 imageStream.ImageStream.getImage(ImageStream.java:75) 在 imageStream.ImageStream.main(ImageStream.java:28)

处存在某种限制,因为当水平拼接两个 300 x 300 的图像时,这意味着生成的图像将为 600 x 300 ...这将使 180000 索引大小,但其在 90000 处给出错误,我在这里做错了什么

I have a scenario in which i`m getting a number of tiles (e.g.12) from my mapping server. Now for buffering and offline functions I need to join them all back again so that we have to deal with 1 single image object instead of 12. I ve tried to do it without JAI my code is below.

package imagemerge;


import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

public class ImageSticher extends WindowAdapter {

    Image tile1;
    Image tile2;
    Image result;
    ColorModel colorModel;
    int width,height,widthr,heightr;
    //int t1,t2;
    int t12[];


    public ImageSticher()
    {

    }

    public ImageSticher (Image img1,Image img2,int w,int h)
    {
        tile1=img1;
        tile2=img2;

        width=w;
        height=h;
        colorModel=ColorModel.getRGBdefault();

    }

    public Image horizontalStich() throws Exception
    {

        widthr=width*2;
        heightr=height;

        t12=new int[widthr *  heightr];

        int t1[]=new int[width*height];
        PixelGrabber p1 =new PixelGrabber(tile1, 0, 0, width, height, t1, 0, width);
        p1.grabPixels();

        int t2[]=new int[width*height];
        PixelGrabber p2 =new PixelGrabber(tile2, 0, 0, width, height, t1, 0, width);
        p2.grabPixels();

        int y, x, rp, rpi;
        int red1, red2, redr;
        int green1, green2, greenr;
        int blue1, blue2, bluer;
        int alpha1, alpha2, alphar;

        for(y=0;y<heightr;y++)
        {
            for(x=0;x<widthr;x++)
            {
                //System.out.println(x);
                rpi=y*widthr+x; // index of resulting pixel;
                rp=0;           //initializing resulting pixel
                System.out.println(rpi);

                if(x<(widthr/2)) // x is less than width , copy first tile
                {
                    //System.out.println("tile1="+x);
                    blue1 = t1[rpi] & 0x00ff; // ERROR occurs here
                    green1=(t1[rpi] >> 8) & 0x00ff;
                    red1=(t1[rpi] >> 16) & 0x00ff;
                    alpha1 = (t1[rpi] >> 24) & 0x00ff;

                    redr = (int)(red1 * 1.0); // copying red band pixel into redresult,,,,1.0 is the alpha valye
                    redr = (redr < 0)?(0):((redr>255)?(255):(redr));
                    greenr = (int)(green1 * 1.0); //
                    redr = (int)(red1 * 1.0); //
                    greenr = (greenr < 0)?(0):((greenr>255)?(255):(greenr));
                    bluer = (int)(blue1 * 1.0);
                    bluer =  (bluer < 0)?(0):((bluer>255)?(255):(bluer));
                    alphar = 255;

                    //resulting pixel computed
                    rp = (((((alphar << 8) + (redr & 0x0ff)) << 8) + (greenr & 0x0ff)) << 8) + (bluer & 0x0ff);


                }
                else // index is ahead of half way...copy second tile
                {

                    blue2 = t2[rpi] & 0x00ff; // blue band bit of first tile
                    green2=(t2[rpi] >> 8) & 0x00ff;
                    red2=(t2[rpi] >> 16) & 0x00ff;
                    alpha2 = (t2[rpi] >> 24) & 0x00ff;

                    redr = (int)(red2 * 1.0); // copying red band pixel into redresult,,,,1.0 is the alpha valye
                    redr = (redr < 0)?(0):((redr>255)?(255):(redr));
                    greenr = (int)(green2 * 1.0); //
                    redr = (int)(red2 * 1.0); //
                    greenr = (greenr < 0)?(0):((greenr>255)?(255):(greenr));
                    bluer = (int)(blue2 * 1.0);
                    bluer =  (bluer < 0)?(0):((bluer>255)?(255):(bluer));
                    alphar = 255;

                    //resulting pixel computed
                    rp = (((((alphar << 8) + (redr & 0x0ff)) << 8) + (greenr & 0x0ff)) << 8) + (bluer & 0x0ff);


                }
                t12[rpi] = rp; // copying resulting pixel in the result int array which will be converted to image

            }
        }

        MemoryImageSource mis;
        if (t12!=null) {
            mis = new MemoryImageSource(widthr, heightr, colorModel, t12, 0, widthr);
            result = Toolkit.getDefaultToolkit().createImage(mis);
            return result;

        }
        return null;

    }





}

now to check the my theory Im trying to join or stich two tiles horizontaly but im getting the error :

java.lang.ArrayIndexOutOfBoundsException: 90000
at imagemerge.ImageSticher.horizontalStich(ImageSticher.java:69)
at imageStream.ImageStream.getImage(ImageStream.java:75)
at imageStream.ImageStream.main(ImageStream.java:28)

is there some kind of limitation because when stiching two images of 300 x 300 horizontally it means the resulting image will be 600 x 300 ... that would make 180000 index size but its giving error at 90000, what am I doing wrong here

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

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

发布评论

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

评论(1

像你 2024-09-06 05:03:44

rpi 是结果像素的索引,而不是 t1 中源图块数据的索引,后者大小是一半。

rpi is the index of your resulting pixel, not the index in your source tile data in t1, which is half the size.

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