0

各レイヤーに1つずつ、合計2つの画像を含む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

なぜピクセルをいじっているのかよくわかりませんが、アイデアは比較的単純です

基本的に、と同じサイズの3番目の「マージされた」イメージを作成する必要があります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 に答える