Java to add a watermark to the picture, text watermark __java

Source: Internet
Author: User
Public final class Imageutils ... {
Public Imageutils () ... {

}

Public final static String Getpressimgpath () ... {
Return Applicationcontext.getrealpath ("/template/data/util/shuiyin.gif");
}

/** *//**
* Print the picture to the picture
* @param pressimg--Watermark file
* @param targetimg--target file
* @param x
* @param y
*/
Public final static void Pressimage (String pressimg, string targetimg, 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);

Watermark File
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-x, Height-height_biao-y, Wideth_biao,
Height_biao, NULL);
// /
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 colors
* @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, I NT 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);
String s= "www.qhd.com.cn";
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) ... {

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.