1 /**
2 * 缩略图实现,将图片(jpg、bmp、png、gif等等)真实的变成想要的大小
3 */
4 package com.hljzr.system.util;
5
6 import java.awt.Image;
7 import java.awt.image.BufferedImage;
8 import java.io.File;
9 import java.io.FileOutputStream;
10 import java.io.IOException;
11 import javax.imageio.ImageIO;
12 import com.sun.image.codec.jpeg.JPEGCodec;
13 import com.sun.image.codec.jpeg.JPEGImageEncoder;
14
15 /*******************************************************************************
16 * 缩略图类(通用) 本java类能将jpg、bmp、png、gif图片文件,进行等比或非等比的大小转换。 具体使用方法
17 * compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))
18 */
19 public class CompressPicDemo {
20 private File file = null; // 文件对象
21 private String inputDir; // 输入图路径
22 private String outputDir; // 输出图路径
23 private String inputFileName; // 输入图文件名
24 private String outputFileName; // 输出图文件名
25 private int outputWidth = 100; // 默认输出图片宽
26 private int outputHeight = 100; // 默认输出图片高
27 private boolean proportion = true; // 是否等比缩放标记(默认为等比缩放)
28 public CompressPicDemo() { // 初始化变量
29 inputDir = "";
30 outputDir = "";
31 inputFileName = "";
32 outputFileName = "";
33 outputWidth = 100;
34 outputHeight = 100;
35 }
36 public void setInputDir(String inputDir) {
37 this.inputDir = inputDir;
38 }
39 public void setOutputDir(String outputDir) {
40 this.outputDir = outputDir;
41 }
42 public void setInputFileName(String inputFileName) {
43 this.inputFileName = inputFileName;
44 }
45 public void setOutputFileName(String outputFileName) {
46 this.outputFileName = outputFileName;
47 }
48 public void setOutputWidth(int outputWidth) {
49 this.outputWidth = outputWidth;
50 }
51 public void setOutputHeight(int outputHeight) {
52 this.outputHeight = outputHeight;
53 }
54 public void setWidthAndHeight(int width, int height) {
55 this.outputWidth = width;
56 this.outputHeight = height;
57 }
58
59 /*
60 * 获得图片大小
61 * 传入参数 String path :图片路径
62 */
63 public long getPicSize(String path) {
64 file = new File(path);
65 return file.length();
66 }
67
68 // 图片处理
69 public String compressPic() {
70 try {
71 //获得源文件
72 file = new File(inputDir + inputFileName);
73 if (!file.exists()) {
74 return "";
75 }
76 Image img = ImageIO.read(file);
77 // 判断图片格式是否正确
78 if (img.getWidth(null) == -1) {
79 System.out.println(" can't read,retry!" + "<BR>");
80 return "no";
81 } else {
82 int newWidth; int newHeight;
83 // 判断是否是等比缩放
84 if (this.proportion == true) {
85
86 if(outputWidth>img.getWidth(null)){
87 newWidth = img.getWidth(null);
88 newHeight = img.getHeight(null);
89 }else{
90 // 为等比缩放计算输出的图片宽度及高度
91
92 double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1;
93 double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1;
94 // 根据缩放比率大的进行缩放控制
95 double rate = rate1 > rate2 ? rate1 : rate2;
96
97 newWidth = (int) (((double) img.getWidth(null)) / rate);
98 newHeight = (int) (((double) img.getHeight(null)) / rate);
99 }
100
101 } else {
102 if(outputWidth>img.getWidth(null)){
103 newWidth = img.getWidth(null);
104 newHeight = img.getHeight(null);
105 }else{
106 newWidth = outputWidth; // 输出的图片宽度
107 newHeight = outputHeight; // 输出的图片高度
108 }
109
110 }
111 BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);
112
113 /*
114 * Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的
115 * 优先级比速度高 生成的图片质量比较好 但速度慢
116 */
117 tag.getGraphics().drawImage(img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);
118 FileOutputStream out = new FileOutputStream(outputDir + outputFileName);
119 // JPEGImageEncoder可适用于其他图片类型的转换
120 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
121 encoder.encode(tag);
122 out.close();
123 }
124 } catch (IOException ex) {
125 ex.printStackTrace();
126 }
127 return "ok";
128 }
129 public String compressPic (String inputDir, String outputDir, String inputFileName, String outputFileName) {
130 // 输入图路径
131 this.inputDir = inputDir;
132 // 输出图路径
133 this.outputDir = outputDir;
134 // 输入图文件名
135 this.inputFileName = inputFileName;
136 // 输出图文件名
137 this.outputFileName = outputFileName;
138 return compressPic();
139 }
140 public String compressPic(String inputDir, String outputDir, String inputFileName, String outputFileName, int width, int height, boolean gp) {
141 // 输入图路径
142 this.inputDir = inputDir;
143 // 输出图路径
144 this.outputDir = outputDir;
145 // 输入图文件名
146 this.inputFileName = inputFileName;
147 // 输出图文件名
148 this.outputFileName = outputFileName;
149 // 设置图片长宽
150 setWidthAndHeight(width, height);
151 // 是否是等比缩放 标记
152 this.proportion = gp;
153 return compressPic();
154 }
155
156 // main测试
157 // compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))
158 public static void main(String[] arg) {
159 CompressPicDemo mypic = new CompressPicDemo();
160 System.out.println("输入的图片大小:" + mypic.getPicSize("f:\\2.jpg")/1024 + "KB");
161 int count = 0; // 记录全部图片压缩所用时间
162 for (int i = 0; i < 1; i++) {
163 int start = (int) System.currentTimeMillis(); // 开始时间
164 mypic.compressPic("f:\\", "e:\\", "2.jpg", "r1"+i+".jpg", 1000, 1000, true);
165 int end = (int) System.currentTimeMillis(); // 结束时间
166 int re = end-start; // 但图片生成处理时间
167 count += re; System.out.println("第" + (i+1) + "张图片压缩处理使用了: " + re + "毫秒");
168 System.out.println("输出的图片大小:" + mypic.getPicSize("e:\\r1"+i+".jpg")/1024 + "KB");
169 }
170 System.out.println("总共用了:" + count + "毫秒");
171 }
172
173 }