Java implementation adds a watermark image and watermark text to the picture __java

Source: Internet
Author: User
Tags set background

public static void Main (string[] args)
{
Createmark ("E://image//a.gif", "e://image//b.jpg");
}

/**
* Add a watermark picture and text to the picture
* @param filePath Original picture file path
* @param watermark Watermark Image File path
* @return Add Success returns True, otherwise return false
*/
public static Boolean Createmark (String FilePath, String watermark)
{
Read the original picture
ImageIcon Imgicon = new ImageIcon (FilePath);
Image theimg = Imgicon.getimage ();
Read the label picture
ImageIcon Watericon = new ImageIcon (watermark);
Image waterimg = Watericon.getimage ();

int width = theimg.getwidth (null);
int height = theimg.getheight (null);

Create a new blank picture with the same size as the original picture
BufferedImage bimage = new BufferedImage (width, height,bufferedimage.type_int_rgb);
Graphics2D g = bimage.creategraphics ();
Set font
Font font = new Font ("Sansserif", font.bold,30);
G.setfont (font);
Set foreground color
G.setcolor (color.red);
Set Background color
G.setbackground (Color.White);
Draw the original picture
G.drawimage (theimg, 0, 0, NULL);
Draw a watermark Map
G.drawimage (waterimg, MB, null);
Reprint
g.DrawString ("People's Republic of China", 10, 10);
G.dispose ();
FileOutputStream out = null;
try {
out = new FileOutputStream (FilePath);
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
JPEGEncodeParam param = Encoder.getdefaultjpegencodeparam (bimage);
Param.setquality (50f, true);
Encoder.encode (Bimage, param);
}
catch (Exception e)
{
return false;
}
Finally
{
if (out!=null)
{
Try
{
Out.close ();
out = null;
}
catch (Exception e)
{}
}
}
return true;
}

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.