This article introduces the Java image to add watermark instance code, Java watermark is very convenient, watermark can be pictures or text, the specific contents are as follows
Package michael.io.image;
Import Java.awt.AlphaComposite;
Import Java.awt.Graphics2D;
Import Java.awt.Image;
Import java.awt.RenderingHints;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Javax.imageio.ImageIO;
Import Javax.swing.ImageIcon;
Import Com.sun.image.codec.jpeg.JPEGCodec;
Import Com.sun.image.codec.jpeg.JPEGImageDecoder;
Import Com.sun.image.codec.jpeg.JPEGImageEncoder; /** * Image watermark * @blog http://sjsky.iteye.com * @author Michael/public class Imagemarklogobyicon {/** * @p
Aram args */public static void main (string[] args) {String Srcimgpath = "D:/test/michael/myblog_01.png";
String IconPath = "D:/test/michael/blog_logo.png";
String Targerpath = "D:/test/michael/img_mark_icon.jpg";
String targerPath2 = "D:/test/michael/img_mark_icon_rotate.jpg"; Add a watermark to a picture imagemarklogobyicOn.markimagebyicon (IconPath, Srcimgpath, Targerpath);
Add a watermark to the picture, the watermark rotates -45 imagemarklogobyicon.markimagebyicon (IconPath, Srcimgpath, TargerPath2,-45); /** * Add watermark to Picture * @param iconpath watermark Picture Path * @param srcimgpath source Picture path * @param targerpath target Picture Path * * Publ IC static void Markimagebyicon (String iconpath, String Srcimgpath, String targerpath) {Markimagebyicon (IconPath, S
Rcimgpath, Targerpath, NULL);
/** * Add watermark to picture, can set watermark picture rotation angle * @param iconpath watermark Picture Path * @param srcimgpath source Picture path * @param targerpath destination picture path * @param degree watermark picture rotation angle */public static void Markimagebyicon (String iconpath, String Srcimgpath, String Targ
Erpath, Integer degree) {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)
;
Get the Brush object//Graphics g= buffimg.getgraphics (); Graphics2d g = buffimg.creategraphics (); Sets jagged edge processing for line segments G.setrenderinghint (Renderinghints.key_interpolation, Renderinghints.value_interpolation_bilinea
R); G.drawimage (srcimg.getscaledinstance (Srcimg.getwidth (null), srcimg. GetHeight (null), Image.scale_smooth), 0, 0, NULL
); if (null!= degree) {//Set watermark rotation g.rotate (Math.toradians (degree), (double) buffimg.getwidth ()/2, (double
) buffimg. GetHeight ()/2);
The path watermark for the watermark image is generally GIF or PNG, which sets the transparency ImageIcon Imgicon = new ImageIcon (IconPath);
Gets the image object.
Image img = imgicon.getimage (); float alpha = 0.5f;
Transparency G.setcomposite (Alphacomposite.getinstance (alphacomposite.src_atop, Alpha));
Represents the location of the watermark Picture G.drawimage (img, MB, NULL);
G.setcomposite (Alphacomposite.getinstance (alphacomposite.src_over));
G.dispose ();
OS = new FileOutputStream (Targerpath);
Generate Picture Imageio.write (buffimg, "JPG", OS); System.out. println ("Picture complete add icon seal ...")
");
catch (Exception e) {e.printstacktrace ();
Finally {try {if (null!= OS) os.close ();
catch (Exception e) {e.printstacktrace (); }
}
}
}
To share one more example:
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.Image;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Javax.imageio.ImageIO;
Import Com.sun.image.codec.jpeg.JPEGCodec;
Import Com.sun.image.codec.jpeg.JPEGImageEncoder; Public final class Imageutils {public Imageutils () {}/** * Public final static String Getpressimgpath () {Retu RN ApplicationContext *. Getrealpath ("/template/data/util/shuiyin.gif"); * * * */** the picture to the picture * * @param pressimg--* watermark file * @param targetimg--Target file * @param x *- -X Coordinates * @param y *--y coordinates */public final static void Pressimage (String pressimg, string targetimg, int x, int y
{try {//target file _file = new file (targetimg);
Image src = imageio.read (_file);
int wideth = Src.getwidth (null);
int height = src.getheight (null); BufferedImage image = New BufferedImage (wideth, height, bufferedimage.tyPE_INT_RGB);
Graphics g = image.creategraphics ();
G.drawimage (SRC, 0, 0, wideth, height, null);
Watermark File _filebiao = new file (pressimg);
Image Src_biao = Imageio.read (_filebiao);
int wideth_biao = Src_biao.getwidth (null);
int height_biao = Src_biao.getheight (null);
G.drawimage (Src_biao, (Wideth-wideth_biao)/2, (Height-height_biao)/2, Wideth_biao, Height_biao, NULL);
Watermark file End G.dispose ();
FileOutputStream out = new FileOutputStream (targetimg);
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
Encoder.encode (image);
Out.close ();
catch (Exception e) {e.printstacktrace ();
/** * Print Text watermark picture * * @param presstext *--Text * @param targetimg--* Target picture * @param fontname- * Font name * @param fontstyle--font style * @param color--* Font Color * @param fontsize-* Font Size * @param x-
-* Offset * @param y */public static void Presstext (String presstext, String targetimg, String fontname, int fontstyle, int color, int fontsize, int x, int y) {try {file _file = new File (targetimg);
Image src = imageio.read (_file);
int wideth = Src.getwidth (null);
int height = src.getheight (null);
BufferedImage image = New BufferedImage (wideth, height, bufferedimage.type_int_rgb);
Graphics g = image.creategraphics ();
G.drawimage (SRC, 0, 0, wideth, height, null);
G.setcolor (color.red);
G.setfont (New Font (FontName, FontStyle, FontSize));
g.DrawString (Presstext, Wideth-fontsize-x, Height-fontsize/2-y);
G.dispose ();
FileOutputStream out = new FileOutputStream (targetimg);
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
Encoder.encode (image);
Out.close ();
catch (Exception e) {System.out.println (e);
} public static void Main (string[] args) {pressimage ("F:/logo.png", "F:/123.jpg", 0, 0);
}
}
Hopefully this article will help you to add a watermark to your picture in the Java language.