Dynamic jpg png transparent watermark image generated in servlet (JSP)

Source: Internet
Author: User
Tags transparent watermark

Part of the content is taken from the Internet. Select the best part and add it to your own practice. record it for future generations and convenience!

1. Generate JPG Images

Response. setcontenttype ("image/JPEG ");
Int width = 32, Height = 18;

Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );
Graphics G = image. getgraphics ();
G. setcolor (color. White );
G. fillrect (0, 0, width, height );
G. setcolor (color. Red );
G. drawoval (0, 0, width, height );
Servletoutputstream out = response. getoutputstream ();
Required imageencoder encoder = required codec. createjpegencoder (out );
Encoder. encode (image );
Out. Close ()

Similar effects:


White background, the quality is not good!

2. generate transparent PNG Images

Response. setcontenttype ("image/PNG ");
Int width = 32;
Int Height = 18;
// Create a bufferedimage object
Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );
// Obtain graphics2d
Graphics2d g2d = image. creategraphics ();

// ---------- Add the following code to make the background transparent -----------------
Image = g2d. getdeviceconfiguration (). createcompatibleimage (width, height, transparency. translucent );
G2d. Dispose ();
G2d = image. creategraphics ();
// ---------- The background Transparent Code ends -----------------

// Draw
G2d. setcolor (new color (255, 0, 0 ));
G2d. setstroke (New basicstroke (2 ));
G2d. drawline (1, height-3, width-1, height-3 );
G2d. drawstring (strreqnum, width/2-4, height/2 );
// Release the object
G2d. Dispose ();
// Save the file
ImageIO. Write (image, "PNG", response. getoutputstream ());

This effect is good and satisfactory!

3. Watermark Effect

The watermark effect is also relatively large. Just write an example.

Response. setcontenttype ("image/PNG ");
// Obtain the original watermark image
String temp = request. getsession (). getservletcontext (). getrealpath ("");
String filepath = temp + "/image/s.gif ";

// Watermark File
Bufferedimage theimg = ImageIO. Read (new file (filepath ));
Int width = theimg. getwidth (null );
Int Height = theimg. getheight (null );

Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );
Graphics2d g2d = image. creategraphics ();
// ---------- Add the following code to make the background transparent -----------------
Image = g2d. getdeviceconfiguration (). createcompatibleimage (width, height, transparency. translucent );
G2d. Dispose ();
G2d = image. creategraphics ();

G2d. setcolor (new color (255, 0, 0 ));
G2d. setstroke (New basicstroke (1 ));

G2d. setcolor (color. White );
G2d. drawimage (theimg, 0, 0, null );
G2d. setfont (new font ("", Font. bold, 48); // The second parameter changes the bold italic... bold and italic (font. bold | font. italic)
G2d. drawstring ("syx", width/8, height/2); // Add the watermark text and set the content of the watermark text

G2d. Dispose ();
ImageIO. Write (image, "PNG", response. getoutputstream ());

Another way is to paste images on the image. If you want to paste a transparent source image, the source image is also transparent. PNG is the best. Add a similar

G2d. drawimage (IMG, X, Y, width, height, null)

This kind of code should be enough. I have never tried and don't know the transparency effect!

Due to the limit on the number of words in the article, I will not paste the image code!

Related Article

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.