java 自由自在的给上传图片打水印
2013-05-30 10:39
344 查看
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 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>
相关文章推荐
- 上传图片java中用到的图片(等比缩放、切割、彩色转黑白、添加水印)等操作
- Java生成验证码、图片上传、下载、图片水印
- java实现图片上传并添加水印demo
- 自由自在的给上传图片打水印
- fckeditor-java-2.5--给上传图片添加水印
- java 上传图片 打水印
- 自由自在的给上传图片打水印
- java--上传图片添加水印
- java 上传图片 打水印
- 自由自在的给上传图片打水印
- JAVA技术实现上传图片水印效果
- 解决在ueditor中上传图片通过后台java+SpringMVC添加水印方法
- 使用Servlet上传多张图片——Dao层(BaseDao.java)
- [转]文件上传及图片水印
- java操作pdf生成图片水印
- Java实现图片的压缩(包括文件上传的图片压缩)
- java 给图片打水印
- 为freetextbox1.6.5上传图片加上水印
- 七牛云存储____js打水印加上传图片
- JAVA微信公众号开发第8篇JSSDK&图片上传预览