base64加水印
public static byte[] AddWaterMark(byte[] wjnr, String text) throws Exception {
Font font = new Font("微软雅黑", Font.ITALIC, 19);
Color color = Color.RED;
Float alpha = 0.2f;
text = text + "#" + DateUtil.dateToString(DateUtil.getDBTime(), "yyyy年MM月dd日 hh:mm:ss");
String[] arrText = text.split("#");
InputStream is = null;
OutputStream os = null;
try {
is = new java.io.ByteArrayInputStream(wjnr);
BufferedImage srcImg = ImageIO.read(is);
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);
Integer degree = -40;
g.rotate(Math.toRadians(degree),
(double) buffImg.getWidth() / 5,
(double) buffImg.getHeight() / 5);
g.setColor(color);
g.setFont(font);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
int strHeight = getStringHeight(g);
int height = srcImg.getHeight();
int maxLength = arrText[0].length();
for(int i = 1 ; i < arrText.length; i++){
if(maxLength < arrText[i].length()){
maxLength = arrText[i].length();
}
}
for (int i = 0; i < arrText.length; i++) {
String s = "";
int j = maxLength - arrText[i].length();
int n = isNumeric(arrText[i]);
j = j + n / 2;
for (int k = 0; k < j; k++) {
s = s.concat(" ");
}
StringBuffer str = new StringBuffer(arrText[i]);
str.insert(0, s);
arrText[i] = str.toString();
}
FontMetrics fm = g.getFontMetrics(font);
int x = -50;
int y = 50 + (height - fm.getHeight()) / 2 + fm.getAscent();
for (int i = 0; i < arrText.length; i++) {
char[] strContentArr = arrText[i].toCharArray();
g.drawString(arrText[i].substring(0, strContentArr.length), x, y
+ strHeight * i);
}
g.dispose();
ByteArrayOutputStream bot = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bot);
encoder.encode(buffImg);
byte[] rstByte = bot.toByteArray();
return rstByte;
} catch (Exception e) {
return wjnr;
} finally {
try {
if (null != is) {
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (null != os){
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static int getStringHeight(Graphics2D g) {
int height = g.getFontMetrics().getHeight();
return height;
}
public static int isNumeric(String str){
byte[] array1 = str.getBytes();
int count = 0;
for (int i = 0; i < array1.length ; i++) {
if (array1[i] >= 48 && array1[i] <= 57){
count++;
}
}
return count;
}
}
文字展示方式