/**
*
*/
package com.jijo;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
/**
* @author jijo mathew
*
*/
public class Util {
public static void imageWaterMarking(InputStream input,String outPath){
try
{
BufferedImage bufferedImage = ImageIO.read(input);
Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
Image image= bufferedImage.getScaledInstance(bufferedImage.getWidth(),
bufferedImage.getHeight(),Image.SCALE_DEFAULT);
g2d.drawImage(image, 0, 0, null);
//Create an alpha composite of 50%
AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
0.5f);
g2d.setComposite(alpha);
g2d.setColor(Color.white);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setFont(new Font("Arial", Font.BOLD, 30));
String watermark = "Copyright © jijo84.blogspot.com";
FontMetrics fontMetrics = g2d.getFontMetrics();
Rectangle2D rect = fontMetrics.getStringBounds(watermark, g2d);
g2d.drawString(watermark,
(bufferedImage.getWidth() - (int) rect.getWidth()) / 2,
(bufferedImage.getHeight() - (int) rect.getHeight()) / 2);
//Free graphic resources
g2d.dispose();
//Set the mime type of the image
//Write the image as a jpg
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outPath));
ImageIO.write(bufferedImage, "jpg", out);
out.close();
}
catch (Exception ioe)
{
ioe.printStackTrace();
}
}
public static void main(String args[]) {
try{
String outPath = "C:/images/jijo.jpg";
String inputPath = "C:/jijo.jpg";
File f=new File(inputPath);
InputStream inputStream= new FileInputStream(f);
imageWaterMarking(inputStream,outPath);//for water mark image
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment