import javaawt*;
import javaawtimage*;
import javaio*;
import javaxswing*;
import decjpeg*;
public class WaterMark {
/**
* 给图片添加水印
* @param filePath 需要添加水印的图片的路径
* @param markContent 水印的文字
* @param markContentColor 水印文字的颜色
* @param qualNum 图片质量
* @return
*/
public boolean createMark(String filePathString markContentColor markContentColorfloat qualNumString watermark)
{
ImageIcon imgIcon=new ImageIcon(filePath);
Image theImg =imgIcongetImage();
int width=theImggetWidth(null);
int height= theImggetHeight(null);
//ImageIcon waterIcon=new ImageIcon(watermark);
// Image waterImg =waterIcongetImage();
BufferedImage bimage = new BufferedImage(widthheight BufferedImageTYPE_INT_RGB);
Font font = new Font(新宋体 FontPLAIN );
GraphicsD g=bimagecreateGraphics();
gsetColor(markContentColor);
gsetFont(font);
gsetBackground(Colorwhite);
gdrawImage(theImg null );
// gdrawImage(waterImg width* height null );
gdrawString(markContentwidthheight); //添加水印的文字和设置水印文字出现的内容
gdispose();
try{
FileOutputStream out=new FileOutputStream(filePath);
JPEGImageEncoder encoder =JPEGCodeccreateJPEGEncoder(out);
JPEGEncodeParam param = encodergetDefaultJPEGEncodeParam(bimage);
paramsetQuality(qualNum true);
encoderencode(bimage param);
outclose();
}catch(Exception e)
{ return false; }
return true;
}
public static void main(String arg[]){
WaterMark wk=new WaterMark();
if(wkcreateMark(F:\\资料整理\\图片\\网上图片\\ffcccebdjpgColorREDf)){
Systemoutprintln(制作成功);
}else{
Systemoutprintln(我失败了!);
}
}
}