欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现给图片加铺满的网格式文字水印

程序员文章站 2023-01-13 21:39:14
效果:原图加水印后的图片废话不多说,直接上代码代码:package com.example.demo; import java.awt.alphacomposite;import java.awt.c...

效果:

原图

java实现给图片加铺满的网格式文字水印

加水印后的图片

java实现给图片加铺满的网格式文字水印

废话不多说,直接上代码

代码:

package com.example.demo;
 
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.inputstream;
import java.io.outputstream;
 
import javax.imageio.imageio;
 
/**
 * @author xuyangwei
 * @date 2021/1/28 09:10
 */
public class watermarkutils {
 // 水印透明度
 private static float alpha = 0.5f;
 // 水印文字大小
 public static final int font_size = 18;
 // 水印文字字体
 private static font font = new font("宋体", font.plain, font_size);
 // 水印文字颜色
 private static color color = color.gray;
 // 水印之间的间隔
 private static final int xmove = 80;
 // 水印之间的间隔
 private static final int ymove = 80;
 
 /**
  * 给图片添加水印文字
  *
  * @param logotext 水印文字
  * @param srcimgpath 源图片路径
  * @param targerpath 目标图片路径
  */
 public static void imagebytext(string logotext, string srcimgpath, string targerpath) {
  imagebytext(logotext, srcimgpath, targerpath, null);
 }
 
 
 /**
  * 获取文本长度。汉字为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 srcimgpath
  * @param targerpath
  * @param degree
  */
 public static void imagebytext(string logotext, string srcimgpath, string targerpath, integer degree) {
 
  inputstream is = null;
  outputstream os = null;
  try {
   // 源图片
   image srcimg = imageio.read(new file(srcimgpath));
   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();
   // 设置对线段的锯齿状边缘处理
   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);
   }
   // 设置水印文字颜色
   g.setcolor(color);
   // 设置水印文字font
   g.setfont(font);
   // 设置水印文字透明度
   g.setcomposite(alphacomposite.getinstance(alphacomposite.src_atop, alpha));
 
   int x = -width / 2;
   int y = -height / 2;
   int markwidth = font_size * gettextlength(logotext);// 字体长度
   int markheight = font_size;// 字体高度
 
   // 循环添加水印
   while (x < width * 1.5) {
    y = -height / 2;
    while (y < height * 1.5) {
     g.drawstring(logotext, x, y);
 
     y += markheight + ymove;
    }
    x += markwidth + xmove;
   }
   // 释放资源
   g.dispose();
   // 生成图片
   os = new fileoutputstream(targerpath);
   imageio.write(buffimg, "jpg", os);
   system.out.println("添加水印文字成功!");
  } catch (exception e) {
   e.printstacktrace();
  } finally {
   try {
    if (null != is)
     is.close();
   } catch (exception e) {
    e.printstacktrace();
   }
   try {
    if (null != os)
     os.close();
   } catch (exception e) {
    e.printstacktrace();
   }
  }
 }
 
 public static void main(string[] args) {
  string srcimgpath = "d:/1.jpg";
  // 水印文字
  string logotext = "打印无效";
  string targertextpath2 = "d:/2.jpg";
  system.out.println("给图片添加水印文字开始...");
  // 给图片添加斜水印文字
  watermarkutils.imagebytext(logotext, srcimgpath, targertextpath2, -40);
  system.out.println("给图片添加水印文字结束...");
 }
}

总结

到此这篇关于java实现给图片加铺满的网格式文字水印的文章就介绍到这了,更多相关java图片加铺网格式文字水印内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!