Java Implementation Picture Overlay effect

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.