package io.github.syske;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
public class WatermarkImgUtils {
public static void main(String[] args) {
System.out.println("开始水印:");
new WatermarkImgUtils().addWatermark("D://20201124213253.jpg", "d:/20201124213253_after.jpg", "仅用于面试证明", "jpg", 1.4d);
System.out.println("水印完成。");
}
public void addWatermark(String sourceImgPath, String tarImgPath, String waterMarkContent, String fileExt, double cos) {
Font font = new Font("宋体", Font.BOLD, 25);
Color markContentColor = Color.red;
Integer degree = 45;
float alpha = 0.5f;
OutputStream outImgStream = null;
try {
File srcImgFile = new File(sourceImgPath);
Image srcImg = ImageIO.read(srcImgFile);
int srcImgWidth = srcImg.getWidth(null);
int srcImgHeight = srcImg.getHeight(null);
BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufImg.createGraphics();
g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
g.setColor(markContentColor);
g.setFont(font);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
if (null != degree) {
g.rotate(Math.toRadians(degree));
}
JLabel label = new JLabel(waterMarkContent);
FontMetrics metrics = label.getFontMetrics(font);
int width = (int) Math.ceil(metrics.stringWidth(label.getText()) / cos);
int rowsNumber = srcImgHeight / width;
int columnsNumber = srcImgWidth / width;
if (rowsNumber < 1) {
rowsNumber = 1;
}
if (columnsNumber < 1) {
columnsNumber = 1;
}
for (int j = 0; j < rowsNumber; j++) {
for (int i = 0; i < columnsNumber; i++) {
g.drawString(waterMarkContent, i * width + j * width, -i * width + j * width);
}
}
g.dispose();
outImgStream = new FileOutputStream(tarImgPath);
ImageIO.write(bufImg, fileExt, outImgStream);
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
} finally {
try {
if (outImgStream != null) {
outImgStream.flush();
outImgStream.close();
}
} catch (Exception e) {
e.printStackTrace();
e.getMessage();
}
}
}
}
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Base64Util {
private transient static Logger log = LoggerFactory.getLogger(Base64Util.class);
public static byte[] base64topng(String imageBase64Str) {
byte[] b1 = null;
BASE64Decoder decoder = new BASE64Decoder();
try {
if (imageBase64Str.indexOf("data:image/jpeg;base64,") != -1) {
b1 = decoder.decodeBuffer(imageBase64Str.replaceAll("data:image/jpeg;base64,", ""));
} else if (imageBase64Str.indexOf("data:image/png;base64,") != -1) {
b1 = decoder.decodeBuffer(imageBase64Str.replaceAll(
"data:image/png;base64,", ""));
} else if (imageBase64Str.indexOf("data:image/jpg;base64,") != -1) {
b1 = decoder.decodeBuffer(imageBase64Str.replaceAll(
"data:image/jpg;base64,", ""));
} else {
b1 = decoder.decodeBuffer(imageBase64Str);
}
for (int i = 0; i < b1.length; ++i) {
if (b1[i] < 0) {
b1[i] += 256;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return b1;
}
public static String filePath(String realPath, String imageStr) {
String imgPath = "";
String imgName = "";
try {
byte[] imageBytes = base64topng(imageStr);
File f1 = new File(realPath);
if (!f1.exists()) {
f1.mkdir();
}
imgName = UUID.randomUUID().toString() + ".jpg";
imgPath = realPath + "/" + imgName;
File f2 = new File(imgPath);
if (!f2.exists()) {
f2.createNewFile();
}
OutputStream out = new FileOutputStream(imgPath);
out.write(imageBytes);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return imgName;
}
public static String GenerateImage(String filePath, String imgData) throws IOException {
String imgName = UUID.randomUUID().toString() + ".jpg";
if (imgData == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
OutputStream out = null;
try {
out = new FileOutputStream(filePath + imgName);
byte[] b = decoder.decodeBuffer(imgData);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
out.write(b);
} catch (IOException e) {
log.error("图片转换失败:" + e);
e.printStackTrace();
} finally {
out.flush();
out.close();
}
return imgName;
}
public static String GetImageStr(String imgPath) {
String imgFile = imgPath;
InputStream in = null;
byte[] data = null;
String encode = null;
BASE64Encoder encoder = new BASE64Encoder();
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
encode = encoder.encode(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
log.error("图片转换失败:" + e);
e.printStackTrace();
}
}
return encode;
}
}