public static boolean createMark(String filePath, String Content,
Color ContentColor, float qualNum, String water) {
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = imgIcon.getImage();
int width = theImg.getWidth(null);
int height = theImg.getHeight(null);
// ImageIcon waterIcon=new ImageIcon(water);
// Image waterImg =waterIcon.getImage();
BufferedImage bimage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics();
g.setColor(ContentColor);
g.setBackground(Color.white);
g.drawImage(theImg, 0, 0, null);
// g.drawImage(waterImg, width*2, height, null );
g.drawString(Content, width - 100, height - 30); // 添加水印的文字和设置水印文字出现的内容
g.dispose();
try {
FileOutputStream out = new FileOutputStream(filePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(qualNum, true);
encoder.encode(bimage, param);
out.close();
} catch (Exception e) {
return false;
}
return true;
}
这个方法就可以