import import import import import import import import import import import import import public
java.awt.geom.PathIterator; java.awt.geom.Point2D; javax.imageio.ImageIO; javax.imageio.ImageWriter; javax.imageio.ImageWriteParam; javax.imageio.IIOImage; java.awt.image.BufferedImage; java.awt.*; java.awt.geom.AffineTransform; java.io.IOException; java.io.File; java.util.Iterator; javax.imageio.stream.FileImageOutputStream; 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 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 < list.length; i++) { String extension=list[i].substring(list[i].length()-3); if (extension.equalsIgnoreCase("jpg")) { String filepathDir=filepath+"\\"+list[i]; String outputpath=outputFile+"\\Logo"+list[i]; GalleryPhoto(filepathDir, outputpath, maxImgWidth, maxImgHeight, imageQuality, Logo, logoFill, watermark, positonx,
positony, fontSize, FontColor, fontfill, logoPositionx, logoPositionY, fontSelect );
} } }else{ String FileName=filepath.substring( filepath.lastIndexOf(File.separator), filepath.length()); String outputpath=outputFile+FileName; GalleryPhoto(filepath, outputpath, maxImgWidth, maxImgHeight, imageQuality, Logo, logoFill, watermark, positonx, positony, fontSize, FontColor, fontfill, logoPositionx, logoPositionY, fontSelect); } } catch (Exception e) { e.printStackTrace(); } } public static void GalleryPhoto(String Filepath, String outputPath, int MaxWidth, int MaxHeight, float quality, String Logo, float logofill, String watermark, float positonx, float positony, int fontSize, Color FontColor, float fontfill, int logoPositionx, int logoPositionY, String fontSelect){ try { BufferedImage source = ImageIO.read(new File(Filepath)); // this section adjust the height and widht of the image double height =source.getHeight();
double width =source.getWidth(); double x=0.00000000; double y=0.00000000; int widthreturn=0; int hightreturn=0; if(width>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(); } } }