0

我正在使用一个包含两个图像的 LayeredPane,每层一个。我一直在研究一种获取图像位置的方法(基于图像所在的标签位置),然后保存底部图像(layeredPane 中的下部图像)以及顶部图像(如果它完全覆盖底部)图像(可能只是图像的一部分),但是我遇到了一些麻烦,我有点不确定如何让它正常工作。

我已经为此工作了很长一段时间,所以对我现有代码的任何帮助或关于我应该如何以另一种方式处理这个问题的想法对我来说都是一个很大的帮助。

提前致谢。

public void saveImageLayering(BufferedImage topImg,BufferedImage bottomImg, JLabel topLabel, JLabel bottomLabel) {
    int width = bottomImg.getWidth();
    int height = bottomImg.getHeight();
    Point bottomPoint = new Point();
    Point topPoint = new Point();

    bottomPoint = bottomLabel.getLocation();
    topPoint = topLabel.getLocation();  

    System.out.println("image x coordinate " + bottomPoint.x);
    System.out.println("image y coordinate " + bottomPoint.y);  

    //arrays to store the bottom image
    int bottomRedImgArray[][] = new int[width][height];
    int bottomGreenImgArray[][] = new int[width][height];
    int bottomBlueImgArray[][] = new int[width][height];

    //arrays to store the top image
    int topRedImgArray[][] = new int[width][height];
    int topGreenImgArray[][] = new int[width][height];
    int topBlueImgArray[][] = new int[width][height];

    //loop through the bottom image and get all pixels rgb values
    for(int i = bottomPoint.x; i < width; i++){
        for(int j = bottomPoint.y; j < height; j++){
            //set pixel equal to the RGB value of the pixel being looked at
            pixel = new Color(bottomImg.getRGB(i, j));
            //contain the RGB values in the respective RGB arrays
            bottomRedImgArray[i][j] = pixel.getRed();
            bottomGreenImgArray[i][j] = pixel.getGreen();
            bottomBlueImgArray[i][j] = pixel.getBlue();
        }   
    }
    //create new image the same size as old
    BufferedImage newBottomImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    //set values within the 2d array to the new image
    for (int x1 = 0; x1 < width; x1++){
        for (int y1 = 0; y1 < height; y1++){
            //putting values back into buffered image
            int newPixel = (int) bottomRedImgArray[x1][y1];
            newPixel = (newPixel << 8) + (int) bottomGreenImgArray[x1][y1];
            newPixel = (newPixel << 8) + (int) bottomBlueImgArray[x1][y1];
            newBottomImage.setRGB(x1, y1, newPixel);
        }
    }

    //create rectangle around bottom image to check if coordinates of top in inside and save only the ones that are
    Rectangle rec = new Rectangle(bottomPoint.x, bottomPoint.y, bottomImg.getWidth(), bottomImg.getHeight());

    //loop through the top image and get all pixels rgb values 
    for(int i = bottomPoint.x; i < bottomImg.getWidth(); i++){
        for(int j = bottomPoint.y; j < bottomImg.getHeight(); j++){

            //if top image is inside lower image then getRGB values
            if (rec.contains(topPoint)) { //___________________________________________________________doesnt contain any..
                if (firstPointFound  == true) {
                    //set pixel equal to the RGB value of the pixel being looked at
                    pixel = new Color(topImg.getRGB(i, j));
                    //contain the RGB values in the respective RGB arrays
                    topRedImgArray[i][j] = pixel.getRed();
                    topGreenImgArray[i][j] = pixel.getGreen();
                    topBlueImgArray[i][j] = pixel.getBlue();
                } else {
                    firstPoint = new Point(i, j);
                    firstPointFound = true;
                }
            }
        }
    }
    //create new image the same size as old
    BufferedImage newTopImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    //set values within the 2d array to the new image
    for (int x1 = 0; x1 < topImg.getWidth(); x1++){
        for (int y1 = 0; y1 < topImg.getHeight(); y1++){
            //putting values back into buffered image
            int newPixel = (int) topRedImgArray[x1][y1];
            newPixel = (newPixel << 8) + (int) topGreenImgArray[x1][y1];
            newPixel = (newPixel << 8) + (int) topBlueImgArray[x1][y1];
            newTopImage.setRGB(x1, y1, newPixel);
        }
    }

    BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 

    //uses the Graphics.drawImage() to place them on top of each other
    Graphics g = newImage.getGraphics();
    g.drawImage(newBottomImage, bottomPoint.x, bottomPoint.y, null);
    g.drawImage(newTopImage, firstPoint.x, firstPoint.y, null);

    try {
        //then save as image once all in correct order
        File outputfile = new File("saved_Layered_Image.png");
        ImageIO.write(newImage, "png", outputfile);
        JOptionPane.showMessageDialog(null, "New image saved successfully");
    } catch (IOException e) {
        e.printStackTrace();
    }
}
4

1 回答 1

1

我不太确定你为什么要搞乱像素,但是,这个想法相对简单

基本上,您想创建第三个“合并”图像,其大小与bottomImage. 从那里,您只需要将0x0绘制bottomImage到图像上。merged

然后,您需要计算 离 的位置的topImage距离bottomImage并在该点进行绘制。

BufferedImage merged = new BufferedImage(bottomImg.getWidth(), bottomImg.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = master.createGraphics();
g2d.drawImage(bottomImg, 0, 0, this);

int x = topPoint .x - bottomPoint .x;
int y = topPoint .y - bottomPoint .y;

g2d.drawImage(topImg, x, y, this);
g2d.dispose();

使用这个基本想法,我能够制作这些......

在此处输入图像描述在此处输入图像描述

于 2013-02-08T03:29:55.403 回答