Java add watermark text and pictures

Source: Internet
Author: User
Package com.my.utils;
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.IOException;

Import Javax.imageio.ImageIO; public class ByteArray {public static void main (string[] args) {file Srcfile=new file ("C:/users/administrator/des
		ktop/working folder/Picture/screenshot_2015-09-16-13-55-06.png ");
		
		File Pathfile=new file ("c:/users/administrator/desktop/working folder/Picture/fukejibing.png"); Addtextforimg (Srcfile, "This is the test of the text", 50,50,1, "F:/QQQQ."
		
		JPEG ");
	Addimgforimage (Srcfile, Pathfile, MB, 0.5f, "f:/wwww.jpeg"); /** * * @param file target picture * @param text added @param positionx x axis * @param positiony y axis * @param alpha Text Transparency * @param path shows the location of the picture (for example: f:/qqqq.jpeg)/public static void Addtextforimg (File file,string text,int positionx,i NT Positiony,float Alpha, String path) {try {Image SrciMG = imageio.read (file); BufferedImage buffimg = new BufferedImage (Srcimg.getwidth (null), srcimg.getheight (null), Buffere
			 DIMAGE.TYPE_INT_RGB);
	         int wideth = Srcimg.getwidth (null);
			 int height = srcimg.getheight (null); 
			 Graphics2D g = buffimg.creategraphics (); 
			 G.setrenderinghint (renderinghints.key_text_antialiasing, renderinghints.value_text_antialias_on);
			 G.drawimage (srcimg, 0, 0, wideth, height, null); 
			 G.setcolor (New Color (89,87,87));
			 G.setfont (New Font ("founder Orchid Pavilion in Black _gbk", font.bold,54)); 
			 G.setcomposite (Alphacomposite.getinstance (Alphacomposite.src_atop,alpha));
			 g.DrawString (text, Positionx, positiony);
			 g.DrawString (text, positionx*2, positiony*2); 
			 G.dispose ();
		Imageio.write (buffimg, "JPEG", new file (path));//output to file stream} catch (IOException e) {e.printstacktrace (); }/** * * * @param srcfile target picture * @param pathfile watermark Picture * @param positionx x Axis * @param positiony y axis * @pa
	Ram Alpha Picture Transparency * @param path shows the location of the picture (for example: f:/www.jpeg)/public static void Addimgforimage (File srcfile,file pathfile,int positionx,in
			 T positiony,float alpha,string Path) {try {Image srcimg = Imageio.read (srcfile); BufferedImage buffimg = new BufferedImage (Srcimg.getwidth (null), srcimg.getheight (null), BufferedImage.
			 TYPE_INT_RGB);
			 int wideth = Srcimg.getwidth (null);
			 int height = srcimg.getheight (null); 
			 Graphics2D g = buffimg.creategraphics (); 
			 G.setrenderinghint (renderinghints.key_text_antialiasing, renderinghints.value_text_antialias_on);
            G.drawimage (srcimg, 0, 0, wideth, height, null);
            Image Src_biao = Imageio.read (pathfile);
            int wideth_biao = Src_biao.getwidth (null);
            int height_biao = Src_biao.getheight (null); 
            G.setcomposite (Alphacomposite.getinstance (Alphacomposite.src_atop,alpha));
            G.drawimage (Src_biao, Positionx,positiony, Wideth_biao, Height_biao, NULL);
 G.dispose ();           Imageio.write (buffimg, "JPEG", new file (path));//output to file stream} catch (IOException e) {e.printstacktrace ();
 }
	}
}

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.