请高手给出详细代码,并且加上注释。
怎么使用,非常感谢了。
怎么使用,非常感谢了。
解决方案 »
- 关于htmlparser运用,求高手,在线等,加班的人伤不起啊!!!
- iframe 刷新无法取值!
- 请问servlet里面,response.getWriter()获得的PrintWriter用完后需要关闭吗?
- jsp页面无法正常显示图片和视频,显示视频为黑屏,并且只有声音,没有图像
- jsp调用的javabean放在哪里?
- 请教数据库的一个简单问题
- 关于页面刷新的问题!
- 下拉框与表格连动问题求助
- 在jsp中,求教在线人数统计?包括上线人数增加,怎么判断离线,人数如何减少?谢谢!
- 请教一个关于Rocketmq报错DESC: service not available now, maybe disk full
- 抓狂!tomcat项目发布后不能访问
- struts1中path问题(顶者有分)
*
* <p>Title: 给图片添加水印</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2007</p>
* <p>Company: 福瑞网络有限公司</p>
* <p>[email protected]</p>
* @author 杨轶
* @version 1.0
*/
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;
import java.text.AttributedCharacterIterator;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import java.util.regex.Pattern;public class PicShuiYin {
/**
* 给图片添加水印
* @param filePath 需要添加水印的图片的路径
* @param Content 水印的文字
* @param ContentColor 水印文字的颜色
* @param qualNum 图片质量
* @return
*/
public boolean createMark(String filePath, String sy_pic_path,
String Content, Color ContentColor,
String fontStyle, int fontSize, float qualNum) { //图片
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = imgIcon.getImage();
int width = theImg.getWidth(null);
int height = theImg.getHeight(null);
BufferedImage bimage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);// 水印图片
ImageIcon imgIcon1 = new ImageIcon(sy_pic_path);
Image theImg1 = imgIcon1.getImage();
Font f = new Font(fontStyle, Font.BOLD, fontSize);
AttributedString ats = new AttributedString(Content);
ats.addAttribute(TextAttribute.FONT, f, 0, Content.length());
AttributedCharacterIterator iter = ats.getIterator(); Graphics2D g = bimage.createGraphics();
g.setColor(ContentColor);
g.setBackground(Color.white);
//g.setFont(new Font(Content, fontStyle, fontSize));
g.drawImage(theImg, 0, 0, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1f)); //添加图片水印
g.drawImage(theImg1, width - 530, height -80, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
//g.drawString(iter, width - 120, height - 10); //添加水印的文字和设置水印文字出现的内容 和位置
g.dispose();
try {
FileOutputStream out = new FileOutputStream(filePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bimage);
out.close();
} catch (Exception e) {
return false;
}
return true;
} public static void main(String[] args) {
PicShuiYin wm = new PicShuiYin();
if (wm.createMark("f://11.jpg", "f://logo.png", "要加的文字", Color.white, "华文彩云",
20,100f)) {
System.out.println("添加水印成功!");
} else {
System.out.println("添加水印失败!");
}
}
String sy_pic_path 这个参数是什么意思?
还有是添加成功后 怎么显示添加水印后的图片
添加水印后的图片被保存到什么地方了?