Friday, May 1, 2009

Hi all, i was trying to put watermark on my images. i tried to search watermarking in Google. i found lots of online tools which does watermarking for images but all of them have some restriction. you cannot have image as watermark and more over they do not allow us to put more than few images to watermark, we need to have a break !! it was all problematic. more over if we put watermark using photoshop editor it increases size of image and also lot of editing is to be done. you need to apply watermark to each image which becomes quiet tedious. you waste lot of time to watermark images.

so how if you get something which will reside your images to fixed resolution, fixed max height, fixed max width, and put your watermark Logo and string to all the photos in the folder. Doesn't it sound exiting

Find the example of watermark done by this code




here is java code for doing all this things at same time. you can use this code as java Api for watermarking, copy this code into favorite IDE and run it, you can do all those things which i mentioned. this code also improve the quality of image

Download File

import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.ImageWriteParam;
import javax.imageio.IIOImage;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.io.IOException;
import java.io.File;
import java.util.Iterator;
import javax.imageio.stream.FileImageOutputStream;
public class waterMark {

public waterMark() {
}
public static void main(String[] args) {
try {

int maxImgWidth=560;//set max width of output image
int maxImgHeight=420;//set max height of output image
float imageQuality=.9f;//set quality of output image
//input output file path; if you keep the filepath as directory all the images with extension .jpg will be attached with watermark
String filepath="E:\\hitesh\\My Blog Data\\Watermark\\Coding\\Image\\DSC02298.jpg";
String outputFile="E:\\hitesh\\My Blog Data\\Watermark\\Coding\\Translated";
//logo details
String Logo="E:\\hitesh\\My Blog Data\\Watermark\\Coding\\Image\\logo.png";
int logoPositionx=10;//set x position of logo image
int logoPositionY=10;//set y positon of logo image
float logoFill=.65f;//set opacity for logo image
//text logo details
String watermark="© Hitesh Sathawane";
float positonx=10f;//set x position of watermark string
float positony=80f;//set y position of watermark string
int fontSize=12;//set font size of watermark string
float fontfill=.55f;//set opacity of watermark string
Color FontColor= Color.WHITE;//set color of watermark string
String fontSelect="Georgia";//set font for watermark string
File inputFIle=new File(filepath);
if(inputFIle.isDirectory()){
String list[]=inputFIle.list();
for (int i = 0; i < extension="list[i].substring(list[i].length()-3);" filepathdir="filepath+" outputpath="outputFile+" filename="filepath.substring(" outputpath="outputFile+FileName;" source =" ImageIO.read(new" height ="source.getHeight();" width ="source.getWidth();" x="0.00000000;" y="0.00000000;" widthreturn="0;" hightreturn="0;">height){
x=(MaxWidth/width);
height=height*x;
hightreturn=(int)Math.ceil(height);
if(hightreturn>MaxHeight){
y=MaxHeight/height;
width=MaxWidth*y;
widthreturn=(int)Math.ceil(width);
hightreturn=MaxHeight;
}

}else{
y=MaxHeight/height;
width=width*y;
widthreturn=(int)Math.ceil(width);
if(widthreturn>MaxWidth){
x=MaxWidth/width;
height=MaxHeight*x;
hightreturn=(int)Math.ceil(height);
widthreturn=MaxWidth;
}
}
BufferedImage resized =resize(source,
widthreturn,
hightreturn,
RenderingHints.VALUE_INTERPOLATION_BICUBIC,
Logo,logofill,
watermark,
positonx,
positony,
fontSize,
FontColor,
fontfill,
logoPositionx,
logoPositionY,
fontSelect);
writeJPEG(resized,outputPath,quality);
} catch (Exception e) {
e.printStackTrace();
}

}

public static BufferedImage resize(BufferedImage source,
int destWidth,
int destHeight,
Object interpolation,
String logo,
float logofill,
String watermark,
float positonx,
float positony,
int fontSize,
Color FontColor,
float fontfill,
int logoPositionx,
int logoPositionY,
String fontSelect) {
if (source == null)
throw new NullPointerException("source image is NULL!");
if (destWidth <= 0 && destHeight <= 0)
throw new IllegalArgumentException("destination width & height are both <=0!");
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
double xScale = ((double) destWidth) / (double) sourceWidth;
double yScale = ((double) destHeight) / (double) sourceHeight;
if (destWidth <= 0) {
xScale = yScale;
destWidth = (int) Math.rint(xScale * sourceWidth);
}
if (destHeight <= 0) {
yScale = xScale;
destHeight = (int) Math.rint(yScale * sourceHeight);
}
GraphicsConfiguration gc = getDefaultConfiguration();
BufferedImage result =
gc.createCompatibleImage(destWidth,
destHeight,
source.getColorModel().getTransparency());
Graphics2D g2d = null;
try {
g2d = result.createGraphics();
g2d.setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
interpolation);
AffineTransform at =AffineTransform.getScaleInstance(xScale, yScale);
g2d.drawRenderedImage(source, at);
BufferedImage im2;
try {
//draw logo over main image
im2 = ImageIO.read(new File(logo));
g2d.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER,
logofill));
g2d.drawImage(im2, logoPositionx, logoPositionY, null);
//draw watermark string over main image
Font font = new Font(fontSelect, Font.BOLD, fontSize);
g2d.setFont(font);
g2d.setColor(FontColor);
g2d.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER,
fontfill ));
g2d.drawString(watermark, positonx,positony);
} catch (IOException ex) {
ex.printStackTrace();
}
} finally {
if (g2d != null)
g2d.dispose();
}
return result;
}

public static GraphicsConfiguration getDefaultConfiguration() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
return gd.getDefaultConfiguration();
}


public static void writeJPEG(BufferedImage input,
String name,
float quality) throws IOException {
Iterator iter =ImageIO.getImageWritersByFormatName("JPG");
if (iter.hasNext()) {
ImageWriter writer = (ImageWriter) iter.next();
ImageWriteParam iwp =writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(quality);
File outFile = new File(name);
FileImageOutputStream output =new FileImageOutputStream(outFile);
writer.setOutput(output);
IIOImage image = new IIOImage(input, null, null);
writer.write(null, image, iwp);
output.close();
}
}
}

1 comment: