缩略图工具类

在日常开发中,前端时常会出现这样的场景,图片列表对应一个小缩略图预览,这个其实前端也可以做的,这里放用后台代码生成的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.vshow.control.tool;

import java.awt.Image;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageUtil {


public static void main(String[] args) throws Exception {
File file = new File("D:"+File.separator+"nsh.png");
System.out.println(file.getPath());
ImageUtil.makeSmallImageT(file,"png","_nsh.png");
}
/**
* 生成缩略图
* @param srcImageFile 源图片文件的File实例 File file = new File("文件名");
* @param dstImageFileName 待生成的缩略图片完整路径(生成的格式为:image/jpeg);
* @throws Exception
*/
public static void makeSmallImage(File srcImageFile,String dstImageFileName) throws Exception{
FileOutputStream fileOutputStream = null;
JPEGImageEncoder encoder = null;
BufferedImage tagImage = null;
Image srcImage = null;
try{

srcImage = ImageIO.read(srcImageFile);
int srcWidth = srcImage.getWidth(null);//原图片宽度
int srcHeight = srcImage.getHeight(null);//原图片高度
int dstMaxSize = 120;//目标缩略图的最大宽度/高度,宽度与高度将按比例缩写
int dstWidth = srcWidth;//缩略图宽度
int dstHeight = srcHeight;//缩略图高度
float scale = 0;
//计算缩略图的宽和高
if(srcWidth>dstMaxSize){
dstWidth = dstMaxSize;
scale = (float)srcWidth/(float)dstMaxSize;
dstHeight = Math.round((float)srcHeight/scale);
}
srcHeight = dstHeight;
if(srcHeight>dstMaxSize){
dstHeight = dstMaxSize;
scale = (float)srcHeight/(float)dstMaxSize;
dstWidth = Math.round((float)dstWidth/scale);
}
//生成缩略图
tagImage = new BufferedImage(dstWidth,dstHeight,BufferedImage.TYPE_INT_RGB);
tagImage.getGraphics().drawImage(srcImage,0,0,dstWidth,dstHeight,null);
fileOutputStream = new FileOutputStream(dstImageFileName);
encoder = JPEGCodec.createJPEGEncoder(fileOutputStream);
encoder.encode(tagImage);
fileOutputStream.close();
fileOutputStream = null;
}finally{
if(fileOutputStream!=null){
try{
fileOutputStream.close();
}catch(Exception e){
}
fileOutputStream = null;
}
encoder = null;
tagImage = null;
srcImage = null;
System.gc();
}
}


/**
* 生成缩略图2
* @param srcImageFile 源图片文件的File实例 File file = new File("文件名");
* @param dstImageFileName 待生成的缩略图片完整路径(生成的格式为:image/jpeg);
* @throws Exception
*/
public static void makeSmallImageT(File srcImageFile,String type,String tempname) {

try {

//Thumbnails.of(srcImageFile)
//.size(100, 200)
//.outputFormat(type) //生成图片的格式为png
//.outputQuality(0.5f) //生成质量为80%
//.toFile(Constant.FILES+File.separator+"_"+tempname);

FileHandle.copy3(srcImageFile.getPath(), Constant.FILES + File.separator +"_"+tempname+"."+type);

} catch (Exception e) {

FileHandle.copy3(Constant.FILES + File.separator +tempname, Constant.FILES + File.separator +"_"+tempname);
}

}

}