java对整张图片添加水印
- 把水印铺满整张图片
参考代码
private final static Map<String,Object> imageConfig = getImgDefaultConfig();public static Map<String,Object> getImgDefaultConfig(){Map<String, Object> config = new HashMap<>();config.put("alpha",0.5f); // 水印透明度config.put("fontSize",28); // 水印文字大小config.put("font",new Font("微软雅黑", Font.PLAIN, 28)); // 水印文字字体config.put("color",Color.gray); // 水印文字颜色config.put("xMove",80); // 水印之间的间隔config.put("yMove",80); // 水印之间的间隔config.put("degree",-40);// 旋转角度config.put("formatName","JPG");return config;}/*** 获取文本长度。汉字为1:1,英文和数字为2:1*/private static int getTextLength(String text) {int length = text.length();for (int i = 0; i < text.length(); i++) {String s = String.valueOf(text.charAt(i));if (s.getBytes().length > 1) {length++;}}length = length % 2 == 0 ? length / 2 : length / 2 + 1;return length;}/*** 给图片添加水印文字、可设置水印文字的旋转角度* @param logoText 水印文字* @param inputStream* @param outputStream*/public static void imageByText(String logoText, InputStream inputStream, OutputStream outputStream, Map<String, Object> config) throws IOException {// 源图片Image srcImg = ImageIO.read(inputStream);// 原图宽度int width = srcImg.getWidth(null);// 原图高度int height = srcImg.getHeight(null);BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);// 构建画笔对象Graphics2D g = buffImg.createGraphics();try {// 设置对线段的锯齿状边缘处理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);// 设置水印旋转Integer degree = (Integer) config.getOrDefault("degree", imageConfig.get("degree"));if (null != degree) {g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);}// 设置水印文字颜色g.setColor((Color) config.getOrDefault("color", imageConfig.get("color")));// 设置水印文字Fontg.setFont((Font) config.getOrDefault("font", imageConfig.get("font")));// 设置水印文字透明度g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, (Float) config.getOrDefault("alpha", imageConfig.get("alpha"))));int x = -width / 2;int y;// 字体高度int markHeight = (int) config.getOrDefault("fontSize", imageConfig.get("fontSize"));// 字体长度int markWidth = markHeight * getTextLength(logoText);// 循环添加水印while (x < width * 1.5) {y = -height / 2;while (y < height * 1.5) {g.drawString(logoText, x, y);y += markHeight + (int) config.getOrDefault("yMove", imageConfig.get("yMove"));}x += markWidth + (int) config.getOrDefault("xMove", imageConfig.get("xMove"));}} finally {// 释放资源g.dispose();}// 生成图片ImageIO.write(buffImg, (String) config.getOrDefault("formatName", imageConfig.get("formatName")), outputStream);}
测试
public static void main(String[] args) throws IOException {String logoText = "测试水印-abments";imageByText(logoText, new FileInputStream("F:\\tmp\\1\\test.jpg"), new FileOutputStream("F:\\tmp\\1\\test-水印.jpg"), new HashMap<>());}
效果展示
说明
方法imageByText的参数设置为InputStream inputStream, OutputStream outputStream,是为了方便java中直接输出给响应体对象。
在使用过程如果遇到问题欢迎留言讨论。