Untitled

 avatar
unknown
plain_text
a year ago
1.9 kB
5
Indexable
import org.springframework.stereotype.Component;

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

@Component
public class ImageOverlayService {

    public void overlayImagesWithTransparency(String backgroundImagePath, String overlayImagePath, String outputPath) {
        try {
            BufferedImage backgroundImage = loadImage(backgroundImagePath);
            BufferedImage overlayImage = loadImage(overlayImagePath);

            if (backgroundImage != null && overlayImage != null) {
                BufferedImage resultImage = new BufferedImage(
                        backgroundImage.getWidth(), backgroundImage.getHeight(), BufferedImage.TYPE_INT_ARGB);

                Graphics2D resultGraphics = resultImage.createGraphics();

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

                // Narysuj guziki z pierwszego obrazka na drugim obrazku jako transparentne
                AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.0f);
                resultGraphics.setComposite(alphaComposite);
                resultGraphics.drawImage(overlayImage, 0, 0, null);

                resultGraphics.dispose();

                saveImage(resultImage, outputPath);
            } else {
                System.err.println("Błąd podczas wczytywania obrazów.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private BufferedImage loadImage(String path) throws IOException {
        return ImageIO.read(new File(path));
    }

    private void saveImage(BufferedImage image, String outputPath) throws IOException {
        ImageIO.write(image, "png", new File(outputPath));
    }
}
Editor is loading...