上传oss 给图片添加水印
controller
@PostMapping("/uploadImgWaterMark")
public JsonResult uploadImgWaterMark(@RequestParam(value = "file") MultipartFile file) {
ByteArrayResource contentsAsResource = null;
InputStream inputStream = null;
try {
contentsAsResource = new ByteArrayResource(file.getBytes());
inputStream = new ByteArrayInputStream(contentsAsResource.getByteArray());
ByteArrayOutputStream os = new ByteArrayOutputStream();
//获取图片名称
String fileName = "img_" + MushiRndUtil.getRandomFileName() + ".png";
Image srcImg = ImageIO.read(inputStream);
LocalDateTime now = LocalDateTime.now();
// add by chenxin start 给图片加上水印
Font font = new Font("微软雅黑", Font.PLAIN, 100); //水印字体
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String waterMarkContent = sdf.format(new Date()); //水印内容
Color color = new Color(255, 255, 255, 255);
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, 1, 2, srcImgWidth, srcImgHeight, null);
g.setColor(color); //根据图片的背景设置水印颜色
g.setFont(font); //设置字体
g.drawString(waterMarkContent, 100, 100); //画出水印
g.dispose();
ImageIO.write(bufImg, "jpg", os);
bufImg.flush();
InputStream input=new ByteArrayInputStream(os.toByteArray());
String imgUrl = FileUtilByOSS.putFileAndGetUri(fileName, input);
return JsonResult.ok().add("imgUrl", imgUrl).add("fileName", fileName);
} catch (IOException e) {
e.printStackTrace();
return JsonResult.ok("接口异常");
}
}
util
package com.mushiwang.common.utils;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
public class WaterMarkUtil {
// 水印透明度
private static float alpha = 0.3f;
// 水印横向位置
private static int positionWidth = 50;
// 水印纵向位置
private static int positionHeight = 100;
// 水印文字字体
private static Font font = new Font("宋体", Font.BOLD, 60);
// 水印文字颜色
private static Color color = Color.red;
/**
* 给图片添加水印文字
*
* @param text 水印文字
* @param srcImgPath 源图片路径
* @param targetPath 目标图片路径
*/
public static void markImage(String text, String srcImgPath, String targetPath) {
markImage(text, srcImgPath, targetPath, null);
}
/**
* 给图片添加水印文字、可设置水印文字的旋转角度
*
* @param text 水印文字
* @param srcImgPath 源图片路径
* @param targetPath 目标图片路径
* @param degree 水印旋转
*/
public static void markImage(String text, String srcImgPath, String targetPath, Integer degree) {
OutputStream os = null;
try {
// 0、图片类型
String type = srcImgPath.substring(srcImgPath.indexOf(".") + 1, srcImgPath.length());
// 1、源图片
Image srcImg = ImageIO.read(new File(srcImgPath));
int imgWidth = srcImg.getWidth(null);
int imgHeight = srcImg.getHeight(null);
BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
// 2、得到画笔对象
Graphics2D g = buffImg.createGraphics();
// 3、设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImg.getScaledInstance(imgWidth, imgHeight, Image.SCALE_SMOOTH), 0, 0, null);
// 4、设置水印旋转
if (null != degree) {
g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
}
// 5、设置水印文字颜色
g.setColor(color);
// 6、设置水印文字Font
g.setFont(font);
// 7、设置水印文字透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
// 8、第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y)
//设置水印的坐标
int x = imgWidth - 2 * getWatermarkLength(text, g);
int y = imgHeight - 2 * getWatermarkLength(text, g);
g.drawString(text, x, y); //画出水印
// g.drawString(text, positionWidth, positionHeight);
// 9、释放资源
g.dispose();
// 10、生成图片
os = new FileOutputStream(targetPath);
// ImageIO.write(buffImg, "JPG", os);
ImageIO.write(buffImg, type.toUpperCase(), os);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os) {
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 给图片添加水印文字、可设置水印文字的旋转角度
*
* @param text 水印文字
* @param inputStream 源图片路径
* @param outputStream 目标图片路径
* @param degree 水印旋转
* @param typeName
*/
public static void markImageByIO(String text, InputStream inputStream, OutputStream outputStream,
Integer degree, String typeName) {
try {
// 1、源图片
Image srcImg = ImageIO.read(inputStream);
int imgWidth = srcImg.getWidth(null);
int imgHeight = srcImg.getHeight(null);
BufferedImage buffImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
// 2、得到画笔对象
Graphics2D g = buffImg.createGraphics();
// 3、设置对线段的锯齿状边缘处理
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImg.getScaledInstance(imgWidth, imgHeight, Image.SCALE_SMOOTH), 0, 0, null);
// 4、设置水印旋转
if (null != degree) {
g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
}
// 5、设置水印文字颜色
g.setColor(color);
// 6、设置水印文字Font
g.setFont(font);
// 7、设置水印文字透明度
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
// 8、第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y)
g.drawString(text, positionWidth, positionHeight);
// 9、释放资源
g.dispose();
// 10、生成图片
ImageIO.write(buffImg, typeName.toUpperCase(), outputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
// public static void main(String[] args) {
// String srcImgPath = "d:/002.png";
// String text = "hello world";
//
// String targerTextPath = "d:/002--1.png";
// String targerTextPath2 = "d:/002--2.png";
//
// System.out.println("给图片添加水印文字开始...");
// // 给图片添加水印文字
// markImage(text, srcImgPath, targerTextPath);
// // 给图片添加水印文字,水印文字旋转-45
// markImage(text, srcImgPath, targerTextPath2, -45);
// System.out.println("给图片添加水印文字结束...");
//
// }
public static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
}
}