转收藏的代码:结合在jspsmart.upload下使用的,这个是imgbean的代码
package com.pic;import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.image.*;
import org.w3c.dom.*;
import com.sun.image.codec.jpeg.*;
import javax.imageio.*;public class ImgBean_i{
public void ImgBean_i(){}public void ImgYin(String Imagename_biao,String ImgName){
try{
File _file = new File(ImgName);
Image src = ImageIO.read(_file);
int wideth=src.getWidth(null);
int height=src.getHeight(null);
BufferedImage image=new BufferedImage(wideth,height,BufferedImage.TYPE_INT_RGB);
Graphics g=image.createGraphics();
g.drawImage(src,0,0,wideth,height,null);//水印文件
File _filebiao = new File(Imagename_biao);
Image src_biao = ImageIO.read(_filebiao);
int wideth_biao=src_biao.getWidth(null);
int height_biao=src_biao.getHeight(null);
g.drawImage(src_biao,wideth-110,height-110,wideth_biao,height_biao,null);
//水印文件在原图片文件的位置,原图片文件的右下角为wideth-0,height-0
g.dispose();
FileOutputStream out=new FileOutputStream(ImgName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
以下是修改过的smartupload代码
<%@ page language="java" import="com.jspsmart.upload.*" import="java.util.*,java.text.*"%>
<%@ page contentType="text/html;charset=GBK" %><jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload"/><HTML><META http-equiv=Content-Type content="text/html; charset=gb2312">
<LINK href="images/css.css" rel=stylesheet>
<BODY BGCOLOR="white" leftmargin="0" topmargin="0">
<%
int count=0;
String path=request.getRealPath("");
mySmartUpload.initialize(pageContext); mySmartUpload.upload(); connectToDatabase();
for (int i=0;i<mySmartUpload.getFiles().getCount();i++){ com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i); if (!myFile.isMissing()) {
int Size=myFile.getSize() ;
String FileName=myFile.getFileName();
String FileExt=myFile.getFileExt();
String uploadtime=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( java.util.Calendar.getInstance().getTime() );
if(Size>=1000000) {
out.println("文件超过1000K!"); //限制大小
}else{
if((FileExt.equals("jpg")) || (FileExt.equals("JPG")) || (FileExt.equals("gif")) || (FileExt.equals("GIF"))) {
java.util.Date date = new java.util.Date();
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMddHHmmss");
String name1=FileName.substring(0,FileName.indexOf("."));
String name2=FileName.substring(FileName.indexOf("."),FileName.length());
String newname=name1+formatter1.format(date)+name2;
//session.putValue("path1",newname);
myFile.saveAs("/photo/upfile/" + newname);
if(Integer.parseInt(sy)==1) {
com.pic.ImgBean_i bbb= new com.pic.ImgBean_i();
bbb.ImgYin(path+"/photo/images/shuiyin.gif",path+"/photo/upfile/" + newname);//这里是水印文件的来源地址,可修改成取得下拉菜单值的形式,这样就可以实现打文字水印或者图片水印了
}
stmt.executeUpdate("insert into img_photo (name,http,userid,classid,title,content,imgsize,format,num,primflag,posttime) values ()");
out.println(("<br/><br/><center>图片上传成功!<br/>").toString());
}else{
out.println("文件格式不对!");
}
}
count ++;
}
else
{
out.println("未找到上传文件!\n");
}
}
disConnectToDatabase();
%>
</BODY>
</HTML>
package com.pic;import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.image.*;
import org.w3c.dom.*;
import com.sun.image.codec.jpeg.*;
import javax.imageio.*;public class ImgBean_i{
public void ImgBean_i(){}public void ImgYin(String Imagename_biao,String ImgName){
try{
File _file = new File(ImgName);
Image src = ImageIO.read(_file);
int wideth=src.getWidth(null);
int height=src.getHeight(null);
BufferedImage image=new BufferedImage(wideth,height,BufferedImage.TYPE_INT_RGB);
Graphics g=image.createGraphics();
g.drawImage(src,0,0,wideth,height,null);//水印文件
File _filebiao = new File(Imagename_biao);
Image src_biao = ImageIO.read(_filebiao);
int wideth_biao=src_biao.getWidth(null);
int height_biao=src_biao.getHeight(null);
g.drawImage(src_biao,wideth-110,height-110,wideth_biao,height_biao,null);
//水印文件在原图片文件的位置,原图片文件的右下角为wideth-0,height-0
g.dispose();
FileOutputStream out=new FileOutputStream(ImgName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
以下是修改过的smartupload代码
<%@ page language="java" import="com.jspsmart.upload.*" import="java.util.*,java.text.*"%>
<%@ page contentType="text/html;charset=GBK" %><jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload"/><HTML><META http-equiv=Content-Type content="text/html; charset=gb2312">
<LINK href="images/css.css" rel=stylesheet>
<BODY BGCOLOR="white" leftmargin="0" topmargin="0">
<%
int count=0;
String path=request.getRealPath("");
mySmartUpload.initialize(pageContext); mySmartUpload.upload(); connectToDatabase();
for (int i=0;i<mySmartUpload.getFiles().getCount();i++){ com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i); if (!myFile.isMissing()) {
int Size=myFile.getSize() ;
String FileName=myFile.getFileName();
String FileExt=myFile.getFileExt();
String uploadtime=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( java.util.Calendar.getInstance().getTime() );
if(Size>=1000000) {
out.println("文件超过1000K!"); //限制大小
}else{
if((FileExt.equals("jpg")) || (FileExt.equals("JPG")) || (FileExt.equals("gif")) || (FileExt.equals("GIF"))) {
java.util.Date date = new java.util.Date();
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMddHHmmss");
String name1=FileName.substring(0,FileName.indexOf("."));
String name2=FileName.substring(FileName.indexOf("."),FileName.length());
String newname=name1+formatter1.format(date)+name2;
//session.putValue("path1",newname);
myFile.saveAs("/photo/upfile/" + newname);
if(Integer.parseInt(sy)==1) {
com.pic.ImgBean_i bbb= new com.pic.ImgBean_i();
bbb.ImgYin(path+"/photo/images/shuiyin.gif",path+"/photo/upfile/" + newname);//这里是水印文件的来源地址,可修改成取得下拉菜单值的形式,这样就可以实现打文字水印或者图片水印了
}
stmt.executeUpdate("insert into img_photo (name,http,userid,classid,title,content,imgsize,format,num,primflag,posttime) values ()");
out.println(("<br/><br/><center>图片上传成功!<br/>").toString());
}else{
out.println("文件格式不对!");
}
}
count ++;
}
else
{
out.println("未找到上传文件!\n");
}
}
disConnectToDatabase();
%>
</BODY>
</HTML>
http://www.huaxin99.com.cn/fqfblog/showlog.asp?log_id=557
这里有另外一篇
http://community.csdn.net/Expert/topic/3458/3458714.xml?temp=.314831
这个方法我以前也试过,当时是水印图片没做出透明效果,所以怎么试也不成功,就是直接将水印图片直接覆盖到原图片上。刚才试了一下,透明的图片是可以透明显示的,但是水印上的图片保存下来就没有了透明度的alpha值,显示出来的效果变灰了,我想请问怎么才能把一个水印图片正确的保存下来呢?现在的效果就像是直接在图片上写文字一样,还是会把源图片覆盖的。不过我已经找到问题的解决办法,另一种方式,希望您能把上面的问题再回答一下,谢谢。等下再结帖,一定给分的,谢谢啦。
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;public class WaterMark
{
public void imgMark(String strOriginalFileName,String strWaterMarkFileName)
{
try
{
//源文件
File fileOriginal = new File(strOriginalFileName);
Image imageOriginal = ImageIO.read(fileOriginal);
int widthOriginal = imageOriginal.getWidth(null);
int heightOriginal = imageOriginal.getHeight(null);
System.out.println("widthOriginal:" + widthOriginal + "\theightOriginal:" + heightOriginal); BufferedImage bufImage = new BufferedImage(widthOriginal,heightOriginal,BufferedImage.TYPE_INT_RGB);
Graphics g = bufImage.createGraphics();
g.drawImage(imageOriginal,0,0,widthOriginal,heightOriginal,null); //水印文件
File fileWaterMark = new File(strWaterMarkFileName);
Image imageWaterMark = ImageIO.read(fileWaterMark);
int widthWaterMark = imageWaterMark.getWidth(null);
int heightWaterMark = imageWaterMark.getHeight(null);
System.out.println("widthWaterMark:" + widthWaterMark + "\theightWaterMark:" + heightWaterMark); //水印文件在源文件的右下角
g.drawImage(imageWaterMark,widthOriginal - widthWaterMark,heightOriginal - heightWaterMark,widthWaterMark,heightWaterMark,null);
g.dispose(); FileOutputStream fos = new FileOutputStream(strOriginalFileName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
encoder.encode(bufImage);
fos.flush();
fos.close();
fos = null;
}
catch(Exception e)
{
e.printStackTrace();
}
} public static void main(String[] args)
{
WaterMark waterMark = new WaterMark();
if(args.length == 2)
{
waterMark.imgMark(args[0],args[1]);
}
else
{
System.out.println("Java WaterMark fileOriginal fileWaterMark");
}
}
}
int width = bi.getWidth();
int height = bi.getHeight();
int degree = 50; // set the water degree // set the rect area water
for (int i = width / 4; i < width * 3 / 4; i++) {
for (int j = height / 4; j < height * 3 / 4; j++) {
Color c = new Color(bi.getRGB(i, j));
int r = c.getRed() * degree / 100;
int g = c.getGreen() * degree / 100;
int b = c.getBlue() * degree / 100;
c = new Color(r, g, b);
bi.setRGB(i, j, c.getRGB());
}
}
return bi;
}