The previously used java code for adding watermarks to images has some minor issues. Today we fixed the problems in it and added the padding function to reconstruct the code.
Import java. awt. alphacomposite;
Import java. awt. color;
Import java. awt. font;
Import java. awt. graphics2d;
Import java. awt. image;
Import java. awt. geom. affinetransform;
Import java. awt. image. affinetransformop;
Import java. awt. image. bufferedimage;
Import java. io. file;
Import java. io. ioexception;
Import javax. imageio. imageio;
/**
* @ Author eric xu
*
*/
Public final class imageutils {
/**
* Image watermark
* @ Param pressimg watermark image
* @ Param targetimg target image
* @ Param x the correction value is in the middle by default.
* @ Param y the correction value is in the middle by default.
* @ Param alpha transparency
*/
Public final static void pressimage (string pressimg, string targetimg, int x, int y, float alpha ){
Try {
File img = new file (targetimg );
Image src = imageio. read (img );
Int wideth = src. getwidth (null );
Int height = src. getheight (null );
Bufferedimage image = new bufferedimage (wideth, height, bufferedimage. type_int_rgb );
Graphics2d g = image. creategraphics ();
G. drawimage (src, 0, 0, wideth, height, null );
// Watermark file
Image src_biao = imageio. read (new file (pressimg ));
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, (wideth-wideth_biao)/2, (height-height_biao)/2, wi deth_biao, height_biao, null );
// Watermark file ended
G. dispose ();
Imageio. write (bufferedimage) image, "jpg", img );
} Catch (exception e ){
E. printstacktrace ();
}
}
/**
* Text watermark
* @ Param presstext watermark text
* @ Param targetimg target image
* @ Param fontname: Font Name
* @ Param fontstyle font style
* @ Param color font color
* @ Param fontsize font size
* @ Param x correction value
* @ Param y corrected value
* @ Param alpha transparency
*/
Public static void presstext (string presstext, string targetimg, string fontname, int fontstyle, color, int fontsize, int x, int y, float alpha ){
Try {
File img = new file (targetimg );
Image src = imageio. read (img );
Int width = src. getwidth (null );
Int height = src. getheight (null );
Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );
Graphics2d g = image. creategraphics ();
G. drawimage (src, 0, 0, width, height, null );
G. setcolor (color );
G. setfont (new font (fontname, fontstyle, fontsize ));
G. setcomposite (alphacomposite. getinstance (alphacomposite. src_atop, alpha ));
G. drawstring (presstext, (width-(getlength (presstext) * fontsize)/2 + x, (he ight-fontsize)/2 + y );
G. dispose ();
Imageio. write (bufferedimage) image, "jpg", img );
} Catch (exception e ){
E. printstacktrace ();
}
}
/**
* Zoom
* @ Param filepath: Image path
* @ Param height
* @ Param width: width
* @ Param bb: whether to fill in the white when the proportion is incorrect
*/
Public static void resize (string filepath, int height, int width, boolean bb ){
Try {
Double ratio = 0.0; // scaling ratio
File f = new file (filepath );
Bufferedimage bi = imageio. read (f );
Image itemp = bi. getscaledinstance (width, height, bi. scale_smooth );
// Calculate the proportion
If (bi. getheight ()> height) | (bi. getwidth ()> width )){
If (bi. getheight ()> bi. getwidth ()){
Ratio = (new integer (height). doublevalue ()/bi. getheight ();
} Else {
Ratio = (new integer (width). doublevalue ()/bi. getwidth ();
}
Affinetransformop op = new affinetransformop (affinetransform. getscaleinstance (ratio, ratio), null );
Itemp = op. filter (bi, null );
}
If (bb ){
Bufferedimage image =
New bufferedimage (width, height, bufferedimage. type_int_rgb );
Graphics2d g = image. creategraphics ();
G. setcolor (color. white );
G. fillrect (0, 0, width, height );
If (width = itemp. getwidth (null ))
G. drawimage (itemp, 0, (height-itemp. getheight (null)/2, itemp. getwidth (null), itemp. getheight (null), color. white, null );
Else
G. drawimage (itemp, (width-itemp. getwidth (null)/2, 0, itemp. getwidth (null), I temp. getheight (null), color. white, null );
G. dispose ();
Itemp = image;
}
Imageio. write (bufferedimage) itemp, "jpg", f );
} Catch (ioexception e ){
E. printstacktrace ();
}
}
Public static void main (string [] args) throws ioexception {
Pressimage ("g: imgtestsy.jpg", "g: imgtesttest1.jpg", 0, 0, 0.5f );
Presstext ("I am a text watermark", "g: imgtesttest1.jpg", "", 36, color. white, 80, 0, 0, 0.3f );
Resiize ("g: imgtesttest1.jpg", 500,500, true );
}
Public static int getlength (string text ){
Int length = 0;
For (int I = 0; I <text. length (); I ++ ){
If (new string (text. charat (I) + ""). getbytes (). length> 1 ){
Length + = 2;
} Else {
Length + = 1;
}
}
Return length/2;
}
}
The getlength method is used to get the full length of the text. One word is half a word, but this method is not very good.