自由自在的给上传图片打水印

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> 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值