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

2025-05-29 0 69

效果:

原图

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

水印后的图片

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

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java实现给图片加铺满的网格式文字水印 https://www.kuaiidc.com/109346.html

相关文章

发表评论
暂无评论