日期:2014-05-17 浏览次数:21240 次
package commons;
/**
*
* <p>Title: 给图片添加水印</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2007</p>
* <p>Company: 福瑞网络有限公司</p>
* <p>dd4ll119@gmail.com</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 markContent 水印的文字
* @param markContentColor 水印文字的颜色
* @param qualNum 图片质量
* @return
*/
public boolean createMark(String filePath, String sy_pic_path,
String markContent, Color markContentColor,
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(markContent);
ats.addAttribute(TextAttribute.FONT, f, 0, markContent.length());
AttributedCharacterIterator iter = ats.getIterator();
Graphics2D g = bimage.createGraphics();
g.setColor(markContentColor);
g.setBackground(Color.white);
//g.setFont(new Font(markContent, 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("添加水印失败!");
}
}
------解决方案--------------------
学习LS的了
------解决方案--------------------
http://topic.csdn.net/u/20100423/16/32d92b52-a59b-4dee-9d43-77d1291d3cdf.html
传送门!!!!
------解决方案--------------------
地下室学习
------解决方案--------------------
每天回帖即可获得10分可用分!
------解决方案--------------------
二楼正解
------解决方案--------------------
mark!
------解决方案--------------------
需要什么样的JAR包啊?
------解决方案--------------------