实现后台添加图片后自动添加水印效果
时间:2009-03-11 来源:hkebao
明白其意思了,就是说比如一个图片中添加进来水印字进来比如写上哪个网址信息之类的!
这样的话其他网站如果想要转载的话就比较麻烦了!
1.网上找的一段java写的方法我测试过是正常的!
package com.tryitsoft.image;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;
public class WaterMark {
/**
* 给图片添加水印
* @param filePath 需要添加水印的图片的路径
* @param markContent 水印的文字
* @param markContentColor 水印文字的颜色
* @param qualNum 图片质量
* @return
* @test createMark("d://map.jpg","http://www.tryitsoft.com",Color.ORANGE,70f)
*/
public boolean createMark(String filePath,String markContent,Color markContentColor,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);
Graphics2D g=bimage.createGraphics();
g.setColor(markContentColor);
g.setBackground(Color.white);
g.drawImage(theImg, 0, 0, null );
g.drawString(markContent,width/5,height/5); //添加水印的文字和设置水印文字出现的内容
g.dispose();
try{
FileOutputStream out=new FileOutputStream(filePath);
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(qualNum, true);
encoder.encode(bimage, param);
out.close();
}catch(Exception e)
{ return false; }
return true;
}
}
调用的方法如下:
<%com.tryitsoft.image.WaterMark wm = new com.tryitsoft.image.WaterMark();
if (wm.createMark("d://map.jpg","http://www.tryitsoft.com",Color.ORANGE,70f))
{
out.print("添加水印成功!");
}
else
{
out.print("添加水印失败!");
}
%>
比如说:我现在要上传一个商品之后就可以直接添加水印处理之后再保存了!
2.用ASP来实现功能:需要组件,先测试一下如果正常再看看有没有不用组件的方法了!
3.用HTML或JS实现水印效果的请看链接
http://luuijie123.blog.163.com/blog/static/14165132007828675143/
ASP比较遗憾呀,不能实现添加水印效果的呀!
这样的话其他网站如果想要转载的话就比较麻烦了!
1.网上找的一段java写的方法我测试过是正常的!
package com.tryitsoft.image;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;
public class WaterMark {
/**
* 给图片添加水印
* @param filePath 需要添加水印的图片的路径
* @param markContent 水印的文字
* @param markContentColor 水印文字的颜色
* @param qualNum 图片质量
* @return
* @test createMark("d://map.jpg","http://www.tryitsoft.com",Color.ORANGE,70f)
*/
public boolean createMark(String filePath,String markContent,Color markContentColor,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);
Graphics2D g=bimage.createGraphics();
g.setColor(markContentColor);
g.setBackground(Color.white);
g.drawImage(theImg, 0, 0, null );
g.drawString(markContent,width/5,height/5); //添加水印的文字和设置水印文字出现的内容
g.dispose();
try{
FileOutputStream out=new FileOutputStream(filePath);
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(qualNum, true);
encoder.encode(bimage, param);
out.close();
}catch(Exception e)
{ return false; }
return true;
}
}
调用的方法如下:
<%com.tryitsoft.image.WaterMark wm = new com.tryitsoft.image.WaterMark();
if (wm.createMark("d://map.jpg","http://www.tryitsoft.com",Color.ORANGE,70f))
{
out.print("添加水印成功!");
}
else
{
out.print("添加水印失败!");
}
%>
比如说:我现在要上传一个商品之后就可以直接添加水印处理之后再保存了!
2.用ASP来实现功能:需要组件,先测试一下如果正常再看看有没有不用组件的方法了!
3.用HTML或JS实现水印效果的请看链接
http://luuijie123.blog.163.com/blog/static/14165132007828675143/
ASP比较遗憾呀,不能实现添加水印效果的呀!
相关阅读 更多 +