方法1:[第一种方法比后一种生成的缩略图要清晰]
Java代码
1.import javax.imageio.ImageIO;
2.import java.awt.image.BufferedImage;
3.import java.awt.image.ColorModel;
4.import java.awt.image.WritableRaster;
5.import java.awt.*;
6.import java.awt.geom.AffineTransform;
7.import java.io.InputStream;
8.import java.io.File;
9.import java.io.FileOutputStream;
10.
11.public class Test {
12. public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
13. // targetW,targetH分别表示目标长和宽
14. int type = source.getType();
15. BufferedImage target = null;
16. double sx = (double) targetW / source.getWidth();
17. double sy = (double) targetH / source.getHeight();
18. //这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放
19. //则将下面的if else语句注释即可
20. if(sx>sy)
21. {
22. sx = sy;
23. targetW = (int)(sx * source.getWidth());
24. }else{
25. sy = sx;
26. targetH = (int)(sy * source.getHeight());
27. }
28. if (type == BufferedImage.TYPE_CUSTOM) { //handmade
29. ColorModel cm = source.getColorModel();
30. WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
31. boolean alphaPremultiplied = cm.isAlphaPremultiplied();
32. target = new BufferedImage(cm, raster, alphaPremultiplied, null);
33. } else
34. target = new BufferedImage(targetW, targetH, type);
35. Graphics2D g = target.createGraphics();
36. //smoother than exlax:
37. g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY );
38. g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
39. g.dispose();
40. return target;
41. }
42. public static void saveImageAsJpg (String fromFileStr,String saveToFileStr,int width,int hight)
43. throws Exception {
44. BufferedImage srcImage;
45.// String ex = fromFileStr.substring(fromFileStr.indexOf("."),fromFileStr.length());
46. String imgType = "JPEG";
47. if (fromFileStr.toLowerCase().endsWith(".png")) {
48. imgType = "PNG";
49. }
50.// System.out.println(ex);
51. File saveFile=new File(saveToFileStr);
52. File fromFile=new File(fromFileStr);
53. srcImage = ImageIO.read(fromFile);
54. if(width > 0 || hight > 0)
55. {
56. srcImage = resize(srcImage, width, hight);
57. }
58. ImageIO.write(srcImage, imgType, saveFile);
59.
60. }
61.
62. public static void main (String argv[]) {
63. try{
64. //参数1(from),参数2(to),参数3(宽),参数4(高)
65. Test.saveImageAsJpg("E:/Document/My Pictures/3.gif","c:/6.gif",50,50);
66. } catch(Exception e)
67. {
68. e.printStackTrace();
69. }
70.
71. }
72.}
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.io.InputStream;
import java.io.File;
import java.io.FileOutputStream;
public class Test {
public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
// targetW,targetH分别表示目标长和宽
int type = source.getType();
BufferedImage target = null;
double sx = (double) targetW / source.getWidth();
double sy = (double) targetH / source.getHeight();
//这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放
//则将下面的if else语句注释即可
if(sx>sy)
{
sx = sy;
targetW = (int)(sx * source.getWidth());
}else{
sy = sx;
targetH = (int)(sy * source.getHeight());
}
if (type == BufferedImage.TYPE_CUSTOM) { //handmade
ColorModel cm = source.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
target = new BufferedImage(cm, raster, alphaPremultiplied, null);
} else
target = new BufferedImage(targetW, targetH, type);
Graphics2D g = target.createGraphics();
//smoother than exlax:
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY );
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return target;
}
public static void saveImageAsJpg (String fromFileStr,String saveToFileStr,int width,int hight)
throws Exception {
BufferedImage srcImage;
// String ex = fromFileStr.substring(fromFileStr.indexOf("."),fromFileStr.length());
String imgType = "JPEG";
if (fromFileStr.toLowerCase().endsWith(".png")) {
imgType = "PNG";
}
// System.out.println(ex);
File saveFile=new File(saveToFileStr);
File fromFile=new File(fromFileStr);
srcImage = ImageIO.read(fromFile);
if(width > 0 || hight > 0)
{
srcImage = resize(srcImage, width, hight);
}
ImageIO.write(srcImage, imgType, saveFile);
}
public static void main (String argv[]) {
try{
//参数1(from),参数2(to),参数3(宽),参数4(高)
Test.saveImageAsJpg("E:/Document/My Pictures/3.gif","c:/6.gif",50,50);
} catch(Exception e)
{
e.printStackTrace();
}
}
}
方法2:
Java代码
1. import java.io.*;
2. import java.util.*;
3. import com.sun.image.codec.jpeg.*;
4. import java.awt.image.*;
5. import java.awt.*;
6. import java.net.*;
7. import java.applet.*;
8. import java.sql.*;
9.
10.//缩略图类,
11.//本java类能将jpg图片文件,进行等比或非等比的大小转换。
12.//具体使用方法
13.//s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))
14. public class Tes {
15. String InputDir; //输入图路径
16. String OutputDir; //输出图路径
17. String InputFileName; //输入图文件名
18. String OutputFileName; //输出图文件名
19. int OutputWidth = 80; //默认输出图片宽
20. int OutputHeight = 80; //默认输出图片高
21. int rate = 0;
22. boolean proportion = true; //是否等比缩放标记(默认为等比缩放)
23.
24. public Tes() {
25.//初始化变量
26. InputDir = "";
27. OutputDir = "";
28. InputFileName = "";
29. OutputFileName = "";
30. OutputWidth = 80;
31. OutputHeight = 80;
32. rate = 0;
33. }
34.
35. public void setInputDir(String InputDir) {
36. this.InputDir = InputDir;
37. }
38.
39. public void setOutputDir(String OutputDir) {
40. this.OutputDir = OutputDir;
41. }
42.
43. public void setInputFileName(String InputFileName) {
44. this.InputFileName = InputFileName;
45. }
46.
47. public void setOutputFileName(String OutputFileName) {
48. this.OutputFileName = OutputFileName;
49. }
50.
51. public void setOutputWidth(int OutputWidth) {
52. this.OutputWidth = OutputWidth;
53. }
54.
55. public void setOutputHeight(int OutputHeight) {
56. this.OutputHeight = OutputHeight;
57. }
58.
59. public void setW_H(int width, int height) {
60. this.OutputWidth = width;
61. this.OutputHeight = height;
62. }
63.
64. public String s_pic() {
65. BufferedImage image;
66. String NewFileName;
67.//建立输出文件对象
68. File file = new File(OutputDir + OutputFileName);
69. FileOutputStream tempout = null;
70. try {
71. tempout = new FileOutputStream(file);
72. } catch (Exception ex) {
73. System.out.println(ex.toString());
74. }
75. Image img = null;
76. Toolkit tk = Toolkit.getDefaultToolkit();
77. Applet app = new Applet();
78. MediaTracker mt = new MediaTracker(app);
79. try {
80. img = tk.getImage(InputDir + InputFileName);
81. mt.addImage(img, 0);
82. mt.waitForID(0);
83. } catch (Exception e) {
84. e.printStackTrace();
85. }
86.
87. if (img.getWidth(null) == -1) {
88. System.out.println(" can't read,retry!" + "<BR>");
89. return "no";
90. } else {
91. int new_w;
92. int new_h;
93. if (this.proportion == true) { //判断是否是等比缩放.
94.//为等比缩放计算输出的图片宽度及高度
95. double rate1 = ((double) img.getWidth(null)) /
96. (double) OutputWidth + 0.1;
97. double rate2 = ((double) img.getHeight(null)) /
98. (double) OutputHeight + 0.1;
99. double rate = rate1 > rate2 ? rate1 : rate2;
100. new_w = (int) (((double) img.getWidth(null)) / rate);
101. new_h = (int) (((double) img.getHeight(null)) / rate);
102. } else {
103. new_w = OutputWidth; //输出的图片宽度
104. new_h = OutputHeight; //输出的图片高度
105. }
106. BufferedImage buffImg = new BufferedImage(new_w, new_h,
107. BufferedImage.TYPE_INT_RGB);
108.
109. Graphics g = buffImg.createGraphics();
110.
111. g.setColor(Color.white);
112. g.fillRect(0, 0, new_w, new_h);
113.
114. g.drawImage(img, 0, 0, new_w, new_h, null);
115. g.dispose();
116.
117. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(tempout);
118. try {
119. encoder.encode(buffImg);
120. tempout.close();
121. } catch (IOException ex) {
122. System.out.println(ex.toString());
123. }
124. }
125. return "ok";
126. }
127.
128. public String s_pic(String InputDir, String OutputDir, String InputFileName,
129. String OutputFileName) {
130.//输入图路径
131. this.InputDir = InputDir;
132.//输出图路径
133. this.OutputDir = OutputDir;
134.//输入图文件名
135. this.InputFileName = InputFileName;
136.//输出图文件名
137. this.OutputFileName = OutputFileName;
138. return s_pic();
139. }
140.
141. public String s_pic(String InputDir, String OutputDir, String InputFileName,
142. String OutputFileName, int width, int height,
143. boolean gp) {
144.//输入图路径
145. this.InputDir = InputDir;
146.//输出图路径
147. this.OutputDir = OutputDir;
148.//输入图文件名
149. this.InputFileName = InputFileName;
150.//输出图文件名
151. this.OutputFileName = OutputFileName;
152.//设置图片长宽
153. setW_H(width, height);
154.//是否是等比缩放 标记
155. this.proportion = gp;
156. return s_pic();
157. }
158.
159. public static void main(String[] a) {
160.//s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度)
161. Tes mypic = new Tes();
162. System.out.println(
163. mypic.s_pic("E:\Document\My Pictures\",
164. "E:\Document\My Pictures\",
165. "topbg-3.gif", "3.gif", 400, 400, true)
166. );
167.
168. }
169. }