色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Java用自帶的Image IO給圖片添加水印

瀏覽:71日期:2022-08-10 14:39:35
目錄1. 文字水印2. 旋轉文字3. 旋轉坐標軸另外的寫法1. 文字水印

import sun.font.FontDesignMetrics;import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;/** * @Author ChengJianSheng * @Date 2021/6/10 */public class WatermarkUtil{ public static void main(String[] args) throws IOException{ addText('F:/1.jpeg', '我的夢想是成為火影');}/** * 加文字水印 * @param srcPath 原文件路徑 * @param content 文字內容 * @throws IOException */ public static void addText(String srcPath, String content) throws IOException {// 讀取原圖片信息BufferedImage srcImage = ImageIO.read(new File(srcPath));int width = srcImage.getWidth();int height = srcImage.getHeight();// 創建畫筆,設置繪圖區域BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics2D g = bufferedImage.createGraphics();g.drawImage(srcImage, 0, 0, width, height, null);//g.drawImage(srcImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);g.setFont(new Font('宋體', Font.PLAIN, 32));g.setColor(Color.RED);// 計算文字長度int len = g.getFontMetrics(g.getFont()).charsWidth(content.toCharArray(), 0, content.length());FontDesignMetrics metrics = FontDesignMetrics.getMetrics(g.getFont());int len2 = metrics.stringWidth(content);System.out.println(len);System.out.println(len2);// 計算文字坐標int x = width - len - 10;int y = height - 20;//int x = width - 2 * len;//int y = height - 1 * len;g.drawString(content, x, y);g.dispose();// 輸出文件FileOutputStream fos = new FileOutputStream('F:/2.png');ImageIO.write(bufferedImage, 'png', fos);fos.flush();fos.close(); }}

Java用自帶的Image IO給圖片添加水印

可以設置文字透明度

g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.8f)); 2. 旋轉文字

import sun.font.FontDesignMetrics;import javax.imageio.ImageIO;import java.awt.*;import java.awt.geom.AffineTransform;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;/** * @Author ChengJianSheng * @Date 2021/6/10 */public class WatermarkUtil{ // 水印透明度 private static final float alpha = 0.5 f; // 水印文字字體 private static final Font font = new Font('宋體', Font.BOLD, 30); // 水印文字顏色 private static final Color color = Color.RED; public static void main(String[] args) throws IOException{ addText('F:/1.jpeg', '圖片由木葉村提供,僅供忍者聯軍使用!');}/** * 加文字水印 * @param srcPath 原文件路徑 * @param content 文字內容 * @throws IOException */ public static void addText(String srcPath, String content) throws IOException {// 讀取原圖片信息BufferedImage srcImage = ImageIO.read(new File(srcPath));int width = srcImage.getWidth();int height = srcImage.getHeight();// 創建畫筆,設置繪圖區域BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics2D g = bufferedImage.createGraphics();g.drawImage(srcImage, 0, 0, width, height, null);//g.drawImage(srcImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);// 旋轉文字AffineTransform affineTransform = g.getTransform();affineTransform.rotate(Math.toRadians(-30), 0, 0);Font rotatedFont = font.deriveFont(affineTransform);g.setFont(rotatedFont); // 字體g.setColor(color); // 顏色g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 透明度// 計算文字長度int len = g.getFontMetrics(g.getFont()).charsWidth(content.toCharArray(), 0, content.length());FontDesignMetrics metrics = FontDesignMetrics.getMetrics(g.getFont());int len2 = metrics.stringWidth(content);System.out.println(len);System.out.println(len2);// 計算水印文字坐標int x = width / 5;int y = height / 3 * 2;g.drawString(content, x, y);g.dispose();// 輸出文件FileOutputStream fos = new FileOutputStream('F:/2.png');ImageIO.write(bufferedImage, 'png', fos);fos.flush();fos.close(); }}

Java用自帶的Image IO給圖片添加水印

畫矩形框

import sun.font.FontDesignMetrics;import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;/** * @Author ChengJianSheng * @Date 2021/6/10 */public class WatermarkUtil{ // 水印透明度 private static final float alpha = 0.5 f; // 水印文字字體 private static final Font font = new Font('宋體', Font.BOLD, 30); // 水印文字顏色 private static final Color color = Color.RED; public static void main(String[] args) throws IOException{ addText('F:/1.jpeg', '圖片由木葉村提供,僅供忍者聯軍使用!');}/** * 加文字水印 * @param srcPath 原文件路徑 * @param content 文字內容 * @throws IOException */ public static void addText(String srcPath, String content) throws IOException {// 讀取原圖片信息BufferedImage srcImage = ImageIO.read(new File(srcPath));int width = srcImage.getWidth();int height = srcImage.getHeight();// 創建畫筆,設置繪圖區域BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics2D g = bufferedImage.createGraphics();g.drawImage(srcImage, 0, 0, width, height, null);g.setFont(font); // 字體g.setColor(color); // 顏色g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 透明度// 計算文字寬高度FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);int textWidth = metrics.stringWidth(content); // 文字寬度int textHeight = metrics.getHeight(); // 文字高度// 計算文字坐標int x = (width - textWidth) / 2;int y = (height + textHeight) / 2;// 寫文字g.drawString(content, x, y);// 畫矩形int padding = 10; // 內邊距g.drawRect(x - padding / 2, y - textHeight, textWidth + padding, textHeight + padding);g.dispose();// 輸出文件FileOutputStream fos = new FileOutputStream('F:/2.png');ImageIO.write(bufferedImage, 'png', fos);fos.flush();fos.close(); }}

Java用自帶的Image IO給圖片添加水印

3. 旋轉坐標軸

import sun.font.FontDesignMetrics;import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;/** * @Author ChengJianSheng * @Date 2021/6/10 */public class WatermarkUtil{ // 水印透明度 private static final float alpha = 0.5 f; // 水印文字字體 private static final Font font = new Font('宋體', Font.BOLD, 30); // 水印文字顏色 private static final Color color = Color.RED; public static void main(String[] args) throws IOException{ addText('F:/1.jpeg', '圖片由木葉村提供,僅供忍者聯軍使用!');}/** * 加文字水印 * @param srcPath 原文件路徑 * @param content 文字內容 * @throws IOException */ public static void addText(String srcPath, String content) throws IOException {// 讀取原圖片信息BufferedImage srcImage = ImageIO.read(new File(srcPath));int width = srcImage.getWidth();int height = srcImage.getHeight();// 創建畫筆,設置繪圖區域BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics2D g = bufferedImage.createGraphics();g.drawImage(srcImage, 0, 0, width, height, null);g.setFont(font); // 字體g.setColor(color); // 顏色g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 透明度// 計算文字寬高度FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);int textWidth = metrics.stringWidth(content); // 文字寬度int textHeight = metrics.getHeight(); // 文字高度// 旋轉坐標軸g.translate(-width / 5, height / 4);g.rotate(-30 * Math.PI / 180);// 計算文字坐標int x = (width - textWidth) / 2;int y = (height + textHeight) / 2;// 寫文字g.drawString(content, x, y);// 畫矩形int padding = 10; // 內邊距g.drawRect(x - padding / 2, y - textHeight, textWidth + padding, textHeight + padding);g.dispose();// 輸出文件FileOutputStream fos = new FileOutputStream('F:/2.png');ImageIO.write(bufferedImage, 'png', fos);fos.flush();fos.close(); }}

Java用自帶的Image IO給圖片添加水印

結合下載功能,完整的代碼如下:

/** * 下載 */@GetMapping('/download')public void download(@RequestParam('mediaId') String mediaId, HttpServletRequest request, HttpServletResponse response) throws IOException{ SysFile sysFile = sysFileService.getByMediaId(mediaId); if(null == sysFile) {throw new IllegalArgumentException('文件不存在'); } String mimeType = request.getServletContext().getMimeType(sysFile.getPath()); System.out.println(mimeType); response.setContentType(sysFile.getContentType()); response.setHeader(HttpHeaders.CONTENT_DISPOSITION, 'attachment;filename=' + URLEncoder.encode(sysFile.getOriginalFilename(), 'UTF-8')); //FileInputStream fis = new FileInputStream(sysFile.getPath()); ServletOutputStream sos = response.getOutputStream(); WatermarkUtil.addText(sysFile.getPath(), '哈哈哈哈', sos); //IOUtils.copy(fis, sos); //IOUtils.closeQuietly(fis); IOUtils.closeQuietly(sos);}

import sun.font.FontDesignMetrics;import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.io.OutputStream;/** * @Author ChengJianSheng * @Date 2021/6/10 */public class WatermarkUtil{ // 水印透明度 private static final float alpha = 0.5 f; // 水印文字字體 private static final Font font = new Font('宋體', Font.BOLD, 30); // 水印文字顏色 private static final Color color = Color.RED; /** * 加文字水印 * @param srcPath 原文件路徑 * @param content 文字內容 * @throws IOException */ public static void addText(String srcPath, String content, OutputStream outputStream) throws IOException {// 讀取原圖片信息BufferedImage srcImage = ImageIO.read(new File(srcPath));int width = srcImage.getWidth();int height = srcImage.getHeight();// 畫板BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// 畫筆Graphics2D g = bufferedImage.createGraphics();g.drawImage(srcImage, 0, 0, width, height, null);g.setFont(font); // 字體g.setColor(color); // 顏色g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 透明度// 計算文字寬高度FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);int textWidth = metrics.stringWidth(content); // 文字寬度int textHeight = metrics.getHeight(); // 文字高度// 旋轉坐標軸g.translate(-width / 5, height / 4);g.rotate(-30 * Math.PI / 180);// 計算文字坐標int x = (width - textWidth) / 2;int y = (height + textHeight) / 2;// 寫文字g.drawString(content, x, y);// 畫矩形int padding = 10; // 內邊距g.drawRect(x - padding / 2, y - textHeight, textWidth + padding, textHeight + padding);g.dispose();// 輸出ImageIO.write(bufferedImage, 'png', outputStream); }}另外的寫法

import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;/** * @ProjectName: test * @Package: com.test.utils * @ClassName: MyTest * @Author: luqiming * @Description: * @Date: 2020/10/29 11:48 * @Version: 1.0 */public class AddWatermarkUtil { public static void waterPress(String srcImgPath, String outImgPath, Color markContentColor, int fontSize, String waterMarkContent) {try { String[] waterMarkContents = waterMarkContent.split('||'); // 讀取原圖片信息 File srcImgFile = new File(srcImgPath); Image srcImg = ImageIO.read(srcImgFile); int srcImgWidth = srcImg.getWidth(null); int srcImgHeight = srcImg.getHeight(null); // 加水印 BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB); // 得到畫筆對象 Graphics2D g = bufImg.createGraphics(); // 設置起點 g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null); Font font = new Font('宋體', Font.PLAIN, fontSize); // 根據圖片的背景設置水印顏色 g.setColor(markContentColor); // 設置水印文字字體 g.setFont(font); // 數組長度 int contentLength = waterMarkContents.length; // 獲取水印文字中最長的 int maxLength = 0; for (int i = 0; i < contentLength; i++) {int fontlen = getWatermarkLength(waterMarkContents[i], g);if (maxLength < fontlen) { maxLength = fontlen;} } for (int j = 0; j < contentLength; j++) {waterMarkContent = waterMarkContents[j];int tempX = 10;int tempY = fontSize;// 單字符長度int tempCharLen = 0;// 單行字符總長度臨時計算int tempLineLen = 0;StringBuffer sb = new StringBuffer();for (int i = 0; i < waterMarkContent.length(); i++) { char tempChar = waterMarkContent.charAt(i); tempCharLen = getCharLen(tempChar, g); tempLineLen += tempCharLen; if (tempLineLen >= srcImgWidth) {// 長度已經滿一行,進行文字疊加g.drawString(sb.toString(), tempX, tempY);// 清空內容,重新追加sb.delete(0, sb.length());tempLineLen = 0; } // 追加字符 sb.append(tempChar);}// 通過設置后兩個輸入參數給水印定位g.drawString(sb.toString(), 20, srcImgHeight - (contentLength - j - 1) * tempY-50); } g.dispose(); // 輸出圖片 FileOutputStream outImgStream = new FileOutputStream(outImgPath); ImageIO.write(bufImg, 'jpg', outImgStream); outImgStream.flush(); outImgStream.close();} catch (Exception e) { e.printStackTrace();} } public static int getCharLen(char c, Graphics2D g) {return g.getFontMetrics(g.getFont()).charWidth(c); } /** * 獲取水印文字總長度 * * @paramwaterMarkContent水印的文字 * @paramg * @return水印文字總長度 */ public static int getWatermarkLength(String waterMarkContent, Graphics2D g) {return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length()); } public static void main(String[] args) {// 原圖位置, 輸出圖片位置, 水印文字顏色, 水印文字String font = '張天愛||就很完美||2020-05-27 17:00:00';String inputAddress = 'F:/UpupooWallpaper/1.jpg';String outputAddress = 'F:/UpupooWallpaper/1.jpg';Color color = Color.GREEN;waterPress(inputAddress, outputAddress, color, 50, font); }}

添加效果

Java用自帶的Image IO給圖片添加水印

關于水印位置,需要修改:

//左下角 g.drawString(sb.toString(), 0, srcImgHeight - (contentLength - j - 1) * tempY);//右下角 g.drawString(sb.toString(), srcImgWidth - maxLength, srcImgHeight - (contentLength - j - 1) * tempY);

以上就是Java用自帶的Image IO給圖片添加水印的詳細內容,更多關于Java 圖片添加水印的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
主站蜘蛛池模板: 毛片1级| 中文字幕三区 | 国产高清在线精品一区二区 | 亚洲国产精品成 | 国产成人精品高清在线 | 欧美成人免费夜夜黄啪啪 | 麻豆国产96在线 | 中国 | 欧美成人影院在线观看三级 | 中国美女乱淫免费看视频 | 91b站| 中文字幕日韩精品中文区 | 欧美扣逼视频 | 日本暖暖视频在线播放 | hd最新国产人妖ts视频 | 在线视频一二三区2021不卡 | 亚欧成人毛片一区二区三区四区 | 国产高清a毛片在线看 | 男女午夜免费视频 | 成人免费观看网欧美片 | 成年人www | 天堂中文资源在线8 | 欧美大片国产在线永久播放 | 中文字幕无线码中文字幕网站 | 国产成人综合视频 | 欧美激情精品久久久久久久久久 | 欧美成人免费午夜影视 | 成人免费一区二区三区在线观看 | 全部aⅴ极品视觉盛宴精品 全部免费a级毛片 | 一级a毛片免费观看久久精品 | 欧美黑人巨大最猛性xxxxx | 国产呦精品一区二区三区网站 | 能直接看的一级欧美毛片 | 国产精品久久久久久吹潮 | 久久福利青草狠狠午夜 | 成年人在线观看网站 | 免费一级欧美片片线观看 | 国产成人免费在线视频 | 亚洲男人的天堂久久香蕉网 | 玖玖99视频 | 日韩精品免费看 | 精品玖玖玖视频在线观看 |