import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
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.*;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
/**
* 功能:图片水印工具类
*
* @Date 2022/3/4
* @Version 1.0
*/
public class ImageRemarkUtil {
// 水印透明度
private static float alpha = 0.5f;
// 水印横向位置
private static int positionWidth = 100;
// 水印纵向位置
private static int positionHeight = 100;
// 水印文字字体
private static Font font = new Font("宋体", Font.BOLD, 72);
// 水印文字颜色
private static Color color = Color.red;
/**
* @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)
ImageRemarkUtil.alpha = alpha;
if (positionWidth != 0)
ImageRemarkUtil.positionWidth = positionWidth;
if (positionHeight != 0)
ImageRemarkUtil.positionHeight = positionHeight;
if (font != null)
ImageRemarkUtil.font = font;
if (color != null)
ImageRemarkUtil.color = color;
}
/**
* 给图片添加水印图片
*
* @param iconPath 水印图片路径
* @param srcImgPath 源图片路径
* @param targerPath 目标图片路径
*/
public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath) {
markImageByIcon(iconPath, srcImgPath, targerPath, null);
}
/**
* 给图片添加水印图片、可设置水印图片旋转角度
*
* @param iconPath 水印图片路径
* @param srcImgPath 源图片路径
* @param targerPath 目标图片路径
* @param degree 水印图片旋转角度
*/
public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath, Integer degree) {
OutputStream os = null;
try {
Image srcImg = ImageIO.read(new File(srcImgPath));
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、水印图片的位置
g.drawImage(img, positionWidth, positionHeight, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
// 7、释放资源
g.dispose();
// 8、生成图片
os = new FileOutputStream(targerPath);
ImageIO.write(buffImg, "JPG", os);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 给图片添加水印文字
*
* @param logoText 水印文字
* @param srcImgPath 源图片路径
* @param targerPath 目标图片路径
*/
public static void markImageByText(String logoText, String srcImgPath, String targerPath) {
markImageByText(logoText, srcImgPath, targerPath, null);
}
/**
* 给网络图片添加水印文字
*
* @param logoText 水印文字
* @param srcImgPath 源图片路径
* @param targerPath 目标图片路径
*/
public static void markNetImageByText(String logoText, String srcImgPath, String targerPath, String watermarkPosition) {
markNetImageByText(logoText, srcImgPath, targerPath, null, watermarkPosition);
}
/**
* 网络图片转base64
*
* @param logoText
* @param srcImgPath
* @return
*/
public static String markNetImageToBase64(String logoText, String srcImgPath) {
return markNetImageToBase64(logoText, srcImgPath, null);
}
/**
* InputStream转base64
*
* @param logoText
* @param is
* @return
*/
public static String markIoToBase64(String logoText, InputStream is, String watermarkPosition) {
return markIoToBase64(logoText, is, null, watermarkPosition);
}
/**
* InputStream 转base64
*
* @param is
* @return
*/
public static String ioToBase64(InputStream is) {
String strBase64 = null;
try {
// in.available()返回文件的字节长度
byte[] bytes = new byte[is.available()];
// 将文件中的内容读入到数组中
is.read(bytes);
strBase64 = new BASE64Encoder().encode(bytes); // 将字节流数组转换为字符串
is.close();
} catch (FileNotFoundException fe) {
fe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return strBase64;
}
/**
* 给图片添加水印转换成base64
*
* @param logoText
* @param srcImgPath
* @return
*/
public static String markImageToBase64(String logoText, String srcImgPath) {
return markImageToBase64(logoText, srcImgPath, null);
}
/**
* 给图片添加水印文字、可设置水印文字的旋转角度
*
* @param logoText 水印文字
* @param srcImgPath 图片地址
* @param targerPath 存放地址
* @param degree 旋转角度
*/
public static void markImageByText(String logoText, String srcImgPath, String targerPath, Integer degree) {
InputStream is = null;
OutputStream os = null;
try {
// 1、源图片
Image srcImg = ImageIO.read(new File(srcImgPath));
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)
g.drawString(logoText, positionWidth, positionHeight);
// 9、释放资源
g.dispose();
// 10、生成图片
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ImageIO.write(buffImg, "JPG", stream);
os = new FileOutputStream(targerPath);
ImageIO.write(buffImg, "JPG", os);
} 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();
}
}
}
/**
* 给图片添加水印文字、转成base64
*
* @param logoText
* @param srcImgPath
* @param degree
* @return
*/
public static String markImageToBase64(String logoText, String srcImgPath, Integer degree) {
InputStream is = null;
try {
// 1、源图片
Image srcImg = ImageIO.read(new File(srcImgPath));
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)
g.drawString(logoText, positionWidth, positionHeight);
// 9、释放资源
g.dispose();
// 10、生成图片
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ImageIO.write(buffImg, "JPG", stream);
String base64 = Base64.encode(stream.toByteArray());
return base64;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is)
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* InputStream 转base64加上水印
*
* @param logoText
* @param is
* @param degree
* @return
*/
public static String markIoToBase64(String logoText, InputStream is, Integer degree, String watermarkPosition) {
try {
// 1、源图片
Image srcImg = ImageIO.read(is);
int srcWidth = srcImg.getWidth(null);
int srcHeight = srcImg.getHeight(null);
double dmarkWidth = srcWidth * 0.2;// 设置水印的宽度为图片宽度的0.2倍
double dmarkHeight = dmarkWidth * ((double) srcHeight / (double) srcHeight);//强转为double计算宽高比例
int imarkWidth = (int) dmarkWidth;
int imarkHeight = (int) dmarkHeight;
int x;
int y;
switch (watermarkPosition) {
case "topLeft":
x = 100;
y = 100;
break;
case "topRight":
x = srcWidth - imarkWidth;
y = 100;
break;
case "bottomLeft":
x = 100;
y = srcHeight - imarkHeight / 4;
break;
case "bottomRight":
x = srcWidth - imarkWidth;
y = srcHeight - imarkHeight / 4;
break;
default:
x = srcWidth - imarkWidth;
y = srcHeight - imarkHeight / 4;
break;
}
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)
g.drawString(logoText, x, y);
// 9、释放资源
g.dispose();
// 10、生成图片
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ImageIO.write(buffImg, "JPG", stream);
String base64 = Base64.encode(stream.toByteArray());
return base64;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is)
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 给网络图片添加水印文字、可设置水印文字的旋转角度
*
* @param logoText
* @param srcImgPath
* @param targerPath
* @param degree
*/
public static void markNetImageByText(String logoText, String srcImgPath, String targerPath, Integer degree, String watermarkPosition) {
InputStream is = null;
OutputStream os = null;
try {
// 1、源图片
File file = getFile(srcImgPath);
Image srcImg = ImageIO.read(file);
int srcWidth = srcImg.getWidth(null);
int srcHeight = srcImg.getHeight(null);
double dmarkWidth = srcWidth * 0.2;// 设置水印的宽度为图片宽度的0.2倍
double dmarkHeight = dmarkWidth * ((double) srcHeight / (double) srcHeight);//强转为double计算宽高比例
int imarkWidth = (int) dmarkWidth;
int imarkHeight = (int) dmarkHeight;
int x;
int y;
switch (watermarkPosition) {
case "topLeft":
x = 100;
y = 100;
break;
case "topRight":
x = srcWidth - imarkWidth;
y = 100;
break;
case "bottomLeft":
x = 100;
y = srcHeight - imarkHeight / 4;
break;
case "bottomRight":
x = srcWidth - imarkWidth;
y = srcHeight - imarkHeight / 4;
break;
default:
x = srcWidth - imarkWidth;
y = srcHeight - imarkHeight / 4;
break;
}
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)
g.drawString(logoText, x, y);
// 9、释放资源
g.dispose();
// 10、生成图片
os = new FileOutputStream(targerPath);
ImageIO.write(buffImg, "JPG", os);
} 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();
}
}
}
/**
* 网络图片转base64
*
* @param logoText
* @param srcImgPath
* @param degree
* @return
*/
public static String markNetImageToBase64(String logoText, String srcImgPath, Integer degree) {
InputStream is = null;
try {
// 1、源图片
File file = getFile(srcImgPath);
Image srcImg = ImageIO.read(file);
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)
g.drawString(logoText, positionWidth, positionHeight);
// 9、释放资源
g.dispose();
// 10、生成图片
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ImageIO.write(buffImg, "JPG", stream);
String base64 = Base64.encode(stream.toByteArray());
return base64;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is)
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 将网络图片转file文件
*
* @param url
* @return
*/
public static File getFile(String url) {
//对本地文件命名
String fileName = url.substring(url.lastIndexOf("."));
File file = null;
URL urlfile;
InputStream inStream = null;
OutputStream os = null;
try {
file = File.createTempFile("net_url", fileName);
//下载
urlfile = new URL(url);
inStream = urlfile.openStream();
os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os) {
os.close();
}
if (null != inStream) {
inStream.close();
}
if (null != file) {
file.deleteOnExit(); //程序退出时删除临时文件
}
} catch (Exception e) {
e.printStackTrace();
}
}
return file;
}
/**
* base64字符串转图片
*
* @param base64ImgData
* @param filePath
* @throws IOException
*/
public static void base64StrToImage(String base64ImgData, String filePath) throws IOException {
BASE64Decoder d = new BASE64Decoder();
byte[] bs = d.decodeBuffer(base64ImgData);
FileOutputStream os = new FileOutputStream(filePath);
os.write(bs);
os.close();
}
/**
* 从本地服务器获取图片转base64
*
* @param localUrl
*/
public static String getLocalFile(String localUrl) {
File file = new File(localUrl);
//判断文件是否存在
if (!file.exists()) {
return null;
}
FileInputStream in = null;
String base64 = null;
try {
in = new FileInputStream(file);
base64 = ioToBase64(in);
return base64;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
Java 图片水印工具类
最新推荐文章于 2024-07-29 13:22:42 发布