關(guān)鍵字: 企業(yè)應(yīng)用
今天客戶說,他想把他上傳的圖片加上個水印的功能,以防止別人盜用他的圖片。他認(rèn)為他的圖片資料很重要。所以。。。好,客戶有需求,我們就滿足他,以前我也比較少寫操作圖片的api,所以對圖片加水印的功能也一直沒接觸,不過對于現(xiàn)在網(wǎng)絡(luò)來說。這些根本就不算什么,上網(wǎng)一搜,就找了幾個程序出來,現(xiàn)在我重構(gòu)了下,使它滿足我的要求,現(xiàn)在發(fā)布出來,希望可以給有需要的朋友一點(diǎn)幫助。
1 package com.teesoo.util;
2
3 /**
4 * <b>類名:sdf.java</b> </br> 編寫日期: 2007-5-16 <br/>
5 * 程序功能描述: 對圖片進(jìn)行打水印工作,
6 * 包括圖片水印,文字水印等。
7 * <br/> Demo: <br/>
8 * Bug: <br/>
9 *
10 * 程序變更日期 :<br/> 變更作者 :<br/> 變更說明 :<br/>
11 *
12 * @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>
13 */
14
15
16 import java.awt.*;
17 import java.awt.event.*;
18 import java.io.*;
19 import java.awt.image.*;
20
21 import org.w3c.dom.*;
22 import com.sun.image.codec.jpeg.*;
23
24 import javax.imageio.*;
25
26 public final class ImageUtils {
27 public ImageUtils() {
28
29 }
30
31 public final static String getPressImgPath(){
32 return ApplicationContext.getRealPath("/template/data/util/shuiyin.png");
33 }
34
35 /**
36 * 把圖片印刷到圖片上
37 * @param pressImg -- 水印文件
38 * @param targetImg -- 目標(biāo)文件
39 * @param x
40 * @param y
41 */
42 public final static void pressImage(String pressImg, String targetImg, int x, int y) {
43 System.setProperty("java.awt.headless", "true");
44 try {
45 File _file = new File(targetImg);
46 Image src = ImageIO.read(_file);
47 int wideth = src.getWidth(null);
48 int height = src.getHeight(null);
49 BufferedImage image = new BufferedImage(wideth, height,
50 BufferedImage.TYPE_INT_RGB);
51 Graphics g = image.createGraphics();
52 g.drawImage(src, 0, 0, wideth, height, null);
53
54 // 水印文件
55 File _filebiao = new File(pressImg);
56 Image src_biao = ImageIO.read(_filebiao);
57 int wideth_biao = src_biao.getWidth(null);
58 int height_biao = src_biao.getHeight(null);
59 g.drawImage(src_biao, wideth - wideth_biao - x, height - height_biao -y, wideth_biao,
60 height_biao, null);
61 // /
62 g.dispose();
63 FileOutputStream out = new FileOutputStream(targetImg);
64 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
65 encoder.encode(image);
66 out.close();
67 } catch (Exception e) {
68 e.printStackTrace();
69 }
70 }
71
72 /**
73 * 打印文字水印圖片
74 * @param pressText --文字
75 * @param targetImg -- 目標(biāo)圖片
76 * @param fontName -- 字體名
77 * @param fontStyle -- 字體樣式
78 * @param color -- 字體顏色
79 * @param fontSize -- 字體大小
80 * @param x -- 偏移量
81 * @param y
82 */
83
84 public static void pressText(String pressText, String targetImg, String fontName,int fontStyle, int color, int fontSize, int x, int y) {
85 System.setProperty("java.awt.headless", "true");
86 try {
87 File _file = new File(targetImg);
88 Image src = ImageIO.read(_file);
89 int wideth = src.getWidth(null);
90 int height = src.getHeight(null);
91 BufferedImage image = new BufferedImage(wideth, height,
92 BufferedImage.TYPE_INT_RGB);
93 Graphics g = image.createGraphics();
94 g.drawImage(src, 0, 0, wideth, height, null);
95 // String s="www.qhd.com.cn";
96 g.setColor(Color.RED);
97 g.setFont(new Font(fontName, fontStyle, fontSize));
98
99
100 g.drawString(pressText, wideth - fontSize - x, height - fontSize/2 - y);
101 g.dispose();
102 FileOutputStream out = new FileOutputStream(targetImg);
103 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
104 encoder.encode(image);
105 out.close();
106 } catch (Exception e) {
107 System.out.println(e);
108 }
109 }
110
111 public static void main(String[] args) {
112 pressImage("C:/shuiyin/shuiyin.png", "c:/shuiyin/DSC02342.JPG", 300 ,300);
113 }
114 }
2
3 /**
4 * <b>類名:sdf.java</b> </br> 編寫日期: 2007-5-16 <br/>
5 * 程序功能描述: 對圖片進(jìn)行打水印工作,
6 * 包括圖片水印,文字水印等。
7 * <br/> Demo: <br/>
8 * Bug: <br/>
9 *
10 * 程序變更日期 :<br/> 變更作者 :<br/> 變更說明 :<br/>
11 *
12 * @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>
13 */
14
15
16 import java.awt.*;
17 import java.awt.event.*;
18 import java.io.*;
19 import java.awt.image.*;
20
21 import org.w3c.dom.*;
22 import com.sun.image.codec.jpeg.*;
23
24 import javax.imageio.*;
25
26 public final class ImageUtils {
27 public ImageUtils() {
28
29 }
30
31 public final static String getPressImgPath(){
32 return ApplicationContext.getRealPath("/template/data/util/shuiyin.png");
33 }
34
35 /**
36 * 把圖片印刷到圖片上
37 * @param pressImg -- 水印文件
38 * @param targetImg -- 目標(biāo)文件
39 * @param x
40 * @param y
41 */
42 public final static void pressImage(String pressImg, String targetImg, int x, int y) {
43 System.setProperty("java.awt.headless", "true");
44 try {
45 File _file = new File(targetImg);
46 Image src = ImageIO.read(_file);
47 int wideth = src.getWidth(null);
48 int height = src.getHeight(null);
49 BufferedImage image = new BufferedImage(wideth, height,
50 BufferedImage.TYPE_INT_RGB);
51 Graphics g = image.createGraphics();
52 g.drawImage(src, 0, 0, wideth, height, null);
53
54 // 水印文件
55 File _filebiao = new File(pressImg);
56 Image src_biao = ImageIO.read(_filebiao);
57 int wideth_biao = src_biao.getWidth(null);
58 int height_biao = src_biao.getHeight(null);
59 g.drawImage(src_biao, wideth - wideth_biao - x, height - height_biao -y, wideth_biao,
60 height_biao, null);
61 // /
62 g.dispose();
63 FileOutputStream out = new FileOutputStream(targetImg);
64 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
65 encoder.encode(image);
66 out.close();
67 } catch (Exception e) {
68 e.printStackTrace();
69 }
70 }
71
72 /**
73 * 打印文字水印圖片
74 * @param pressText --文字
75 * @param targetImg -- 目標(biāo)圖片
76 * @param fontName -- 字體名
77 * @param fontStyle -- 字體樣式
78 * @param color -- 字體顏色
79 * @param fontSize -- 字體大小
80 * @param x -- 偏移量
81 * @param y
82 */
83
84 public static void pressText(String pressText, String targetImg, String fontName,int fontStyle, int color, int fontSize, int x, int y) {
85 System.setProperty("java.awt.headless", "true");
86 try {
87 File _file = new File(targetImg);
88 Image src = ImageIO.read(_file);
89 int wideth = src.getWidth(null);
90 int height = src.getHeight(null);
91 BufferedImage image = new BufferedImage(wideth, height,
92 BufferedImage.TYPE_INT_RGB);
93 Graphics g = image.createGraphics();
94 g.drawImage(src, 0, 0, wideth, height, null);
95 // String s="www.qhd.com.cn";
96 g.setColor(Color.RED);
97 g.setFont(new Font(fontName, fontStyle, fontSize));
98
99
100 g.drawString(pressText, wideth - fontSize - x, height - fontSize/2 - y);
101 g.dispose();
102 FileOutputStream out = new FileOutputStream(targetImg);
103 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
104 encoder.encode(image);
105 out.close();
106 } catch (Exception e) {
107 System.out.println(e);
108 }
109 }
110
111 public static void main(String[] args) {
112 pressImage("C:/shuiyin/shuiyin.png", "c:/shuiyin/DSC02342.JPG", 300 ,300);
113 }
114 }