本博客介绍一下用jdk awt实现图片加文字水印和图片水印的方法
一、图片文字水印
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
/**
* 給图片添加图片
*
* @author liqiang
*
*/
public class WaterMarkIMGUtils {
/**
* @param args
*/
public static void main(String[] args) {
String srcImgPath = "C:/Users/zhaoh/Desktop/test.jpg";
String targerPath1 = "C:/Users/zhaoh/Desktop/target1.jpg";
WaterMarkIMGUtils.setWaterMarkForIMGByFontString(srcImgPath, targerPath1,
Color.blue, "公司内部资料");
}
public static void setWaterMarkForIMGByFontString(String srcImgPath, String targerPath, Color markContentColor, String waterMarkContent){
OutputStream os = null;
try {
Image srcImg = ImageIO.read(new File(srcImgPath));
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null),
BufferedImage.TYPE_INT_RGB);
// 得到画笔对象
Graphics2D g = buffImg.createGraphics();
// 设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0,
0, null);
Font font = new Font("宋体", Font.PLAIN, 20);
g.setColor(markContentColor); // 根据图片的背景设置水印颜色
g.setFont(font);
int srcImgWidth = srcImg.getWidth(null);
int srcImgHeight = srcImg.getHeight(null);
int waterMarkLength = g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
int x = srcImgWidth - waterMarkLength - 3;
int y = srcImgHeight - 3;
g.drawString(waterMarkContent, x, y);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g.dispose();
os = new FileOutputStream(targerPath);
// 生成图片
ImageIO.write(buffImg, "JPG", os);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
原来图片
加上文字水印后图片
二、图片加图片水印
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class WaterMarkIMGUtils {
/**
* 给图片添加水印
* @param iconPath
*
水印图片路径
* @param srcImgPath
*
源图片路径
* @param targerPath
*
目标图片路径
* @param imgType
*
上传附件的类型
*/
public static void setWaterMarkForIMGByIcon(BufferedImage bufferedImage, InputStream in, OutputStream os, String imgType) {
setWaterMarkForIMG(iconPath, srcImgPath, targerPath, null, imgType);
}
/**
* 给图片添加水印
* @date 2019年6月12日下午2:11:11
* @param iconPath
*
水印图片路径
* @param srcImgPath
*
源图片路径
* @param targerPath
*
目标图片路径
* @param degree
*
选择角度
* @param imgType
*
上传附件的类型
*/
public static void setWaterMarkForIMGByIcon(String iconPath, String srcImgPath, String targerPath, Integer degree,String imgType) {
OutputStream os = null;
try {
Image srcImg = ImageIO.read(new File(srcImgPath));
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null),
BufferedImage.TYPE_INT_RGB);
// 得到画笔对象
Graphics2D g = buffImg.createGraphics();
// 设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0,
0, null);
if (null != degree) {
// 设置水印旋转
g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
}
// 水印图象的路径 水印一般为gif或者png的,这样可设置透明度
ImageIcon imgIcon = new ImageIcon(iconPath);
// 得到Image对象。
Image img = imgIcon.getImage();
float alpha = 0.5f; // 透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
// 表示水印图片的位置
g.drawImage(img, 150, 300, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g.dispose();
os = new FileOutputStream(targerPath);
// 生成图片
ImageIO.write(buffImg, imgType, os);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String srcImgPath = "C:/Users/admin/Desktop/test.jpg";
String iconPath = "C:/Users/admin/Desktop/logo.png";
String targerPath1 = "C:/Users/admin/Desktop/target1.jpg";
String targerPath2 = "C:/Users/admin/Desktop/target2.jpg";
// 给图片添加水印
WaterMarkIMGUtils.setWaterMarkForIMGByIcon(iconPath, srcImgPath, targerPath1,"JPG");
// 给图片添加水印,水印旋转-10
WaterMarkIMGUtils.setWaterMarkForIMGByIcon(iconPath, srcImgPath, targerPath2, -10,"JPG");
}
}