上传图片如何个图片加水印??? jsp的系统要加一个上传图片的功能...如何才能上传的同时加一个水印? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在你点击上传提交的时候,调用加水印的方法即可:package commons;/** * * <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("添加水印失败!"); } } //g.setFont(new Font(Content, fontStyle, fontSize));这句什么意思啊??? 楼主很强,但这个 AttributedCharacterIterator iter = ats.getIterator();接口是做什么用的呀?好像没用到呀/ 如何手动关闭ibatis的数据库连接? 表单验证问题??? 哪位大侠能告诉我 slideshow2 插件的调用方法??在线等…… 求教Hibernate:如何将bean中的一个成员变量作为条件集合 struts2.1中的Socket问题 关于刷新框架的问 关于jsp文件上传的,我作了些总结,代码已调试过,绝对正确,供菜鸟使用 新手jsp 页面查询问题 一些关于现在多用户系统的问题 求助下,网页端js保存的session,java后台可以读取出来吗? Java程序连接mysql时出错是为什么? jsp第一次连接mysql的菜鸟问题,请大家帮帮忙。
*
* <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("添加水印失败!");
}
}
这句什么意思啊???