日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 批量为图片添加图标水印和文字水印

發布時間:2024/9/27 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 批量为图片添加图标水印和文字水印 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

需求,給指定目錄下面以.jpg結尾的文件,添加圖標水印和文字水印

文章目錄

          • 一、基礎版本
          • 二、升級版本
          • 三、依賴

一、基礎版本
package com.gblfy.util;import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder;import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException;/*** java為圖片添加圖標水印和文字水印** @author gblfy* @version 1.0* @explain 文字水印、圖片水印* @creationTime 2020/07/01*/ public class WaterMarkUtil {//水印圖片路徑private static String WATER_MARKIMG_PATH = "D:" + File.separator + "1" + File.separator + "iconPath" + File.separator + "22.png";//當前登陸操作員 采用全局變量private static String operator;static {operator = "zhangsan";}/*** 將指定圖標(png圖片)印刷到指定圖片上* <p>* 1.圖標圖片格式:* png* 2.坐標軸:* x軸決定左右位置* y軸決定上下位置* 3.坐標位置* x值越大距離右越近,反之,x值越小距離左越近;* y值越大距離越往下,反之,y值越小距離越往上* </p>** @param pressImg 水印圖片* @param targetImg 源圖片路徑的目標文件* @param x x坐標* @param y y坐標*/public final static void pressImage(String pressImg, String targetImg, int x, int y) {try {// 目標文件File imageFile = new File(targetImg);Image src = ImageIO.read(imageFile);int wideth = src.getWidth(null);int height = src.getHeight(null);BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);Graphics g = image.createGraphics();g.drawImage(src, 0, 0, wideth, height, null);// 水印文件File waterMarkImage = new File(pressImg);Image markImage = ImageIO.read(waterMarkImage);int weightMarkImage = markImage.getWidth(null);int heightMarkImage = markImage.getHeight(null);g.drawImage(markImage, x, y, weightMarkImage, heightMarkImage, null);// 水印結束g.dispose();FileOutputStream out = new FileOutputStream(targetImg);JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);encoder.encode(image);out.close();} catch (IOException e) {e.printStackTrace();}}/*** 打印文字水印圖片* <p>* 1.圖標圖片格式:png* 2.坐標軸:* x軸決定左右位置* y軸決定上下位置* 3.坐標位置* x值越大距離右越近,反之,x值越小距離左越近;* y值越大距離越往下,反之,y值越小距離越往上* </p>** @param pressText 文字* @param targetImg 目標圖片* @param fontName 字體名* @param fontStyle 字體樣式* @param color 字體顏色* @param fontSize 字體大小* @param x 偏移量* @param y 偏移量*/public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color,int fontSize, int x, int y) {try {File imageFile = new File(targetImg);Image src = ImageIO.read(imageFile);int weidth = src.getWidth(null);int height = src.getHeight(null);BufferedImage image = new BufferedImage(weidth, height, BufferedImage.TYPE_INT_RGB);Graphics g = image.createGraphics();g.drawImage(src, 0, 0, weidth, height, null);g.setColor(color);g.setFont(new Font(fontName, fontStyle, fontSize));g.drawString(pressText, x, y);g.dispose();FileOutputStream out = new FileOutputStream(targetImg);JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);encoder.encode(image);out.close();} catch (IOException e) {e.printStackTrace();}}/*** 添加圖標水印+文字水印** @param srcImgPath 需要處理的源圖片路徑*/public static void iconAndTextWatermark(String srcImgPath) {// 1.添加圖標水印pressImage(WATER_MARKIMG_PATH, srcImgPath, 100, 500);System.out.println("--------------------添加圖標水印 執行完成!--------------------");String date = TimeUtil.getCurrentDate().toString();String time = TimeUtil.getCurrentTime().toString();// 2.添加文字水印 參數根據真實保單圖片進行調整pressText(operator, srcImgPath, "宋體", 0, Color.BLUE, 30, 540, 600);pressText(date, srcImgPath, "宋體", 0, Color.BLUE, 30, 540, 650);pressText(time, srcImgPath, "宋體", 0, Color.BLUE, 30, 540, 700);System.out.println("--------------------添加文字水印 執行完成!--------------------");}/*** 對.jpg圖片文件進行篩選* <p>* 1.使用遞歸遍歷目錄下面的.jpg結尾圖片文件* 2.對篩選通過的文件調用水印文件處理* </p>** @param path* @return*/public static void getFileListNotReturn(String path) {File dir = new File(path);// 該文件目錄下文件全部放入數組File[] files = dir.listFiles();if (files != null) {for (int i = 0; i < files.length; i++) {String fileName = files[i].getName();// 判斷是文件還是文件夾if (files[i].isDirectory()) {// 獲取文件絕對路徑getFileListNotReturn(files[i].getAbsolutePath());// 判斷文件名是否以.jpg結尾} else if (fileName.endsWith(".jpg")) {String strFileName = files[i].getAbsolutePath();System.out.println("文件名:" + strFileName);//對符合條件的文件統一水印處理iconAndTextWatermark(strFileName);} else {continue;}}}}//--------------------------------------------單元測試--------------------------------------------public static void main(String[] args) {String srcImgPath = "D:\\1\\srcImgPath\\";System.out.println("圖片路徑:" + srcImgPath);getFileListNotReturn(srcImgPath);}/*** 特別說明:* 如上面的效果展示的那樣,要想將水印打印到圖片指定位置,進行動態設置的話,需要:</p>*  第一,原圖片大小;</p>*  第二,水印大小;</p>*  滿足這兩個條件才能將水印打印到圖片的指定位置,也就是可以進行動態位移。&nbsp;</p>*  其中,圖片水印可以進行動態設置,而文字水印則實現不了,因為我們無法獲取文字水印的大小。</p>*  調試水印輸出位置也是個細致活呀。</p>* 另外,水印的偏移量一般情況下均為正值,圖片左上角為起點0,0,不同于數學上的Y軸,向下偏移用正值表示。*/ }
二、升級版本

根據圖片的寬度(分辨率)和高度(分辨率),動態改變圖標水印的寬度(分辨率)和高度(分辨率),適配所有的圖片比例

實現原理:
1.圖標水印
找到圖片寬度(分辨率)和高度(分辨率)和圖標水印的寬度(分辨率)和高度(分辨率)的比例關系即可
2.文字水印同理:
找到文字水印寬度(分辨率)和高度(分辨率)和圖標水印的寬度(分辨率)和高度(分辨率)的比例關系即可

/*** 單證添加圖標水印+文字水印* <p>* 1.使用遞歸遍歷目錄下面的單證圖片文件* 2.對文件的不同元素,做不同處理* 1>如果是文件夾就地柜處理* 2>如果是文件就就進入處理范圍* 3.獲取單證圖片的寬度和高度* 4.推算出新華水印圖標的寬度和高度* 5.調用水印處理和新方法* </p>** @param srcImgPath*/public void getImageParamsAndDeal(String srcImgPath) {File dir = new File(srcImgPath);// 該文件目錄下文件全部放入數組File[] files = dir.listFiles();if (files != null) {for (int i = 0; i < files.length; i++) {//文件的絕對路徑String absolutePath = files[i].getAbsolutePath();// 判斷是文件還是文件夾if (files[i].isDirectory()) {// 文件元素是文件夾遞歸調用自己getImageParamsAndDeal(absolutePath);} else {//對所有的單證圖片進行統一處理String strFileName = absolutePath;System.out.println("文件名:" + strFileName);// 獲取每張圖片的高度 和 寬度File file = new File(strFileName);int[] imgWidth = getImgWidth(file);System.out.println("圖片的寬度:" + imgWidth[0]);System.out.println("圖片的高度:" + imgWidth[1]);//單證的寬度和高度int picWidth = imgWidth[0];int picHeight = imgWidth[1];//新華水印圖標的寬度和高度初始化int picX = 0;int picY = 0;//新華水印圖標的寬度和高度double width = (imgWidth[0] - (picWidth * 0.71)) / 2;double height = (imgWidth[1] - (picHeight * 0.18)) / 2;//新華水印圖標的寬度和高度 取整特殊處理picX = (int) Math.floor(width);picY = (int) Math.floor(height);System.out.println("水印圖標x:" + picX);System.out.println("水印圖標y:" + picY);// 對符合條件的文件統一水印處理iconAndTextWatermark(strFileName, picX, picY, picWidth, picHeight);}}}} /*** 獲取圖片寬度和高度** @param file 圖片文件* @return 寬度*/public static int[] getImgWidth(File file) {InputStream is = null;BufferedImage src = null;int result[] ={0, 0};try {is = new FileInputStream(file);src = javax.imageio.ImageIO.read(is);// 得到源圖寬result[0] = src.getWidth(null);// 得到源圖高result[1] = src.getHeight(null);is.close();} catch (Exception e) {e.printStackTrace();}return result;} /*** 添加圖標水印+文字水印* <p>* 1.新華圖標水印和單證圖片的比例關系* 2.操作員文字水印和新華圖標水印的比例關系* 3.日期文字水印和新華圖標水印的比例關系* 4.時間文字水印和新華圖標水印的比例關系* </p>** @param srcImgPath 需要處理的源圖片路徑*/public static void iconAndTextWatermark(String srcImgPath, int picX, int picY, int picWidth, int picHeight) {File fromFile = new File(nclWaterFilePath);File toFile = new File(nclWaterTempFilePath);//判斷生成水印圖標的絕對路徑路是否存在不存在就創建if (!toFile.exists()) {toFile.mkdirs();}// ImageUtil.resizePng(fromFile, toFile, (int) (1244 * 0.71), (int)(1684 * 0.18), false);resizePng(fromFile, toFile, (int) (picWidth * 0.71), (int) (picHeight * 0.18), false);// 1.添加圖標水印pressImage(nclWaterTempFilePath, srcImgPath, picX, picY);System.out.println("--------------------添加圖標水印 執行完成!--------------------");// 2.添加文字水印 參數根據真實保單圖片進行調整pressText(operator, srcImgPath, "宋體", 0, Color.BLUE, (int) (picHeight * 0.032), picX + (int) (picWidth * 0.57),picY + (int) (picHeight * 0.20));pressText(PubFun.getCurrentDate(), srcImgPath, "宋體", 0, Color.BLUE, (int) (picHeight * 0.032), picX+ (int) (picWidth * 0.57), picY + (int) (picHeight * 0.24));pressText(PubFun.getCurrentTime(), srcImgPath, "宋體", 0, Color.BLUE, (int) (picHeight * 0.032), picX+ (int) (picWidth * 0.57), picY + (int) (picHeight * 0.28));// System.out.println("--------------------添加文字水印 執行完成!--------------------");} /*** 將指定圖標(png圖片)印刷到指定圖片上* <p>* 1.圖標圖片格式:* png* 2.坐標軸:* x軸決定左右位置* y軸決定上下位置* 3.坐標位置* x值越大距離右越近,反之,x值越小距離左越近;* y值越大距離越往下,反之,y值越小距離越往上* </p>** @param pressImg 水印圖片* @param targetImg 源圖片路徑的目標文件* @param x x坐標* @param y y坐標*/public final static void pressImage(String pressImg, String targetImg, int x, int y) {try {// 目標文件File imageFile = new File(targetImg);Image src = ImageIO.read(imageFile);int wideth = src.getWidth(null);int height = src.getHeight(null);BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);Graphics g = image.createGraphics();g.drawImage(src, 0, 0, wideth, height, null);// 水印文件File waterMarkImage = new File(pressImg);Image markImage = ImageIO.read(waterMarkImage);int weightMarkImage = markImage.getWidth(null);int heightMarkImage = markImage.getHeight(null);g.drawImage(markImage, x, y, weightMarkImage, heightMarkImage, null);// 水印結束g.dispose();FileOutputStream out = new FileOutputStream(targetImg);JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);encoder.encode(image);out.close();} catch (IOException e) {e.printStackTrace();}}/*** 打印文字水印圖片* <p>* 1.圖標圖片格式:* png* 2.坐標軸:* x軸決定左右位置* y軸決定上下位置* 3.坐標位置* x值越大距離右越近,反之,x值越小距離左越近;* y值越大距離越往下,反之,y值越小距離越往上* </p>** @param pressText 文字* @param targetImg 目標圖片* @param fontName 字體名* @param fontStyle 字體樣式* @param color 字體顏色* @param fontSize 字體大小* @param x 偏移量* @param y 偏移量*/public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color,int fontSize, int x, int y) {try {File imageFile = new File(targetImg);Image src = ImageIO.read(imageFile);int weidth = src.getWidth(null);int height = src.getHeight(null);BufferedImage image = new BufferedImage(weidth, height, BufferedImage.TYPE_INT_RGB);Graphics g = image.createGraphics();g.drawImage(src, 0, 0, weidth, height, null);g.setColor(color);g.setFont(new Font(fontName, fontStyle, fontSize));g.drawString(pressText, x, y);g.dispose();FileOutputStream out = new FileOutputStream(targetImg);JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);encoder.encode(image);out.close();} catch (IOException e) {e.printStackTrace();}} /*** 任意壓縮PNG圖片* <p>* 1.用于修改PNG水印圖標的寬度(分辨率)和寬度(分辨率)* 2.動態適配單證圖片寬度(分辨率)和寬度(分辨率)** @param fromFile 源文件* @param toFile 裁剪后的文件* @param outputWidth 裁剪寬度* @param outputHeight 裁剪高度* @param proportion 是否是等比縮放*/public static void resizePng(File fromFile, File toFile, int outputWidth, int outputHeight, boolean proportion) {try {BufferedImage bi2 = ImageIO.read(fromFile);int newWidth;int newHeight;// 判斷是否是等比縮放if (proportion) {// 為等比縮放計算輸出的圖片寬度及高度double rate1 = ((double) bi2.getWidth(null)) / (double) outputWidth + 0.1;double rate2 = ((double) bi2.getHeight(null)) / (double) outputHeight + 0.1;// 根據縮放比率大的進行縮放控制double rate = rate1 < rate2 ? rate1 : rate2;newWidth = (int) (((double) bi2.getWidth(null)) / rate);newHeight = (int) (((double) bi2.getHeight(null)) / rate);} else {// 輸出的圖片寬度newWidth = outputWidth;// 輸出的圖片高度newHeight = outputHeight;}BufferedImage to = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);Graphics2D g2d = to.createGraphics();to = g2d.getDeviceConfiguration().createCompatibleImage(newWidth, newHeight, Transparency.TRANSLUCENT);g2d.dispose();g2d = to.createGraphics();@SuppressWarnings("static-access")Image from = bi2.getScaledInstance(newWidth, newHeight, bi2.SCALE_AREA_AVERAGING);g2d.drawImage(from, 0, 0, null);g2d.dispose();ImageIO.write(to, "png", toFile);} catch (Exception e) {e.printStackTrace();}}

測試
只需要傳遞需要添加水印的目錄即可

public static void main(String[] args) {Bl bl=new Bl()//1.批量添加水印bl.getImageParamsAndDeal(srcImgPath);}
三、依賴
jimi-1.0.jar jai_imageio.jar jai_core.jar jai_codec-1.1.3.jar

下載鏈接:https://download.csdn.net/download/weixin_40816738/46766690

總結

以上是生活随笔為你收集整理的java 批量为图片添加图标水印和文字水印的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。