实现方式及相关工具使用:
- Thumbnailator类库 [size()API]--->推荐
- Java AWT类库,根据缩略比例计算缩略图高度和宽度,使用Image类获取原图的缩放版本,使用ImageIo类保存缩略图 [BufferedImage、ImageIo、Graphics]--->了解
工具版本:
- Eclipse:eclipse-jee-mars-R-win32-x86_64
- JDK:jdk1.8.0_60
具体实现:
1.搭建应用程序框架(使用之前WaterMark的项目开发),首先创建一个项目,这次我们只需要再引入Thumbnailator-0.4.7.jar文件即可
2.首先我们使用第一种实现方式,首先写一个ThumbnailService业务逻辑处理类:
package com.watermark.service;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import net.coobird.thumbnailator.Thumbnails;
/**
* ThumbnailService
* 将上传的图进行等比例缩略
* @author aibinxiao
* @date 2017年6月9日 上午11:00:46
*/
public class ThumbnailService {
public static final int WIDTH = 100;
public static final int HEIGHT = 100;
public String thumbnail(File image, String imageFileName, String uploadPath, String realUploadPath){
String thumFileName = "thum_" + imageFileName;
InputStream is =null;
try {
is = new FileInputStream(image);
String des = realUploadPath + File.separator + thumFileName;
Thumbnails.of(is).size(WIDTH, HEIGHT).toFile(des);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(is!=null){
try {
is.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
return uploadPath + File.separator + thumFileName;
}
}
3.然后写ThumbnailAction来接收请求:
package com.watermark.action;
import java.io.File;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.watermark.entity.PicInfo;
import com.watermark.service.ThumbnailAWTService;
import com.watermark.service.ThumbnailService;
import com.watermark.service.UploadService;
/**
* ThumbnailHandler类
* @author aibinxiao
* @date 2017年6月9日 上午10:46:51
*/
public class ThumbnailAction extends ActionSupport{
private File image; // 要上传的图片文件
private String imageFileName; // 要上传的图片文件名
private String uploadPath; // 要上传的图片文件相对路径
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getUploadPath() {
return uploadPath;
}
public void setUploadPath(String uploadPath) {
this.uploadPath = uploadPath;
}
PicInfo picInfo = new PicInfo();
public PicInfo getPicInfo() {
return picInfo;
}
public void setPicInfo(PicInfo picInfo) {
this.picInfo = picInfo;
}
public String thumbnail() throws Exception{
// 绝对路径,是基于我们的相对路径来获取的
String realUploadPath = ServletActionContext.getServletContext().getRealPath(uploadPath);
UploadService uploadService = new UploadService();
//ThumbnailAWTService thumbnailAWTService = new ThumbnailAWTService();
ThumbnailService thumbnailService = new ThumbnailService();
// 设置图片上传后返回的相对路径
picInfo.setImageURL(uploadService.uploadImage(image, imageFileName, uploadPath, realUploadPath));
// 设置文字水印
//picInfo.setLogoImageURL(thumbnailAWTService.thumbnail(image, imageFileName, uploadPath, realUploadPath));
picInfo.setLogoImageURL(thumbnailService.thumbnail(image, imageFileName, uploadPath, realUploadPath));
return SUCCESS;
}
}
4.配置struts.xml文件
<!-- 批获取图片缩略图 -->
<action name="thumbnail" class="com.watermark.action.ThumbnailAction" method="thumbnail">
<param name="uploadPath">/images</param>
<result name="success">showthumbnail.jsp</result>
</action>
5.编写上传文件的thumbnail.jsp6.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>获取缩略图</title>
</head>
<body>
<h4>请选择图片上传</h4>
<hr/>
<form name = "upload_form" action="${pageContext.request.contextPath}/thumbnail.action" method="post" enctype="multipart/form-data">
<input type="file" name="image" id="image"/><br/>
<input type="submit" value="上传图片"/>
</form>
<hr/>
<a href="${pageContext.request.contextPath}/index.jsp">返回首页</a><br/>
</body>
</html>
6.编写成功页面结果页面showthumbnail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>缩略前后对比</title>
</head>
<body>
<h4>图片信息</h4>
<hr/>
<table width="100%">
<tr>
<td width="50%" align="center">
<img src='${pageContext.request.contextPath}<s:property value="picInfo.imageURL"/>'>
</td>
<td width="50%" align="center">
<img src='${pageContext.request.contextPath}<s:property value="picInfo.logoImageURL"/>'>
</td>
</tr>
</table>
<hr/>
<a href="${pageContext.request.contextPath}/index.jsp">返回首页</a><br/>
</body>
</html>
7.我们开始测试第一种实现方式:
8.由上图可以看到成功通过测试,接下来我们实现另一种方式,添加一个ThumbnailAWTService的类:
package com.watermark.service;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
/**
* Java AWT类库实现缩略图
* @author aibinxiao
* @date 2017年6月9日 上午11:16:21
*/
public class ThumbnailAWTService {
public static final int WIDTH = 100;
public static final int HEIGHT = 100;
public String thumbnail(File image, String imageFileName, String uploadPath, String realUploadPath){
String thumFileName = "thum_" + imageFileName;
OutputStream os = null;
try {
String des = realUploadPath + File.separator + thumFileName;
os = new FileOutputStream(des);
Image image2 = ImageIO.read(image);
int width = image2.getWidth(null);// 获取原图宽度
int height = image2.getHeight(null);// 获取原图高度
int rate1 = width/WIDTH;// 宽度缩略比例
int rate2 = height/HEIGHT;// 高度缩略比例
int rate = 0;
if(rate1 > rate2){// 宽度缩略比例大于高度缩略比例,使用宽度缩略比例
rate = rate1;
}else{
rate = rate2;
}
// 计算缩略图最终的宽度和高度
int newWidth = width/rate;
int newHeight = height/rate;
// 创建一个缓存图片对象
BufferedImage bufferedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
// 将原图通过新的高度宽度绘制到缓存图片对象中 image.SCALE_SMOOTH平滑版本
bufferedImage.getGraphics().drawImage(image2.getScaledInstance(newWidth, newHeight, image2.SCALE_SMOOTH), 0, 0, null);
// "image/jpeg" 获取原图后缀名
//String imageType = image.getName().substring(image.getName().indexOf(".")+1);
String imageType = imageFileName.substring(imageFileName.indexOf(".")+1);
// 通过ImageIO将生成的缓存图片对象输出到对应的输出文件中去
ImageIO.write(bufferedImage, imageType, os);
} catch (Exception e) {
e.printStackTrace();
}finally {
if(os!=null){
try {
os.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
return uploadPath + File.separator + thumFileName;
}
}
9.然后,我们在ThumbnailAction的方法中,修改调用ThumbnailAWTService中的thumbnial()方法,进行测试,经过测试,效果一致!
综上,其实这个只是一个类库的使用,大家需要源码的,可以点击-->https://github.com/xiaoaibin/WaterMark.git
本文为原创文章,如果对你有一点点的帮助,别忘了点赞哦!比心!如需转载,请注明出处,谢谢!