Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.5 kB
1
Indexable
Never
import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

@Component
public class ImageOverlayService {

    public void overlayImages(String backgroundImagePath, String overlayImagePath, String outputPath) {
        try {
            // Wczytaj oba obrazy
            BufferedImage backgroundImage = ImageIO.read(new File(backgroundImagePath));
            BufferedImage overlayImage = ImageIO.read(new File(overlayImagePath));

            // Utwórz obraz wynikowy o rozmiarze tła
            BufferedImage resultImage = new BufferedImage(backgroundImage.getWidth(), backgroundImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = resultImage.createGraphics();

            // Narysuj tło
            g2d.drawImage(backgroundImage, 0, 0, null);

            // Narysuj nałożenie obrazka z zachowaniem przezroczystości
            AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
            g2d.setComposite(alphaComposite);
            g2d.drawImage(overlayImage, 0, 0, null);

            // Zapisz obraz wynikowy
            ImageIO.write(resultImage, "png", new File(outputPath));

            // Zwolnij zasoby
            g2d.dispose();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}