Keywords:Enterprise Applications
The customer said today that he wants to add a watermark to the image he uploaded to prevent others from stealing the image. He thinks that his images are very important. So...
Well, if the customer has a need, we can satisfy him. I used to write less image operations APIs, so I have never touched on the Image Watermarking function, but for the current network. These are nothing at all. I searched the internet and found several programs. Now I have reconstructed them to meet my requirements. Now I am releasing them, hope you can give some help to friends in need.
Public final class imageutils ...{
Public imageutils ()...{
}
Public final static string getpressimgpath ()...{
Return applicationcontext. getrealpath ("/template/data/util/shuiyin.gif ");
}
/***//**
* Print the image to the image.
* @ 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 );
Required imageencoder encoder = required codec. createjpegencoder (out );
Encoder. encode (image );
Out. Close ();
} Catch (exception e )...{
E. printstacktrace ();
}
}
/***//**
* Print a text watermark image
* @ Param presstext -- text
* @ Param targetimg -- Target Image
* @ Param fontname -- body 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 );
// 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 );
Required imageencoder encoder = required codec. createjpegencoder (out );
Encoder. encode (image );
Out. Close ();
} Catch (exception e )...{
System. Out. println (E );
}
}
Public static void main (string [] ARGs )...{
Pressimage ("C:/shuiyin/shuiyin.gif", "C:/shuiyin/dsc02342.jpg", 20, 20 );
}
}