Below code for add image water mark on image in java:
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class TestImage41im {
public static void main(String[] args) {
try {
File sourceImageFile = new File("originalImagePath/originalimage.jpg");
File destImageFile = new File("destinationImagePath/destinationImage.png");
File watermarkImageFile = new File("waterMarkImagePath/watermark.png");
BufferedImage sourceImage = ImageIO.read(sourceImageFile);
BufferedImage watermarkImage = ImageIO.read(watermarkImageFile);
// graphic properties
Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();
AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f); //add transparency of watermark image
g2d.setComposite(alphaChannel);
//coordinate where the image is painted below are the center
int x = (sourceImage.getWidth() - watermarkImage.getWidth()) / 2;
int y = (sourceImage.getHeight() - watermarkImage.getHeight()) / 2;
// draw the image watermark
g2d.drawImage(watermarkImage, x, y, null);
ImageIO.write(sourceImage, "png", destImageFile);
g2d.dispose();
System.out.println("The image watermark is added to the image.");
}catch (Exception e) {
// TODO: handle exception
}
}
}
No comments:
Post a Comment