package com.zhuozhengsoft.mark;
import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.font.TextAttribute; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.text.AttributedCharacterIterator; import java.text.AttributedString;
import javax.imageio.ImageIO; /** * 生成水印圖片,可設(shè)置水印文本的Font,透明度,傾斜度,圖片的高度,寬度等 * * @author Dong * @date 2017.3.3 */ public class MarkImage { // 水印的字體,默認(rèn)(“宋體”, Font.BOLD, 30); PRivate Font font = new Font(“宋體”, Font.BOLD, 30); // 水印的顏色,默認(rèn)紅色 private Color color = Color.red; // 水印的透明度,默認(rèn)0.5共有三個(gè)值可選(0,0.5,1透明度依次增強(qiáng)) private float alpha = 0.5f; // 水印的傾斜度,默認(rèn)逆時(shí)針旋轉(zhuǎn)45度 private Integer degree = -45; // 水印文字之間的間距 private Integer distance = 100; // 水印的文本內(nèi)容,默認(rèn)”加密文檔” private String markText = “加密文檔”;
public void setFont(Font font) { this.font = font;}public void setColor(Color color) { this.color = color;}public void setAlpha(float alpha) { this.alpha = alpha;}public void setDegree(Integer degree) { this.degree = degree;}public void setMarkText(String markText) { this.markText = markText;}public void setDistance(Integer distance) { this.distance = distance;}/** * * @param text:根據(jù)水印文本獲取文本的長(zhǎng)度 * @return */private int getTextLength(String text) { int length = text.length();// 獲取所有文本的長(zhǎng)度 for (int i = 0; i < text.length(); i++) { String s = String.valueOf(text.charAt(i));// 獲取指定的字符 if (s.getBytes().length > 1) {// 字節(jié)長(zhǎng)度大于1,說明是中文,那么需要延長(zhǎng)文本長(zhǎng)度 length++; } } // 計(jì)算總共有多少個(gè)字節(jié),也就是有多少個(gè)字 length = (length % 2 == 0) ? length / 2 : length / 2 + 1; return length;}/** * * @param targetPath:生成的目標(biāo)圖片的路徑 * @param width:圖片的寬度 * @param height:圖片的高度 */public void createMarkImage(String targetPath,int width,int height) { Image srcImg = null; OutputStream outputStream = null; try { // 1.創(chuàng)建源空白圖片 BufferedImage buffImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR); // 2、得到畫筆對(duì)象 Graphics2D graph = buffImg.createGraphics(); graph.setClip(0, 0, width, height); graph.setColor(Color.white); graph.fillRect(0, 0, width, height);// 先用黑色填充整張圖片,也就是背景 ImageIO.write(buffImg, "JPG", new File(targetPath));// 輸出png圖片 System.out.println("空白圖片已經(jīng)生成"); //3.讀取生成的空白圖片 srcImg = ImageIO.read(new File(targetPath)); // 4、設(shè)置對(duì)線段的鋸齒狀邊緣處理 graph.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graph.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null); // 5、設(shè)置水印旋轉(zhuǎn)度 graph.rotate(Math.toRadians(this.degree), (double) buffImg .getWidth() / 2, (double) buffImg.getHeight() / 2); // 6、設(shè)置水印文字顏色 graph.setColor(this.color); // 7、設(shè)置水印文字Font AttributedString ats = new AttributedString(this.markText); ats.addAttribute(TextAttribute.FONT, this.font, 0, this.markText .length()); AttributedCharacterIterator iter = ats.getIterator(); graph.setFont(this.font); // 8、設(shè)置水印文字透明度 graph.setComposite(AlphaComposite.getInstance( AlphaComposite.SRC_ATOP, this.alpha)); // 9.循環(huán)添加水印 int textW = this.font.getSize() * getTextLength(this.markText); int textH = this.font.getSize(); int x = -srcImg.getWidth(null) / 2; int y = -srcImg.getHeight(null) / 2; while (x < srcImg.getWidth(null) * 1.5) { y = -srcImg.getHeight(null) / 2; while (y < srcImg.getHeight(null) * 1.5) { // 添加水印效果 graph.drawString(iter, x, y);// y保證至少可以顯示一個(gè)水印的高度 y += textH + this.distance;// distance為間隔值,即每個(gè)水印之間的間隔 } x += textW + this.distance; } // 10.釋放資源 graph.dispose(); // 11、生成水印圖片 outputStream = new FileOutputStream(targetPath); ImageIO.write(buffImg, "JPG", outputStream); System.out.println("圖片完成添加水印文字"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (null != srcImg) srcImg.flush(); } catch (Exception e) { e.printStackTrace(); } try { if (null != outputStream) outputStream.close(); } catch (Exception e) { e.printStackTrace(); } }}}
/** * 測(cè)試生成水印圖片 */public static void main(String[] args) {MarkImage markImage=new MarkImage();markImage.setMarkText("今天天氣不錯(cuò)!");markImage.setFont(new Font("微軟雅黑",Font.BOLD,35));markImage.setColor(Color.pink);markImage.setAlpha(1.0f);markImage.createMarkImage("D://mark.jpg", 700,1400);}-新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注