1 public class ImageFixSizeUtil {
2
3 /** * 图片文件读取 * * @param srcImgPath * @return */
4 private static BufferedImage InputImage(String srcImgPath) {
5 BufferedImage srcImage = null;
6 try {
7 FileInputStream in = new FileInputStream(srcImgPath);
8 srcImage = javax.imageio.ImageIO.read(in);
9 } catch (IOException e) {
10 e.printStackTrace();
11 }
12 return srcImage;
13 }
14
15 /**
16 * * 指定长或者宽的最大值来压缩图片 * * @param srcImgPath * :源图片路径 * @param outImgPath *
17 * :输出的压缩图片的路径 * @param maxLength * :长或者宽的最大值
18 */
19 public static void compressImage(String srcImgPath, String outImgPath, int maxLength) {
20 // 得到图片
21 BufferedImage src = InputImage(srcImgPath);
22 if (null != src) {
23 int old_w = src.getWidth();
24 // 得到源图宽
25 int old_h = src.getHeight();
26 // 得到源图长
27 int new_w = 0;
28 // 新图的宽
29 int new_h = 0;
30 // 新图的长
31 // 根据图片尺寸压缩比得到新图的尺寸
32 if (old_w > old_h) {
33 // 图片要缩放的比例
34 new_w = maxLength;
35 new_h = (int) Math.round(old_h * ((float) maxLength / old_w));
36 } else {
37 new_w = (int) Math.round(old_w * ((float) maxLength / old_h));
38 new_h = maxLength;
39 }
40 disposeImage(src, outImgPath, new_w, new_h);
41 }
42 }
43
44
45 /**
46 * * 指定长或者宽的最大值来压缩图片 * * @param srcImgPath * :源图片路径 * @param outImgPath *
47 * :输出的压缩图片的路径 * @param maxLength * :长或者宽的最大值
48 * @return
49 */
50 public static BufferedImage compressImages(BufferedImage src, int maxLength) {
51 // 得到图片
52 BufferedImage image=null;
53 if (null != src) {
54 int old_w = src.getWidth();
55 // 得到源图宽
56 int old_h = src.getHeight();
57 // 得到源图长
58 int new_w = 0;
59 // 新图的宽
60 int new_h = 0;
61 // 新图的长
62 // 根据图片尺寸压缩比得到新图的尺寸
63 // if (old_w > old_h) {
64 // // 图片要缩放的比例
65 // new_w = maxLength;
66 // new_h = (int) Math.round(old_h * ((float) maxLength / old_w));
67 // } else {
68 // new_w = (int) Math.round(old_w * ((float) maxLength / old_h));
69 // new_h = maxLength;
70 // }
71 new_w = (int) Math.round(old_w * ((float) maxLength / old_h));
72 new_h = (int) Math.round(old_h * ((float) maxLength / old_w));
73 image=disposeImages(src, new_w, new_h);
74 }
75 return image;
76 }
77
78 /**
79 * * 处理图片 * * @param src * @param outImgPath * @param new_w * @param new_h
80 */
81 private static BufferedImage disposeImages(BufferedImage src, int new_w, int new_h) {
82 // 得到图片
83 int old_w = src.getWidth();
84 // 得到源图宽
85 int old_h = src.getHeight();
86 // 得到源图长
87 BufferedImage newImg = null;
88 // 判断输入图片的类型
89 switch (src.getType()) {
90 case 13:
91 // png,gifnewImg = new BufferedImage(new_w, new_h,
92 // BufferedImage.TYPE_4BYTE_ABGR);
93 break;
94 default:
95 newImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
96 break;
97 }
98 Graphics2D g = newImg.createGraphics();
99 // 从原图上取颜色绘制新图
100 g.drawImage(src, 0, 0, old_w, old_h, null);
101 g.dispose();
102 // 根据图片尺寸压缩比得到新图的尺寸
103 newImg.getGraphics().drawImage(src.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0, 0, null);
104 // 调用方法输出图片文件
105 return newImg;
106 }
107
108 /**
109 * * 处理图片 * * @param src * @param outImgPath * @param new_w * @param new_h
110 */
111 private synchronized static void disposeImage(BufferedImage src, String outImgPath, int new_w, int new_h) {
112 // 得到图片
113 int old_w = src.getWidth();
114 // 得到源图宽
115 int old_h = src.getHeight();
116 // 得到源图长
117 BufferedImage newImg = null;
118 // 判断输入图片的类型
119 switch (src.getType()) {
120 case 13:
121 // png,gifnewImg = new BufferedImage(new_w, new_h,
122 // BufferedImage.TYPE_4BYTE_ABGR);
123 break;
124 default:
125 newImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
126 break;
127 }
128 Graphics2D g = newImg.createGraphics();
129 // 从原图上取颜色绘制新图
130 g.drawImage(src, 0, 0, old_w, old_h, null);
131 g.dispose();
132 // 根据图片尺寸压缩比得到新图的尺寸
133 newImg.getGraphics().drawImage(src.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0, 0, null);
134 // 调用方法输出图片文件
135 OutImage(outImgPath, newImg);
136 }
137
138 /**
139 * * 将图片文件输出到指定的路径,并可设定压缩质量 * * @param outImgPath * @param newImg * @param
140 * per
141 */
142 private static void OutImage(String outImgPath, BufferedImage newImg) {
143 // 判断输出的文件夹路径是否存在,不存在则创建
144 File file = new File(outImgPath);
145 if (!file.getParentFile().exists()) {
146 file.getParentFile().mkdirs();
147 } // 输出到文件流
148 try {
149 ImageIO.write(newImg, outImgPath.substring(outImgPath.lastIndexOf(".") + 1), new File(outImgPath));
150 } catch (FileNotFoundException e) {
151 e.printStackTrace();
152 } catch (IOException e) {
153 e.printStackTrace();
154 }
155 }
156 }