适用于给本地图片添加水印,需要知道图片和水印图片的绝对路径工具类
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class ImageUtil {
public enum positionTypeEnum {
BOTTOM_RIGHT("1", "右下角");
private String key;
private String value;
private positionTypeEnum(String key, String value) {
this.key = key;
this.value = value;
}
}
public static void addText(String imgPath, String outImgPath, String text, Color color, int x, int y) {
try {
File imgFile = null;
Image img = null;
if (imgPath != null) {
imgFile = new File(imgPath);
}
if (imgFile != null && imgFile.exists() && imgFile.isFile() && imgFile.canRead()) {
img = ImageIO.read(imgFile);
}
int imgWidth = img.getWidth(null);
int imgHeight = img.getHeight(null);
BufferedImage bufImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
int size = imgWidth / text.length();
Font font = new Font("斜体", Font.HANGING_BASELINE, size);
mark(bufImg, img, text, font, color, 0, imgHeight - 50);
FileOutputStream outImgStream = new FileOutputStream(outImgPath);
ImageIO.write(bufImg, "jpg", outImgStream);
outImgStream.flush();
outImgStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void addPicture(String inputImg, String markImg, String outputImg, int width, int height, int x, int y) {
File inputImgFile = null;
File markImgFile = null;
Image img = null;
Image mark = null;
try {
if (inputImg != null && markImg != null) {
inputImgFile = new File(inputImg);
markImgFile = new File(markImg);
}
if (inputImgFile != null && inputImgFile.exists() && inputImgFile.isFile() && inputImgFile.canRead()) {
img = ImageIO.read(inputImgFile);
}
if (markImgFile != null && markImgFile.exists() && markImgFile.isFile() && markImgFile.canRead()) {
mark = ImageIO.read(markImgFile);
}
int imgWidth = img.getWidth(null);
int imgHeight = img.getHeight(null);
BufferedImage bufImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
mark(bufImg, img, mark, width, height, x, y);
FileOutputStream outImgStream = new FileOutputStream(outputImg);
ImageIO.write(bufImg, "jpg", outImgStream);
outImgStream.flush();
outImgStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void mark(BufferedImage bufImg, Image img, String text, Font font, Color color, int x, int y) {
Graphics2D g = bufImg.createGraphics();
g.drawImage(img, 0, 0, bufImg.getWidth(), bufImg.getHeight(), null);
g.setColor(color);
g.setFont(font);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
g.drawString(text, x, y);
g.dispose();
}
public static void mark(BufferedImage bufImg, Image img, Image markImg, int width, int height, int x, int y) {
Graphics2D g = bufImg.createGraphics();
g.drawImage(img, 0, 0, bufImg.getWidth(), bufImg.getHeight(), null);
g.drawImage(markImg, x, y, width, height, null);
g.dispose();
}
}
测试类
@Test
public void test3() throws IOException {
String inputImg = "D:\\1.jpg";
String markImg = "D:\\logo.jpg";
String outputImg = "D:\\outputImg.jpg";
ImageUtil.addPicture(inputImg,markImg,outputImg,75,20,60,180);
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-11321.html