1 ImportJava.awt.AlphaComposite;2 ImportJava.awt.Graphics2D;3 ImportJava.awt.image.BufferedImage;4 ImportJava.io.File;5 Importjava.io.IOException;6 7 ImportJavax.imageio.ImageIO;8 Public classNewimageutils {9 /**Ten * One * @Title: Construct a picture A * @Description: Generate watermark and return java.awt.image.BufferedImage - * @paramfile - * source file (image) the * @paramWaterfile - * Watermark file (image) - * @paramx - * The x offset from the lower right corner + * @paramy - * The y offset from the lower right corner + * @paramAlpha A * Transparency, select value from 0.0~1.0: completely transparent ~ completely opaque at * @returnBufferedImage - * @throwsIOException - */ - Public StaticBufferedImage Watermark (file file, file Waterfile,intXintYfloatAlphathrowsIOException { - //Get Basemap -BufferedImage buffimg =imageio.read (file); in //Get layer Diagram -BufferedImage waterimg =Imageio.read (waterfile); to //Create a Graphics2D object and use it to draw on the Basemap object +Graphics2D g2d =buffimg.creategraphics (); - intWaterimgwidth = Waterimg.getwidth ();//get the width of a layer graph the intWaterimgheight = Waterimg.getheight ();//get the height of the layer graph * //blend and transparent effects in graphics and images $ G2d.setcomposite (alphacomposite.getinstance (alphacomposite.src_atop, Alpha));Panax Notoginseng //Drawing -G2d.drawimage (waterimg, x, Y, Waterimgwidth, Waterimgheight,NULL); theG2d.dispose ();//releasing system resources used by the graphics context + returnbuffimg; A } the + /** - * Output watermark image $ * $ * @parambuffimg - * BufferedImage Object after image Watermark - * @paramSavepath the * Save path after image Watermark - */Wuyi Private voidGeneratewaterfile (bufferedimage buffimg, String savepath) { the inttemp = Savepath.lastindexof (".") + 1; - Try { WuImageio.write (buffimg, savepath.substring (temp),NewFile (Savepath)); -}Catch(IOException E1) { About e1.printstacktrace (); $ } - } - - /** A * + * @paramargs the * @throwsIOException - * IO exception was thrown directly $ * @authorBLS the */ the Public Static voidMain (string[] args)throwsIOException { theString Sourcefilepath = "D://img//di.png"; theString Waterfilepath = "D://img//ceng.png"; -String Savefilepath = "D://img//new.png"; inNewimageutils newimageutils =Newnewimageutils (); the //Build Overlays theBufferedImage buffimg = Newimageutils.watermark (NewFile (Sourcefilepath),NewFile (Waterfilepath), 0, 0, 1.0f); About //Output watermark Image the newimageutils.generatewaterfile (buffimg, savefilepath); the } the}
Java Implementation Picture Overlay effect