package com.mall.util.imagemarkUtils;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
/**
* @className: ImagemarkUtils
* @description: 图片水印工具类
* @date:
* @author:
*/
public class ImagemarkUtils {
// 水印透明度
private static float alpha = 0.5f;
// 水印横向位置
private static int positionWidth = 150;
// 水印纵向位置
private static int positionHeight = 300;
// 水印文字字体
private static Font font = new Font("宋体", Font.BOLD, 50);
// 水印文字颜色
private static Color color = Color.white;
/**
* @param alpha 水印透明度
* @param positionWidth 水印横向位置
* @param positionHeight 水印纵向位置
* @param font 水印文字字体
* @param color 水印文字颜色
*/
public static void setImageMarkOptions(float alpha, int positionWidth, int positionHeight, Font font, Color color) {
if (alpha != 0.0f)
ImagemarkUtils.alpha = alpha;
if (positionWidth != 0)
ImagemarkUtils.positionWidth = positionWidth;
if (positionHeight != 0)
ImagemarkUtils.positionHeight = positionHeight;
if (font != null)
ImagemarkUtils.font = font;
if (color != null)
ImagemarkUtils.color = color;
}
/**
* 给图片添加水印图片、可设置水印图片旋转角度
* @param degree 水印图片旋转角度
*/
public static MultipartFile markImageByIcon(String iconPath , MultipartFile srcfile, Integer degree) {
InputStream is = null;
OutputStream os = null;
try {
InputStream inputImg = srcfile.getInputStream();
Image srcImg = ImageIO.read(inputImg);
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null),
BufferedImage.TYPE_INT_RGB);
// 1、得到画笔对象
Graphics2D g = buffImg.createGraphics();
// 2、设置对线段的锯齿状边缘处理
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);
// 3、设置水印旋转
if (null != degree) {
g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
}
// 4、水印图片的路径 水印图片一般为gif或者png的,这样可设置透明度
ImageIcon imgIcon = new ImageIcon(iconPath);
// 5、得到Image对象。
Image img = imgIcon.getImage();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
// 6、水印图片的位置
positionWidth = srcImg.getWidth(null) - imgIcon.getIconWidth() > 0 ? (srcImg.getWidth(null) - imgIcon.getIconWidth())/2 : 0;
positionHeight = srcImg.getHeight(null) - imgIcon.getIconHeight() > 0 ? (srcImg.getHeight(null) - imgIcon.getIconHeight())/2 : 0;
g.drawImage(img, positionWidth, positionHeight, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
// 7、释放资源
g.dispose();
// 8、生成图片
// os = new FileOutputStream("D:/photo/c31.png");
// ImageIO.write(buffImg, "JPG", os);
// System.out.println("图片完成添加水印图片");
// 获取图片文件名 xxx.png xxx
String originFileName = srcfile.getOriginalFilename();
// 获取原图片后缀 png
int lastSplit = originFileName.lastIndexOf(".");
String suffix = originFileName.substring(lastSplit + 1);
// 获取图片原始信息
String dOriginFileName = srcfile.getOriginalFilename();
String dContentType = srcfile.getContentType();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(buffImg, suffix, imOut);
is = new ByteArrayInputStream(bs.toByteArray());
// 加水印后的文件上传
srcfile = new MockMultipartFile(dOriginFileName, dOriginFileName, dContentType,
is);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is)
is.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return srcfile;
}
/**
* 给图片添加水印文字、可设置水印文字的旋转角度
*
* @param logoText 水印文字
* @param degree 水印图片旋转角度
*/
public static MultipartFile markImageByText(String logoText,MultipartFile srcfile ,Integer degree) {
InputStream iss = null;
OutputStream oss = null;
try {
// 1、源图片
InputStream inputImg = srcfile.getInputStream();
Image srcImg = ImageIO.read(inputImg);
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null),
BufferedImage.TYPE_INT_RGB);
// 2、得到画笔对象
Graphics2D g = buffImg.createGraphics();
// 3、设置对线段的锯齿状边缘处理
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);
// 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)
//水印位置
positionWidth = srcImg.getHeight(null)/4;
positionHeight = srcImg.getHeight(null)/2;
g.drawString(logoText, positionWidth, positionHeight);
// 9、释放资源
g.dispose();
// 10、生成图片
// os = new FileOutputStream(targerPath);
// ImageIO.write(buffImg, "JPG", os);
// System.out.println("图片完成添加水印文字");
// 获取图片文件名 xxx.png xxx
String originFileName = srcfile.getOriginalFilename();
// 获取原图片后缀 png
int lastSplit = originFileName.lastIndexOf(".");
String suffix = originFileName.substring(lastSplit + 1);
// 获取图片原始信息
String dOriginFileName = srcfile.getOriginalFilename();
String dContentType = srcfile.getContentType();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(buffImg, suffix, imOut);
iss = new ByteArrayInputStream(bs.toByteArray());
// 加水印后的文件上传
srcfile = new MockMultipartFile(dOriginFileName, dOriginFileName, dContentType,
iss);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != iss)
iss.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (null != oss)
oss.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return srcfile;
}
/**
* 测试
*
* @param args 参数
* @author
* @date
*/
public static void main(String[] args) {
String srcImgPath = "D:/photo/c1.png";
// String iconPath = "D:/photo/c30.png";
String targerPath="D:/photo/c31.png";
// markImageByText("呵呵呵呵",srcImgPath,targerPath,-35);
// ImagemarkUtils.setImageMarkOptions(0.3f, 1, 1, null, null);
// 给图片添加水印图片
// ImagemarkUtils.markImageByIcon(iconPath, srcImgPath, aaa);
// 给图片添加水印图片,水印图片旋转-45
// markImageByIcon(iconPath, srcImgPath, targerIconPath2, -45);
// URL resource = Resources.class.getClassLoader().getResource("/");
// String path =resource.getPath();
// System.out.println(path);
System.out.println("给图片添加水印图片结束...");
}
}
给上传的图片加水印再返回一个MultipartFile类
最新推荐文章于 2023-07-19 09:15:21 发布