parent
f896fca1be
commit
d8a2d8271d
16 changed files with 571 additions and 97 deletions
@ -0,0 +1,176 @@ |
|||||||
|
package com.hisense.hiatmp.server_api.utils; |
||||||
|
|
||||||
|
import javax.imageio.ImageIO; |
||||||
|
import java.awt.*; |
||||||
|
import java.awt.image.BufferedImage; |
||||||
|
import java.io.File; |
||||||
|
import java.io.FileOutputStream; |
||||||
|
import java.io.IOException; |
||||||
|
import java.net.URL; |
||||||
|
|
||||||
|
/** |
||||||
|
* 添加水印util |
||||||
|
*/ |
||||||
|
public class WatermarkUtil { |
||||||
|
|
||||||
|
/** |
||||||
|
* 添加水印 |
||||||
|
* |
||||||
|
* @param pictureSourceType 图片来源类型 |
||||||
|
* @param watermarkType 水印类型 |
||||||
|
* @param path 图片路径 |
||||||
|
* @param watermark 水印内容(文字水印内容/图片水印的存放路径) |
||||||
|
* @param formatName 图片格式 |
||||||
|
* @param targetPath 输出图片存放路径 |
||||||
|
* @param color 水印颜色 |
||||||
|
*/ |
||||||
|
public static void addWatermark(PictureSourceType pictureSourceType, WatermarkType watermarkType, String path, String watermark, String formatName, String targetPath, Color color) { |
||||||
|
if (null == pictureSourceType) { |
||||||
|
throw new RuntimeException("图片来源类型不能为空"); |
||||||
|
} |
||||||
|
if (null == watermarkType) { |
||||||
|
throw new RuntimeException("水印类型不能为空"); |
||||||
|
} |
||||||
|
Image image; |
||||||
|
if (pictureSourceType == PictureSourceType.LOCAL_PICTURE) { |
||||||
|
// 读取本地图片
|
||||||
|
image = readLocalPicture(path); |
||||||
|
} else { |
||||||
|
// 读取网络图片
|
||||||
|
image = readNetworkPicture(path); |
||||||
|
} |
||||||
|
// 水印处理
|
||||||
|
manageWatermark(image, watermarkType, watermark, formatName, targetPath, color); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
//图片来源类型枚举
|
||||||
|
public enum PictureSourceType { |
||||||
|
//本地图片
|
||||||
|
LOCAL_PICTURE, |
||||||
|
//网络图片
|
||||||
|
NETWORK_PICTURE; |
||||||
|
} |
||||||
|
|
||||||
|
//水印类型枚举
|
||||||
|
public enum WatermarkType { |
||||||
|
//文字水印
|
||||||
|
TEXT_WATERMARK, |
||||||
|
//图片水印
|
||||||
|
IMAGE_WATERMARK; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 读取本地图片 |
||||||
|
* |
||||||
|
* @param path 本地图片存放路径 |
||||||
|
*/ |
||||||
|
public static Image readLocalPicture(String path) { |
||||||
|
if (null == path) { |
||||||
|
throw new RuntimeException("本地图片路径不能为空"); |
||||||
|
} |
||||||
|
// 读取原图片信息 得到文件
|
||||||
|
File srcImgFile = new File(path); |
||||||
|
try { |
||||||
|
// 将文件对象转化为图片对象
|
||||||
|
return ImageIO.read(srcImgFile); |
||||||
|
} catch (IOException e) { |
||||||
|
throw new RuntimeException(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 读取网络图片 |
||||||
|
* |
||||||
|
* @param path 网络图片地址 |
||||||
|
*/ |
||||||
|
public static Image readNetworkPicture(String path) { |
||||||
|
if (null == path) { |
||||||
|
throw new RuntimeException("网络图片路径不能为空"); |
||||||
|
} |
||||||
|
try { |
||||||
|
// 创建一个URL对象,获取网络图片的地址信息
|
||||||
|
URL url = new URL(path); |
||||||
|
// 将URL对象输入流转化为图片对象 (url.openStream()方法,获得一个输入流)
|
||||||
|
BufferedImage bugImg = ImageIO.read(url.openStream()); |
||||||
|
if (null == bugImg) { |
||||||
|
throw new RuntimeException("网络图片地址不正确"); |
||||||
|
} |
||||||
|
return bugImg; |
||||||
|
} catch (IOException e) { |
||||||
|
throw new RuntimeException(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 水印处理 |
||||||
|
* |
||||||
|
* @param image 图片对象 |
||||||
|
* @param watermarkType 水印类型(1-文字水印 2-图片水印) |
||||||
|
* @param watermark 水印内容(文字水印内容/图片水印的存放路径) |
||||||
|
* @param formatName 图片格式 |
||||||
|
* @param tarImgPath 输出图片存放路径 |
||||||
|
* @param color 水印颜色 |
||||||
|
*/ |
||||||
|
public static void manageWatermark(Image image, WatermarkType watermarkType, String watermark, String formatName, String tarImgPath, Color color) { |
||||||
|
int imgWidth = image.getWidth(null); |
||||||
|
int imgHeight = image.getHeight(null); |
||||||
|
BufferedImage bufImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB); |
||||||
|
// 加水印,创建画笔
|
||||||
|
Graphics2D graphics = bufImg.createGraphics(); |
||||||
|
// 绘制原始图片
|
||||||
|
graphics.drawImage(image, 0, 0, imgWidth, imgHeight, null); |
||||||
|
|
||||||
|
// 校验水印的类型
|
||||||
|
if (watermarkType == WatermarkType.TEXT_WATERMARK) { |
||||||
|
if (watermark.isEmpty()) { |
||||||
|
throw new RuntimeException("文字水印内容不能为空"); |
||||||
|
} |
||||||
|
// 添加文字水印:
|
||||||
|
// 根据图片的背景设置水印颜色
|
||||||
|
graphics.setColor(color == null ? Color.RED : color); |
||||||
|
// 设置字体 画笔字体样式为微软雅黑,加粗,文字大小为45pt
|
||||||
|
graphics.setFont(new Font("微软雅黑", Font.BOLD, 45)); |
||||||
|
// 设置水印的坐标(为原图片中间位置)
|
||||||
|
int x = (imgWidth - getWatermarkLength(watermark, graphics)) / 2; |
||||||
|
int y = imgHeight / 2; |
||||||
|
// 画出水印 第一个参数是水印内容,第二个参数是x轴坐标,第三个参数是y轴坐标
|
||||||
|
graphics.drawString(watermark, x, y); |
||||||
|
graphics.dispose(); |
||||||
|
} else { |
||||||
|
// 添加图片水印:
|
||||||
|
if (watermark.isEmpty()) { |
||||||
|
throw new RuntimeException("图片水印存放路径不能为空"); |
||||||
|
} |
||||||
|
Image srcWatermark = readLocalPicture(watermark); |
||||||
|
int watermarkWidth = srcWatermark.getWidth(null); |
||||||
|
int watermarkHeight = srcWatermark.getHeight(null); |
||||||
|
// 设置 alpha 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
|
||||||
|
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.9f)); |
||||||
|
// 绘制水印图片 坐标为中间位置
|
||||||
|
graphics.drawImage(srcWatermark, (imgWidth - watermarkWidth) / 2, (imgHeight - watermarkHeight) / 2, watermarkWidth, watermarkHeight, null); |
||||||
|
graphics.dispose(); |
||||||
|
} |
||||||
|
// 输出图片
|
||||||
|
try { |
||||||
|
FileOutputStream outImgStream = new FileOutputStream(tarImgPath); |
||||||
|
ImageIO.write(bufImg, formatName, outImgStream); |
||||||
|
outImgStream.flush(); |
||||||
|
outImgStream.close(); |
||||||
|
} catch (IOException e) { |
||||||
|
throw new RuntimeException(e); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取水印文字的长度 |
||||||
|
* |
||||||
|
* @param watermarkContent 文字水印内容 |
||||||
|
* @param graphics 图像类 |
||||||
|
*/ |
||||||
|
private static int getWatermarkLength(String watermarkContent, Graphics2D graphics) { |
||||||
|
return graphics.getFontMetrics(graphics.getFont()).charsWidth(watermarkContent.toCharArray(), 0, watermarkContent.length()); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
Loading…
Reference in new issue