水印测试工具
介绍
在程序中添加了一个增加水印的操作,本地测试都ok,但是在实际使用中发现,服务器不行,打印出来都是方块,经过验证发现是没有安装中文字体,安装字体后就ok了。
你以为这就结束了吗?
当然不是
项目现场要部署在4台linux上面,但是安装完字体后说还是不行,然后就教现场的同事怎么安装字体,怎么部署项目,怎么测试
ok 问题解决了
当然还有后续
这个同事被调去别的项目了,正好现场的服务器被重置了,所有服务器,也就是说得从头来过,
最后没办法就写了这么一个工具,用来测试服务器是否安装字体,安装是否成功,软件水印功能是否ok
其实也挺简单的,就是增加水印的一个jar demo
代码
java">import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
/**
* 图片添加水印
*/
public class ImgWaterMark {
// 水印透明度
private float alpha = 0.5f;
// 水印文字大小
private int fontSize = 28;
// 水印文字字体
private Font font = new Font(Font.DIALOG, Font.PLAIN, fontSize);
// 水印文字颜色
private Color color = Color.gray;
// 水印之间的间隔
private int xmove = 200;
// 水印之间的间隔
private int ymove = 200;
private int degree = -40;
private String logoText;
private ByteArrayOutputStream out;
//rgb
public static Color color(int v1,int v2,int v3){
return new Color(v1,v2,v3);
}
//文字加时间
public static String timeText(String logoText){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return logoText + sdf.format(new Date());
}
public ImgWaterMark(float alpha,int fontSize ,Color color,int move,int degree){
this.alpha = alpha;
this.fontSize = fontSize;
this.font = new Font("宋体", Font.PLAIN, this.fontSize);
this.color = color;
this.xmove = move;
this.ymove = move;
this.degree = degree;
}
public ImgWaterMark(String logoText){
this.logoText = logoText;
out = new ByteArrayOutputStream();
}
/**
* 创建图片
* @return
*/
public ImgWaterMark create(InputStream in,String fileType) throws Exception {
try {
Image source = ImageIO.read(in);
int srcWidth = source.getWidth(null);
int srcHeight = source.getHeight(null);
BufferedImage buffImg = new BufferedImage(srcWidth,srcHeight, BufferedImage.TYPE_INT_RGB);
// 得到画笔对象
Graphics2D g = buffImg.createGraphics();
// 设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(source.getScaledInstance(srcWidth, srcHeight, Image.SCALE_SMOOTH),0, 0, null);
// 设置水印旋转
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 = -srcWidth / 2;
int y = -srcHeight / 2;
int markWidth = fontSize * getTextLength(logoText);// 字体长度
int markHeight = fontSize;// 字体高度
// 循环添加水印
while (x < srcWidth * 1.5) {
y = -srcHeight / 2;
while (y < srcHeight * 1.5) {
g.drawString(logoText, x, y);
y += markHeight + ymove;
}
x += markWidth + xmove;
}
// 释放资源
g.dispose();
ImageIO.write(buffImg,fileType,out);
} catch (IOException e) {
throw new IOException("文件读取失败");
}
return this;
}
/**
* 获取文本长度。汉字为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;
}
public byte[] getData(){
return out.toByteArray();
}
public String getDataBase64(){
return Base64.getEncoder().encodeToString(getData());
}
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String logoText = "测试图片水印字体" + sdf.format(new Date());
FileInputStream in = new FileInputStream( "./imgTest.jpg");
ImgWaterMark img = new ImgWaterMark(logoText);
img.create(in,"png");
byte[] data = img.getData();
OutputStream outFile = new FileOutputStream("./resultimgTest.png");
outFile.write(data);
outFile.flush();
outFile.close();
}
}
使用
打好包后直接运行就行了,当前目录下会生成一个带水印的图片
java -jar imgTest.jar