1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.GradientPaint; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.Shape; import java.awt.font.GlyphVector; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException;
import javax.imageio.ImageIO;
import org.slf4j.Logger; import org.slf4j.LoggerFactory;
public class WatermarkHelper {
private static Logger logger = LoggerFactory .getLogger(WatermarkHelper.class);
/** * 斜水印,重复水印,文字,并且生成文件 * @param targetImg 目标图片 * @param newImg 生成的文件路径 * @param pressText 文字 * @param fontName 字体名称 * @param colorStr 字体颜色字符串,格式如:#29944f * @param fontSize 字体大小 * @param alpha 透明度(0.1-0.9) * @param carelessness true为字体实心,false为字体空心 * @param colSpacing 行间距(1-10) * @return */ public static boolean pressText(String targetImg, String newImg,String pressText, String fontName, String colorStr, int fontSize, float alpha, boolean carelessness, int colSpacing) { boolean isok = false; try { BufferedImage image = pressText(targetImg, pressText, fontName, colorStr, fontSize, alpha, carelessness, colSpacing); File fileout = new File(newImg); ImageIO.write(image, "JPEG", fileout); isok = true; }catch(Exception ex) { logger.error(ex.getMessage(),ex); } return isok; } /** * 斜水印,重复水印,文字 * @param targetImg 目标图片 * @param pressText 文字 * @param fontName 字体名称 * @param colorStr 字体颜色字符串,格式如:#29944f * @param fontSize 字体大小 * @param alpha 透明度(0.1-0.9) * @param carelessness true为字体实心,false为字体空心 * @param colSpacing 行间距(1-10) * @return * @throws IOException */ public static BufferedImage pressText(String targetImg,String pressText, String fontName, String colorStr, int fontSize, float alpha, boolean carelessness, int colSpacing) throws IOException {
BufferedImage image = null; Graphics2D g2d = null; try {
File file = new File(targetImg);
Image src = ImageIO.read(file);
//图片宽度 int width = src.getWidth(null); //图片高度 int height = src.getHeight(null);
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
g2d = image.createGraphics(); //绘原图 g2d.drawImage(src, 0, 0, width, height, null); //比例 g2d.scale(1, 1);
g2d.addRenderingHints(new RenderingHints( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
//颜色 Color color = Color.decode(colorStr);
//字体 Font font = new Font(fontName, Font.PLAIN, fontSize);
GlyphVector fontGV = font.createGlyphVector(g2d .getFontRenderContext(), pressText); Rectangle size = fontGV.getPixelBounds(g2d.getFontRenderContext(), 0, 0); Shape textShape = fontGV.getOutline(); double textWidth = size.getWidth(); double textHeight = size.getHeight(); AffineTransform rotate45 = AffineTransform .getRotateInstance(Math.PI / 4d); Shape rotatedText = rotate45.createTransformedShape(textShape); // use a gradient that repeats 4 times g2d.setPaint(new GradientPaint(0, 0, color, image.getWidth() / 2, image.getHeight() / 2, color));
//透明度 g2d.setStroke(new BasicStroke(alpha));
// step in y direction is calc'ed using pythagoras + 5 pixel padding double yStep = Math.sqrt(textWidth * textWidth / 2) + 10; // step over image rendering watermark text
for (double x = -textHeight * colSpacing; x < image.getWidth(); x += (textHeight * colSpacing)) { double y = -yStep; for (; y < image.getHeight(); y += yStep) { g2d.draw(rotatedText); if (carelessness)//字体实心 { g2d.fill(rotatedText); } g2d.translate(0, yStep); } g2d.translate(textHeight * colSpacing, -(y + yStep)); } } finally { if (g2d != null) { g2d.dispose(); } } return image; } }
|