這是個很常用的操作,一般我們的網站在用戶上傳圖片時都會給圖片添加一個水印以防止其他站點盜圖的行為
實現功能:①給圖片增加文字水印②給圖片增加圖片水印
一、核心功能實現:
1、添加文字水印
// 加文字水印
public void mark(BufferedImage bufImg, Image img, String text, Font font, Color color, int x, int y) {
Graphics2D g = bufImg.createGraphics();
g.drawImage(img, 0, 0, bufImg.getWidth(), bufImg.getHeight(), null);
g.setColor(color);
g.setFont(font);
g.drawString(text, x, y);
g.dispose();
}
2、添加圖片水印
// 加圖片水印
public void mark(BufferedImage bufImg, Image img, Image markImg, int width, int height, int x, int y) {
Graphics2D g = bufImg.createGraphics();
g.drawImage(img, 0, 0, bufImg.getWidth(), bufImg.getHeight(), null);
g.drawImage(markImg, x, y, width, height, null);
g.dispose();
}
以上兩個方法實現了最核心的功能:給圖片添加水印。
補充:
為什么要這么寫?
考慮到該方法不僅可以用於給圖片文件增加水印,而且還可以給視頻的每一幀圖像也添加,所以為了方便不同場合重復使用,去除了不必要的依賴關系。
有了核心的兩個方法就我們可以給圖片文件增加水印了
二、功能性實現
1、給圖片增加文字水印
/**
* 給圖片增加文字水印
*
* @param imgPath
* -要添加水印的圖片路徑
* @param outImgPath
* -輸出路徑
* @param text-文字
* @param font
* -字體
* @param color
* -顏色
* @param x
* -文字位於當前圖片的橫坐標
* @param y
* -文字位於當前圖片的豎坐標
*/
public void mark(String imgPath, String outImgPath, String text, Font font, Color color, int x, int y) {
try {
// 讀取原圖片信息
File imgFile = null;
Image img = null;
if (imgPath != null) {
imgFile = new File(imgPath);
}
if (imgFile != null && imgFile.exists() && imgFile.isFile() && imgFile.canRead()) {
img = ImageIO.read(imgFile);
}
int imgWidth = img.getWidth(null);
int imgHeight = img.getHeight(null);
// 加水印
BufferedImage bufImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
mark(bufImg, img, text, font, color, x, y);
// 輸出圖片
FileOutputStream outImgStream = new FileOutputStream(outImgPath);
ImageIO.write(bufImg, "jpg", outImgStream);
outImgStream.flush();
outImgStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
2、給圖片增加圖片水印
/**
* 給圖片增加圖片水印
*
* @param inputImg
* -源圖片,要添加水印的圖片
* @param markImg
* - 水印圖片
* @param outputImg
* -輸出圖片(可以是源圖片)
* @param width
* - 水印圖片寬度
* @param height
* -水印圖片高度
* @param x
* -橫坐標,相對於源圖片
* @param y
* -縱坐標,同上
*/
public void mark(String inputImg, String markImg, String outputImg, int width, int height, int x, int y) {
// 讀取原圖片信息
File inputImgFile = null;
File markImgFile = null;
Image img = null;
Image mark = null;
try {
if (inputImg != null && markImg != null) {
inputImgFile = new File(inputImg);
markImgFile = new File(markImg);
}
if (inputImgFile != null && inputImgFile.exists() && inputImgFile.isFile() && inputImgFile.canRead()) {
img = ImageIO.read(inputImgFile);
}
if (markImgFile != null && markImgFile.exists() && markImgFile.isFile() && markImgFile.canRead()) {
mark = ImageIO.read(markImgFile);
}
int imgWidth = img.getWidth(null);
int imgHeight = img.getHeight(null);
BufferedImage bufImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
mark(bufImg, img, mark, width, height, x, y);
FileOutputStream outImgStream = new FileOutputStream(outputImg);
ImageIO.write(bufImg, "jpg", outImgStream);
outImgStream.flush();
outImgStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
3、測試一下效果
文字水印的字體和顏色需要自行定義,這里我們使用宋體,14號字體,顏色選擇橙色 - -!,坐標是x軸0,y軸等於字體的大小,也就是圖片的左上角。
public static void main(String[] args) {
Font font = new Font("宋體", Font.PLAIN, 14);
// 原圖位置, 輸出圖片位置, 水印文字顏色, 水印文字
// new MarkText4J().mark("eguidMarkText2.jpg", "eguidMarkText2.jpg", "水印效果測試", font, Color.ORANGE, 0, 14);
// 增加圖片水印
new MarkText4J().mark("eguidMarkText2.jpg", "eguid.jpg", "eguidMarkText3.jpg", 40, 20, 0, 14);
}
本文轉載自:https://blog.csdn.net/eguid_1/article/details/52973508
package com.sm.utils; import java.awt.AlphaComposite; 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.OutputStream; import javax.imageio.ImageIO; import javax.swing.ImageIcon; /** * * 生成水印 * */ public class ImageMarkUtils { /** 水印透明度 */ private static float alpha = 0.5f; /** 水印圖片旋轉角度 */ private static double degree = 0f; private static int interval = 0; /** * 設置水印參數,不設置就使用默認值 * * @param alpha * 水印透明度 * @param degree * 水印圖片旋轉角度 * * @param interval * 水印圖片間隔 */ public static void setImageMarkOptions(float alpha, int degree, int interval) { if (alpha != 0.0f) { ImageMarkUtils.alpha = alpha; } if (degree != 0f) { ImageMarkUtils.degree = degree; } if (interval != 0f) { ImageMarkUtils.interval = interval; } } /** * 給圖片添加水印圖片 * * @param waterImgPath * 水印圖片路徑 * @param srcImgPath * 源圖片路徑 * @param targerPath * 目標圖片路徑 */ public static void waterMarkByImg(String waterImgPath, String srcImgPath, String targerPath) throws Exception { waterMarkByImg(waterImgPath, srcImgPath, targerPath, 0); } /** * 給圖片添加水印圖片 * * @param waterImgPath * 水印圖片路徑 * @param srcImgPath * 源圖片路徑 * @param targerPath * 目標圖片路徑 */ public static void waterMarkByImg(String waterImgPath, String srcImgPath) { try { waterMarkByImg(waterImgPath, srcImgPath, srcImgPath, 0); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 給圖片添加水印圖片、可設置水印圖片旋轉角度 * * @param waterImgPath * 水印圖片路徑 * @param srcImgPath * 源圖片路徑 * @param targerPath * 目標圖片路徑 * @param degree * 水印圖片旋轉角度 */ public static void waterMarkByImg(String waterImgPath, String srcImgPath, String targerPath, double degree) throws Exception { 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 (0 != degree) { g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2); } // 4、水印圖片的路徑 水印圖片一般為gif或者png的,這樣可設置透明度 ImageIcon imgIcon = new ImageIcon(waterImgPath); // 5、得到Image對象。 Image img = imgIcon.getImage(); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 6、水印圖片的位置 for (int height = interval + imgIcon.getIconHeight(); height < buffImg.getHeight(); height = height + interval + imgIcon.getIconHeight()) { for (int weight = interval + imgIcon.getIconWidth(); weight < buffImg.getWidth(); weight = weight + interval + imgIcon.getIconWidth()) { g.drawImage(img, weight - imgIcon.getIconWidth(), height - imgIcon.getIconHeight(), null); } } g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); // 7、釋放資源 g.dispose(); // 8、生成圖片 os = new FileOutputStream(targerPath); ImageIO.write(buffImg, "JPG", os); System.out.println("圖片完成添加水印圖片"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != os) os.close(); } catch (Exception e) { e.printStackTrace(); } } } public static void main(String[] args) { System.out.println("..添加水印圖片開始..."); /** * watermarkPath 水印圖片地址 加水印圖片地址 上傳成功后文件地址 */ // 修改默認參數 String imgPath = "C:/xym/abc.jpg";// 測試需加水印圖片 String watermarkPath = "C:/xym/xinyimei.png"; // 測試水印圖片 ImageMarkUtils.setImageMarkOptions(0.0f, 0, 20); ImageMarkUtils.waterMarkByImg(watermarkPath, imgPath); System.out.println("..添加水印圖片結束..."); } }
