Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.3 kB
2
Indexable
Never
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.StackPane;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

@Component
public class ImageOverlay {

    public void overlayImages(String backgroundImagePath, String overlayImagePath, String outputPath) {
        // Wczytaj oba obrazy
        Image backgroundImage = new Image("file:" + backgroundImagePath);
        Image overlayImage = new Image("file:" + overlayImagePath);

        // Utwórz ImageView dla obu obrazków
        ImageView backgroundImageView = new ImageView(backgroundImage);
        ImageView overlayImageView = new ImageView(overlayImage);

        // Utwórz przyciski na podstawie guzików w pierwszym obrazku
        Button button1 = new Button();
        Button button2 = new Button();
        // Ustaw przyciski na podstawie wyglądu guzików
        button1.setGraphic(new ImageView(backgroundImage));
        button2.setGraphic(new ImageView(backgroundImage));

        // Utwórz kontener StackPane, aby pomieścić oba obrazki
        StackPane root = new StackPane();
        root.getChildren().addAll(backgroundImageView, overlayImageView, button1, button2);

        // Utwórz scenę i ustaw ją na podstawie StackPane
        Scene scene = new Scene(root, backgroundImage.getWidth(), backgroundImage.getHeight());

        // Utwórz obraz wynikowy
        WritableImage resultImage = new WritableImage((int) backgroundImage.getWidth(), (int) backgroundImage.getHeight());
        root.snapshot(new SnapshotParameters(), resultImage);

        // Zapisz obraz wynikowy do pliku PNG
        File outputFile = new File(outputPath);
        BufferedImage bufferedImage = SwingFXUtils.fromFXImage(resultImage, null);
        try {
            ImageIO.write(bufferedImage, "png", outputFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}