好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

效果:

原图

加水印后的图片

废话不多说,直接上代码

代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

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图片加铺网格式文字水印内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/q15102780705/article/details/113307631

查看更多关于java实现给图片加铺满的网格式文字水印的详细内容...

  阅读:15次