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 * @author Eric Xu
16 *
17 */
18 public final class ImageUtils {
19 /**
20 * 图片水印
21 * @param pressImg 水印图片
22 * @param targetImg 目标图片
23 * @param x 修正值 默认在中间
24 * @param y 修正值 默认在中间
25 * @param alpha 透明度
26 */
27 public final static void pressImage(String pressImg, String targetImg, int x, int y, float alpha) {
28 try {
29 File img = new File(targetImg);
30 Image src = ImageIO.read(img);
31 int wideth = src.getWidth(null);
32 int height = src.getHeight(null);
33 BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
34 Graphics2D g = image.createGraphics();
35 g.drawImage(src, 0, 0, wideth, height, null);
36 //水印文件
37 Image src_biao = ImageIO.read(new File(pressImg));
38 int wideth_biao = src_biao.getWidth(null);
39 int height_biao = src_biao.getHeight(null);
40 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
41 g.drawImage(src_biao, (wideth - wideth_biao) / 2, (height - height_biao) / 2, wideth_biao, height_biao, null);
42 //水印文件结束
43 g.dispose();
44 ImageIO.write((BufferedImage) image, "jpg", img);
45 } catch (Exception e) {
46 e.printStackTrace();
47 }
48 }
49
50 /**
51 * 文字水印
52 * @param pressText 水印文字
53 * @param targetImg 目标图片
54 * @param fontName 字体名称
55 * @param fontStyle 字体样式
56 * @param color 字体颜色
57 * @param fontSize 字体大小
58 * @param x 修正值
59 * @param y 修正值
60 * @param alpha 透明度
61 */
62 public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color, int fontSize, int x, int y, float alpha) {
63 try {
64 File img = new File(targetImg);
65 Image src = ImageIO.read(img);
66 int width = src.getWidth(null);
67 int height = src.getHeight(null);
68 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
69 Graphics2D g = image.createGraphics();
70 g.drawImage(src, 0, 0, width, height, null);
71 g.setColor(color);
72 g.setFont(new Font(fontName, fontStyle, fontSize));
73 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
74 g.drawString(pressText, (width - (getLength(pressText) * fontSize)) / 2 + x, (height - fontSize) / 2 + y);
75 g.dispose();
76 ImageIO.write((BufferedImage) image, "jpg", img);
77 } catch (Exception e) {
78 e.printStackTrace();
79 }
80 }
81
82 /**
83 * 缩放
84 * @param filePath 图片路径
85 * @param height 高度
86 * @param width 宽度
87 * @param bb 比例不对时是否需要补白
88 */
89 public static void resize(String filePath, int height, int width, boolean bb) {
90 try {
91 double ratio = 0.0; //缩放比例
92 File f = new File(filePath);
93 BufferedImage bi = ImageIO.read(f);
94 Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
95 //计算比例
96 if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
97 if (bi.getHeight() > bi.getWidth()) {
98 ratio = (new Integer(height)).doubleValue() / bi.getHeight();
99 } else {
100 ratio = (new Integer(width)).doubleValue() / bi.getWidth();
101 }
102 AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
103 itemp = op.filter(bi, null);
104 }
105 if (bb) {
106 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
107 Graphics2D g = image.createGraphics();
108 g.setColor(Color.white);
109 g.fillRect(0, 0, width, height);
110 if (width == itemp.getWidth(null))
111 g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
112 else
113 g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
114 g.dispose();
115 itemp = image;
116 }
117 ImageIO.write((BufferedImage) itemp, "jpg", f);
118 } catch (IOException e) {
119 e.printStackTrace();
120 }
121 }
122
123 public static void main(String[] args) throws IOException {
124 pressImage("G:\\imgtest\\sy.jpg", "G:\\imgtest\\test1.jpg", 0, 0, 0.5f);
125 pressText("我是文字水印", "G:\\imgtest\\test1.jpg", "黑体", 36, Color.white, 80, 0, 0, 0.3f);
126 resize("G:\\imgtest\\test1.jpg", 500, 500, true);
127 }
128
129 public static int getLength(String text) {
130 int length = 0;
131 for (int i = 0; i < text.length(); i++) {
132 if (new String(text.charAt(i) + "").getBytes().length > 1) {
133 length += 2;
134 } else {
135 length += 1;
136 }
137 }
138 return length / 2;
139 }
140 }
141
ExtJS教程-
Hibernate教程-
Struts2 教程-
Lucene教程