Saturday, 11 November 2017

Add Image Water Mark on Image in JAVA

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

links for Data Structure

  1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭:  https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...