原创在这里!
原图:
添加水印后:
工具类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
@Component
public class ImageUtils {
static Logger logger = LoggerFactory.getLogger(ImageUtils.class);
public void m1(){}
public void addWatermark(String pathName,String tarImgPath,String waterMark,Integer type) throws IOException {
File srcImgFile = new File(pathName);
Image srcImg = ImageIO.read(srcImgFile);
int srcImgWidth = srcImg.getWidth(null);
int srcImgHeight = srcImg.getHeight(null);
logger.info("图片的宽:{}" , srcImgWidth);
logger.info("图片的高:{}" , srcImgHeight);
BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufImg.createGraphics();
g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
if (0==type){
int count = 3;
word(srcImgWidth,srcImgHeight,waterMark,g,count);
}else {
picture(srcImgWidth,srcImgHeight,waterMark,g);
}
FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
ImageIO.write(bufImg, "png", outImgStream);
logger.info("添加水印完成");
outImgStream.flush();
outImgStream.close();
}
public static void word(int srcImgWidth,int srcImgHeight,String waterMarkContent,Graphics2D g,int count){
g.setColor(new Color(255, 255, 255, 128));
g.setFont(new Font("微软雅黑", Font.BOLD, 60));
int xCount = srcImgWidth / (count+2);
int yCount = srcImgHeight / (count+2);
logger.info("水印的宽:{}" , xCount);
logger.info("水印的高:{}" , yCount);
for (int i = 0; i <count ; i++) {
for (int j = 0; j < count; j++) {
Random random = new Random();
int x =xCount*(j+1);
int y =yCount*(i+1);
g.drawString(waterMarkContent, x, y);
}
}
g.dispose();
}
public static void picture(int srcImgWidth,int srcImgHeight,String waterMarkImage,Graphics2D g) throws IOException {
Image srcWaterMark = ImageIO.read(new File(waterMarkImage));
int widthWaterMark= (int) (srcWaterMark.getWidth(null)*0.5);
int heightWaterMark = (int) (srcWaterMark.getHeight(null)*0.5);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
Random random = new Random();
int x = random.nextInt((srcImgWidth - widthWaterMark));
int y = random.nextInt((srcImgHeight - heightWaterMark));
g.drawImage(srcWaterMark, x, y, widthWaterMark, heightWaterMark, null);
g.dispose();
}
public static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
}
static String pathName = "C:\\Users\\Desktop\\m\\cat.png";
static String tarImgPath = "C:\\Users\\Desktop\\m\\cat2.png";
static String waterMarkContent = "java 水印";
static int srcImgWidth;
static int srcImgHeight;
static String waterMarkImage="C:\\Users\\Desktop\\m\\6.jpg";
static int type = 0;
public void main(String[] args) throws IOException {
addWatermark(pathName,tarImgPath,waterMarkContent,type);
}
}