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;
}