java类(ImageUtil.java)

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import javax.imageio.ImageIO; public class ImageUtil {
private BufferedImage i = null;
/*
* 上传图片
* */
public void setImg(String imgpath) {
try {
this.i = ImageIO.read(new FileInputStream(imgpath));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} public ImageUtil() { } public ImageUtil(String imgpath) {
try {
this.i = ImageIO.read(new FileInputStream(imgpath));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* 图片加水印文字
* */
public void txt(String s, int fontsize, String imgpath) {
Graphics g = this.i.getGraphics();
g.setFont(new Font("隶书", Font.BOLD, fontsize));
// g.setColor(Color.RED);
g.setColor(new Color(255, 255, 255, 80));
g.drawString(s, i.getWidth() - 200, this.i.getHeight() - fontsize - 10);
try {
ImageIO.write(this.i, "jpg", new File(imgpath));
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* 图片加水印logo
* */
public void logo(String logopath, String imgpath) {
Graphics g = this.i.getGraphics();
try {
BufferedImage logo = ImageIO.read(new File(logopath));
g.drawImage(logo, this.i.getWidth() - 189 - 10,
this.i.getHeight() - 69 - 10, this.i.getWidth() - 10,
this.i.getHeight() - 10, 0, 0, logo.getWidth(),
logo.getHeight(), null);
ImageIO.write(this.i, "jpg", new File(imgpath));
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* 缩略图
* */
public void thumd(int ww, int hh, String imgpath) { int w = this.i.getWidth(); int h = this.i.getHeight(); BufferedImage nimg = new BufferedImage(ww, hh, BufferedImage.TYPE_INT_RGB); Graphics g = nimg.getGraphics(); g.drawImage(this.i, 0, 0, ww, hh, 0, 0, w, h, null); try { ImageIO.write(nimg, "jpg", new File(imgpath)); } catch (IOException e) { e.printStackTrace(); } } }

使用上传照片在jsp中使用

SmartUpload su = new SmartUpload();
su.initialize(pageContext);
su.upload();
ImageUtil iu = new ImageUtil();
for(int i=0;i<su.getFiles().getCount();i++){
File f = su.getFiles().getFile(i);
if(f.isMissing()){
continue;
}
UUID u = UUID.randomUUID();
String path = "/upload/"+u.toString()+"."+f.getFileExt();
f.saveAs(path);
String pp = request.getServletContext().getRealPath(path);
iu.setImg(pp);
}

最新文章

  1. Uva 1220,Hali-Bula 的晚会
  2. What are definitions of ​Model, Inference and Algorithm and its associations ?
  3. JNI-数据类型
  4. javascript进阶——测试和打包分发
  5. commands - `ls`
  6. 基于jQuery的上下无缝滚动应用(单行或多行)
  7. sql 行转列 PIVOT 列转行 UNPIVOT
  8. Redis中的master-slave&amp;sentinel
  9. ASP.NET CORE小试牛刀:干货(完整源码)
  10. DBA_TABLES ALL_TABLES USER_TABLES
  11. 闭包函数&amp;回调函数
  12. python3 自动识图
  13. SpringBoot整合Swagger2
  14. 【WebService】WebService之CXF和Spring整合(六)
  15. golang学习笔记 ---面向并发的内存模型
  16. 在Centos7下发布.NET CORE项目[转]
  17. [数据结构与算法] : AVL树
  18. 模型的偏差bias以及方差variance
  19. CentOS LDAP安装配置
  20. 《BAT前端进阶[师徒班]》学习总结

热门文章

  1. MD5加密算法Java代码
  2. 【kotlin】kotlin中List中添加List怎么操作
  3. Delphi GDI对象之绘制文本
  4. 系统重装 如何转换GPT的磁盘格式为MBR或者反过来
  5. C++中结构和类的区别
  6. 如何给老婆解释什么是RESTful
  7. UBUNTU安装PHP,即所谓得LAMP
  8. Android Webview的测试
  9. Struts2的运行流程及其工作原理
  10. node-sass 安装失败win32-x64-48_binding.node