今天需要用Java程序给图片加水印,于是在网上找到了一段代码,感觉很好,于是记录了下来,原来的网址给忘了:
1 import java.awt.AlphaComposite; 2 import java.awt.Color; 3 import java.awt.Font; 4 import java.awt.Graphics2D; 5 import java.awt.Image; 6 import java.awt.geom.AffineTransform; 7 import java.awt.image.AffineTransformOp; 8 import java.awt.image.BufferedImage; 9 import java.io.File; 10 import java.io.IOException; 11 12 import javax.imageio.ImageIO; 13 14 /** 15 * 图片工具类, 图片水印,文字水印,缩放,补白等 16 * 17 * @author Carl He 18 */ 19 public final class ImageUtils { 20 /** 图片格式:JPG */ 21 private static final String PICTRUE_FORMATE_JPG = "jpg"; 22 23 private ImageUtils() { 24 } 25 26 /** 27 * 添加图片水印 28 * 29 * @param targetImg 30 * 目标图片路径,如:C://myPictrue//1.jpg 31 * @param waterImg 32 * 水印图片路径,如:C://myPictrue//logo.png 33 * @param x 34 * 水印图片距离目标图片左侧的偏移量,如果x<0, 则在正中间 35 * @param y 36 * 水印图片距离目标图片上侧的偏移量,如果y<0, 则在正中间 37 * @param alpha 38 * 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明) 39 */ 40 public final static void pressImage(String targetImg, String waterImg, 41 int x, int y, float alpha) { 42 try { 43 File file = new File(targetImg); 44 Image image = ImageIO.read(file); 45 int width = image.getWidth(null); 46 int height = image.getHeight(null); 47 BufferedImage bufferedImage = new BufferedImage(width, height, 48 BufferedImage.TYPE_INT_RGB); 49 Graphics2D g = bufferedImage.createGraphics(); 50 g.drawImage(image, 0, 0, width, height, null); 51 52 Image waterImage = ImageIO.read(new File(waterImg)); // 水印文件 53 int width_1 = waterImage.getWidth(null); 54 int height_1 = waterImage.getHeight(null); 55 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 56 alpha)); 57 58 int widthDiff = width - width_1; 59 int heightDiff = height - height_1; 60 if (x < 0) { 61 x = widthDiff / 2; 62 } else if (x > widthDiff) { 63 x = widthDiff; 64 } 65 if (y < 0) { 66 y = heightDiff / 2; 67 } else if (y > heightDiff) { 68 y = heightDiff; 69 } 70 g.drawImage(waterImage, x, y, width_1, height_1, null); // 水印文件结束 71 g.dispose(); 72 ImageIO.write(bufferedImage, PICTRUE_FORMATE_JPG, file); 73 } catch (IOException e) { 74 e.printStackTrace(); 75 } 76 } 77 78 /** 79 * 添加文字水印 80 * 81 * @param targetImg 82 * 目标图片路径,如:C://myPictrue//1.jpg 83 * @param pressText 84 * 水印文字, 如:中国证券网 85 * @param fontName 86 * 字体名称, 如:宋体 87 * @param fontStyle 88 * 字体样式,如:粗体和斜体(Font.BOLD|Font.ITALIC) 89 * @param fontSize 90 * 字体大小,单位为像素 91 * @param color 92 * 字体颜色 93 * @param x 94 * 水印文字距离目标图片左侧的偏移量,如果x<0, 则在正中间 95 * @param y 96 * 水印文字距离目标图片上侧的偏移量,如果y<0, 则在正中间 97 * @param alpha 98 * 透明度(0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明) 99 */ 100 public static void pressText(String targetImg, String pressText, 101 String fontName, int fontStyle, int fontSize, Color color, int x, 102 int y, float alpha) { 103 try { 104 File file = new File(targetImg); 105 106 Image image = ImageIO.read(file); 107 int width = image.getWidth(null); 108 int height = image.getHeight(null); 109 BufferedImage bufferedImage = new BufferedImage(width, height, 110 BufferedImage.TYPE_INT_RGB); 111 Graphics2D g = bufferedImage.createGraphics(); 112 g.drawImage(image, 0, 0, width, height, null); 113 g.setFont(new Font(fontName, fontStyle, fontSize)); 114 g.setColor(color); 115 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 116 alpha)); 117 118 int width_1 = fontSize * getLength(pressText); 119 int height_1 = fontSize; 120 int widthDiff = width - width_1; 121 int heightDiff = height - height_1; 122 if (x < 0) { 123 x = widthDiff / 2; 124 } else if (x > widthDiff) { 125 x = widthDiff; 126 } 127 if (y < 0) { 128 y = heightDiff / 2; 129 } else if (y > heightDiff) { 130 y = heightDiff; 131 } 132 133 g.drawString(pressText, x, y + height_1); 134 g.dispose(); 135 ImageIO.write(bufferedImage, PICTRUE_FORMATE_JPG, file); 136 } catch (Exception e) { 137 e.printStackTrace(); 138 } 139 } 140 141 /** 142 * 获取字符长度,一个汉字作为 1 个字符, 一个英文字母作为 0.5 个字符 143 * 144 * @param text 145 * @return 字符长度,如:text="中国",返回 2;text="test",返回 2;text="中国ABC",返回 4. 146 */ 147 public static int getLength(String text) { 148 int textLength = text.length(); 149 int length = textLength; 150 for (int i = 0; i < textLength; i++) { 151 if (String.valueOf(text.charAt(i)).getBytes().length > 1) { 152 length++; 153 } 154 } 155 return (length % 2 == 0) ? length / 2 : length / 2 + 1; 156 } 157 158 /** 159 * 图片缩放 160 * 161 * @param filePath 162 * 图片路径 163 * @param height 164 * 高度 165 * @param width 166 * 宽度 167 * @param bb 168 * 比例不对时是否需要补白 169 */ 170 public static void resize(String filePath, int height, int width, boolean bb) { 171 try { 172 double ratio = 0; // 缩放比例 173 File f = new File(filePath); 174 BufferedImage bi = ImageIO.read(f); 175 Image itemp = bi.getScaledInstance(width, height, 176 BufferedImage.SCALE_SMOOTH); 177 // 计算比例 178 if ((bi.getHeight() > height) || (bi.getWidth() > width)) { 179 if (bi.getHeight() > bi.getWidth()) { 180 ratio = (new Integer(height)).doubleValue() 181 / bi.getHeight(); 182 } else { 183 ratio = (new Integer(width)).doubleValue() / bi.getWidth(); 184 } 185 AffineTransformOp op = new AffineTransformOp( 186 AffineTransform.getScaleInstance(ratio, ratio), null); 187 itemp = op.filter(bi, null); 188 } 189 if (bb) { 190 BufferedImage image = new BufferedImage(width, height, 191 BufferedImage.TYPE_INT_RGB); 192 Graphics2D g = image.createGraphics(); 193 g.setColor(Color.white); 194 g.fillRect(0, 0, width, height); 195 if (width == itemp.getWidth(null)) 196 g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, 197 itemp.getWidth(null), itemp.getHeight(null), 198 Color.white, null); 199 else 200 g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, 201 itemp.getWidth(null), itemp.getHeight(null), 202 Color.white, null); 203 g.dispose(); 204 itemp = image; 205 } 206 ImageIO.write((BufferedImage) itemp, "jpg", f); 207 } catch (IOException e) { 208 e.printStackTrace(); 209 } 210 } 211 212 public static void main(String[] args) throws IOException { 213 pressImage("C://pic//jpg", "C://pic//test.gif", 5000, 5000, 0f); 214 pressText("C://pic//jpg", "旺仔之印", "宋体", Font.BOLD | Font.ITALIC, 20, 215 Color.BLACK, 0, 0, 8f); 216 resize("C://pic//4.jpg", 1000, 500, true); 217 } 218 }