图片添加水印

图片添加水印
1 /*
2 * TODO 实现图片的水印效果,只针对jpg
3 */
4  package com.eshu;
5 import java.awt.*;
6 import java.awt.image.*;
7 import java.io.*;
8 import javax.swing.*;
9
10 import com.sun.image.codec.jpeg.*;
11
12 public class WaterMark {
13 /**
14 * 给图片添加文字水印
15 * @param filePath 需要添加水印的图片的路径
16 * @param markContent 水印的文字
17 * @param markContentColor 水印文字的颜色
18 * @param qualNum 图片质量
19 * @return
20 */
21 public static boolean pressText(String filePath, String markContent,
22 Color markContentColor, float qualNum) {
23 int idx = filePath.lastIndexOf(".");
24 if (idx != -1) {
25 if (filePath.substring(idx + 1).equals("jpg")) {
26 ImageIcon imgIcon = new ImageIcon(filePath);
27 Image theImg = imgIcon.getImage(); // 根据路径获取图片
28 int width = theImg.getWidth(null); // 图片的宽
29 int height = theImg.getHeight(null); // 图片的高
30 BufferedImage bimage = new BufferedImage(width, height,
31 BufferedImage.TYPE_INT_RGB);
32 Graphics2D g = bimage.createGraphics(); // 创建一个
33 // Graphics2D,可以将它绘制到此BufferedImage
34 //
35 g.setColor(markContentColor); // 水印字体颜色
36 g.drawImage(theImg, 0, 0, null); // 呈现一个图像,在绘制前进行从图像空间到用户空间的转换
37 if (width < 150) {
38 g.setFont(new Font("黑体", 0, 10)); // 设置字体样式,大小
39 g.drawString(markContent, width / 2, height - 5);
40 } else {
41 g.setFont(new Font("楷体", 0, 30));
42 g.drawString(markContent, width - 180, height - 20);
43 }
44 g.dispose();
45 try {
46 FileOutputStream out = new FileOutputStream(filePath); // 创建一个文件输出流,向指定的
47 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
48 JPEGEncodeParam param = encoder
49 .getDefaultJPEGEncodeParam(bimage);
50 param.setQuality(qualNum, true);
51 encoder.encode(bimage, param);
52 out.close();
53 } catch (Exception e) {
54 return false;
55 }
56 }
57 }
58 return true;
59 }
60 /**
61 * 添加图片水印
62 * @param pressImg --水印文件
63 * @param targetImg --目标文件
64 */
65 public static boolean pressImage(String pressImg, String targetImg) {
66 int idx = targetImg.lastIndexOf(".");
67 if (idx != -1) {
68 if (targetImg.substring(idx + 1).equals("jpg")) {
69 Image src = new ImageIcon(targetImg).getImage();// 目标图片文件
70 int wideth = src.getWidth(null); // 图片宽
71 int height = src.getHeight(null); // 图片高
72 BufferedImage image = new BufferedImage(wideth, height,
73 BufferedImage.TYPE_INT_RGB);
74 Graphics g = image.createGraphics(); // 绘图
75 g.drawImage(src, 0, 0, wideth, height, null);
76 Image src_biao = new ImageIcon(pressImg).getImage();// 水印文件
77 int wideth_biao = src_biao.getWidth(null); // 水印的宽
78 int height_biao = src_biao.getHeight(null); // 水印的高
79 g.drawImage(src_biao, (wideth - wideth_biao) / 2,
80 (height - height_biao) / 2, wideth_biao,
81 height_biao, null);// 可以调整水印的位置
82 g.dispose();
83 try {
84 FileOutputStream out = new FileOutputStream(targetImg);
85 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
86 encoder.encode(image);
87 out.close();
88 } catch (Exception e) {
89 e.printStackTrace();
90 }
91 }
92 }
93 return true;
94 }
95 }

 

posted @ 2009-12-30 15:26  TQ.CH  阅读(294)  评论(0编辑  收藏  举报