给图片添加水印图标或文字
原文出处已经记不得了,这是根据原文自己敲出来的,实际运行过,可以直接使用,感谢原作者
代码块
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
/**
* Created by czy on 2018/1/9.
* <p>
* 图片水印工具
*/
public class ImageRemarkUtil {
private static Logger logger = LoggerFactory.getLogger(ImageRemarkUtil.class);
//水印透明度
private static float alpha = 0.5f;
//水印横向位置
private static int positionWidth = 150;
//水印纵向位置
private static int positionHeight = 300;
//水印文字字体
private static Font font = new Font("宋体", Font.BOLD, 72);
//水印文字颜色
private static Color color = Color.red;
public static void setImageMarkOptions(float alpha, int positionWidth, int positionHeight, Font font, Color color) {
if (alpha != 0.0f)
ImageRemarkUtil.alpha = alpha;
if (positionHeight != 0)
ImageRemarkUtil.positionHeight = positionHeight;
if (positionWidth != 0)
ImageRemarkUtil.positionWidth = positionWidth;
if (font != null)
ImageRemarkUtil.font = font;
if (color != null)
ImageRemarkUtil.color = color;
}
/**
* 给图片加上水印
*/
public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath) {
markImageByIcon(iconPath, srcImgPath, targerPath, null);
}
/**
* 给图片添加水印图片,可设置水印图片旋转角度
*
* @param iconPath 水印图片路径
* @param srcImgPath 原图路径
* @param targetPath 目标图片路径
* @param degree 水印图片的旋转角度
*/
public static void markImageByIcon(String iconPath, String srcImgPath, String targetPath, Integer degree) {
OutputStream outputStream = null;
try {
Image srcImg = ImageIO.read(new File(srcImgPath));
BufferedImage bufferedImage = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
//1、得到画笔对象
Graphics2D graphics2D = bufferedImage.createGraphics();
//2、设置对线段的锯齿状边缘处理
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
//3、设置水印旋转
if (degree != null) {
graphics2D.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
}
//4、水印图片的路径,水印图片一般为gif或者png的,这样可以设置透明度
ImageIcon imageIcon = new ImageIcon(iconPath);
//5、得到iamge对象
Image img = imageIcon.getImage();
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
//6、水印图片的位置
graphics2D.drawImage(img, positionWidth, positionHeight, null);
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
//7、释放资源
graphics2D.dispose();
//8、生成图片
outputStream = new FileOutputStream(targetPath);
ImageIO.write(bufferedImage, "JPG", outputStream);
System.out.println("图片添加水印完成");
} catch (IOException e) {
e.printStackTrace();
logger.info("添加水印图片过程异常----->"+e.getMessage());
} finally {
try {
if (outputStream != null)
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
logger.info("添加水印图片过程异常,输出流关闭异常----->"+e.getMessage());
}
}
}
/**
* 给图片添加水印文字
* @param logoText 水印文字
* @param srcImgPath 原图片路径
* @param targerPath 目标图片路径
*/
public static void markImageByText(String logoText,String srcImgPath,String targerPath){
markImageByText(logoText,srcImgPath,targerPath,null);
}
public static void markImageByText(String logoText,String srcImgPath,String targerPath,Integer degree){
InputStream inputStream = null;
OutputStream outputStream = null;
try {
//1、原图片
Image srcImg = ImageIO.read(new File(srcImgPath));
BufferedImage bufferedImage = new BufferedImage(srcImg.getWidth(null),srcImg.getHeight(null),BufferedImage.TYPE_INT_RGB);
//2、得到画笔对象
Graphics2D graphics2D = bufferedImage.createGraphics();
//3、设置对线段的锯齿状边缘处理
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
//4、设置水印旋转
if (degree != null) {
graphics2D.rotate(Math.toRadians(degree), (double) bufferedImage.getWidth() / 2, (double) bufferedImage.getHeight() / 2);
}
//5、设置水印文字颜色
graphics2D.setColor(color);
//6、设置水印文字字体
graphics2D.setFont(font);
//7、设置水印文字透明度
graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));
//8、设置文字内容和坐标
graphics2D.drawString(logoText,positionWidth,positionHeight);
//9、释放资源
graphics2D.dispose();
//10、生成图片
outputStream = new FileOutputStream(targerPath);
ImageIO.write(bufferedImage,"JPG",outputStream);
System.out.println("图片添加完成水印文字");
} catch (IOException e) {
e.printStackTrace();
logger.info("添加水印文字过程异常----->"+e.getMessage());
}finally {
try {
if(inputStream != null)
inputStream.close();
}catch (IOException e){
e.printStackTrace();
logger.info("添加水印文字过程异常,输入流关闭异常----->"+e.getMessage());
}
try {
if(outputStream != null)
outputStream.close();
}catch (IOException e){
e.printStackTrace();
logger.info("添加水印文字过程异常,输出流关闭异常----->"+e.getMessage());
}
}
}
public static void main(String[] args) {
String srcImgPath = "D:\\1.jpg";
String logoText = "复印无效";
String iconPath = "D:\\1.png";
String targerTextPath = "D:\\3.jpg";
String targerTextPath2 = "D:\\4.jpg";
String targerIconPath = "D:\\1.jpg";
String targerIconPath2 = "D:\\6.jpg";
System.out.println("添加水印文字开始..");
markImageByText(logoText,srcImgPath,targerTextPath);
markImageByText(logoText,srcImgPath,targerTextPath2,-45);
System.out.println("添加水印文字结束");
System.out.println("添加水印图片开始");
setImageMarkOptions(0.3f,1,1,null,null);
markImageByIcon(iconPath,srcImgPath,targerIconPath);
markImageByIcon(iconPath,srcImgPath,targerIconPath2,-45);
System.out.println("添加水印图片结束");
//以下代码将水印加在右下角
try {
Image img = ImageIO.read(new File(srcImgPath));
int width = img.getWidth(null);
int height = img.getHeight(null);
Image imgIcon = ImageIO.read(new File(iconPath));
int iconWidth = imgIcon.getWidth(null);
int iconHeight = imgIcon.getHeight(null);
System.out.println("添加水印图片开始");
setImageMarkOptions(0.3f, width - iconWidth, height - iconHeight, null, null);
markImageByIcon(iconPath, srcImgPath, targerIconPath);
System.out.println("添加水印图片结束");
} catch (IOException e) {
e.printStackTrace();
}
}
}