Adds an image watermark and a text watermark to an uploaded image.

Source: Internet
Author: User
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 );
}
}

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.