Java implementation of tilt watermark full picture _java

Source: Internet
Author: User
Tags watermark images

Today encountered a demand for the upload map covered with watermarks, on the internet to find a long time is set in the specified location watermark, the following code is I find the code online, and then transformed into. Let's look at the effect chart first.

Here's how to implement it:
The first step: the use of PS or beauty map software design watermark images, such as:

Step two: Put the following code into the class:

Package org.platform.framework.commons.util;
Import Java.awt.AlphaComposite;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics2D;
Import Java.awt.Image;
Import java.awt.RenderingHints;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;

Import Java.io.OutputStream;
Import Javax.imageio.ImageIO;

Import Javax.swing.ImageIcon;

Import Com.founder.cms.watermark.util.ImageMarkLogoUtil;
 /** * * * * Create watermark */public class Imagemarkutil {/** watermark transparency * * private static float alpha = 0.5f;
 /** watermark Picture Rotation angle * * private static double degree = 0f;

 private static int interval = 0; 
 /** * Set watermark parameters, do not set the default value * * @param alpha * Watermark Transparency * @param degree * watermark Picture Rotation angle * * @param interval * Watermark image interval * * public static void Setimagemarkoptions (float alpha, int degree, int interval) {if (alpha!= 0.0f) {imagemarkutil.al
 PHA = Alpha;
 } if (degree!= 0f) {imagemarkutil.degree = degree; } if (Interval!= 0f) {imagemarkutil.interval = interval;   /** * Add watermark image to Picture * * @param waterimgpath * Watermark Picture Path * @param srcimgpath * source Picture path * @param targerpath *  Target Picture Path */public static void Watermarkbyimg (String waterimgpath, String Srcimgpath, String targerpath) throws Exception
 {watermarkbyimg (Waterimgpath, Srcimgpath, Targerpath, 0); /** * Add watermark image to Picture * * @param waterimgpath * Watermark Picture Path * @param srcimgpath * source Picture path * @param targerpath * target picture Path */public static void Watermarkbyimg (String waterimgpath, String srcimgpath) {try {watermarkbyimg (Waterimgpath, S
 Rcimgpath, Srcimgpath, 0);
 catch (Exception e) {//TODO auto-generated catch block E.printstacktrace (); /** * Add watermark picture to picture, can set watermark picture rotation angle * * @param waterimgpath * Watermark Picture Path * @param srcimgpath * source Picture path * @param targe rpath * Target Picture path * @param degree * watermark Picture Rotation angle * * public static void Watermarkbyimg (String waterimgpath, String srcimg Path, String Targerpath, double degree) throws exception {outputstream OS = null;

 try {Image srcimg = imageio.read (new File (Srcimgpath)); BufferedImage buffimg = new BufferedImage (Srcimg.getwidth (null), srcimg.getheight (null), BUFFEREDIMAGE.TYPE_INT_RGB)

 ;

 1, get the Brush object graphics2d g = Buffimg.creategraphics (); 2, set the jagged edge of the line to handle G.setrenderinghint (Renderinghints.key_interpolation, Renderinghints.value_interpolation_
 bilinear); G.drawimage (srcimg.getscaledinstance (Srcimg.getwidth (null), srcimg. GetHeight (null), Image.scale_smooth), 0, 0, NULL
 ); 3, set the watermark rotation if (0!= degree) {G.rotate (Math.toradians (degree), (double) buffimg.getwidth ()/2, (Double) buffimg. G
 Etheight ()/2);

 ///4, Watermark pictures of the path watermark picture is generally gif or PNG, so that you can set the transparency ImageIcon Imgicon = new ImageIcon (Waterimgpath);
 5, Get Image object.

 Image img = imgicon.getimage ();

 G.setcomposite (Alphacomposite.getinstance (alphacomposite.src_atop, Alpha)); 6, Watermark picture position for (int height = interval + imgicon.geticonheight (); height < buffimg. getheight (); height = HeiGht +interval+ imgicon.geticonheight ()) {for (int weight = interval + imgicon.geticonwidth (); weight < buffimg. get Width (); Weight = weight +interval+ imgicon.geticonwidth ()) {G.drawimage (IMG, Weight-imgicon.geticonwidth (), Height-imgico
 N.geticonheight (), NULL);
 } g.setcomposite (Alphacomposite.getinstance (alphacomposite.src_over));

 7, Release resources G.dispose ();
 8, generate picture OS = new FileOutputStream (Targerpath);

 Imageio.write (buffimg, "JPG", OS);

 System.out.println ("Picture complete add watermark picture");
 catch (Exception e) {e.printstacktrace ();
 Finally {try {if (null!= OS) os.close ();
 catch (Exception e) {e.printstacktrace (); }} public static void Main (string[] args) {System.out.println (".
 Add watermark Picture Start ... ");
 /** * Watermarkpath watermark image address Uploadpath Upload after successful file address///modify Default Parameters//imagemarkutil.setimagemarkoptions (0.0f, 0, 20);
 Imagemarkutil.watermarkbyimg (Watermarkpath, Uploadpath); System.out.println ("..
 Add watermark Picture End ... ");

 }

}

The third step: in the implementation of the successful upload, call the class, such as:

 /**
 * Watermarkpath watermark Image Address
 * Uploadpath upload successful file address *
 *
 imagemarkutil.watermarkbyimg (Watermarkpath, Uploadpath); If you think the watermark interval is too close, you can set the interval by the following code:

//Modify the default parameter
 imagemarkutil.setimagemarkoptions (0.0f, 0,
 );
 Imagemarkutil.watermarkbyimg (Watermarkpath, Uploadpath);

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.