fs-danaus
2024-08-09 7204e3dff0490732e861ccd1338e3e3c31d768c6
提交 | 用户 | age
a6a76f 1 package com.yc.utils;
F 2
3 /**
4  * 图片缩小算法,方形区域抽样 by alfred
5  **/
6
7 import com.yc.servlet.BuildFormat;
8
9 import java.awt.*;
10 import java.awt.image.BufferedImage;
11 import java.io.File;
12 import java.io.IOException;
13
14 public class NewScaleImage {
15
16     int width = 0;
17     int height = 0;
18     private String destFile;
19     private String srcFileName;
20     BufferedImage srcBufferImage;
21     private int[] rgbArray = null;
22
23
24     public NewScaleImage(File file) throws IOException {
25         String srcFile = file.getName();
26         srcFileName = file.getAbsolutePath();
27         this.destFile = srcFile.substring(0, srcFile.lastIndexOf(".")) + ".jpg";
28         try {
29             srcBufferImage = javax.imageio.ImageIO.read(file);
30         } catch (Exception e) {
31             try {
32                 //设置默认图片
33                 String urlmr = file.getPath().substring(0, file.getPath().indexOf("WEB-INF"));
34                 if (file.getPath().indexOf(".") != -1) {
35                     String typeim = file.getPath().substring(file.getPath().indexOf("."), file.getPath().length());
36                     if (typeim.toLowerCase().indexOf("jpg") != -1) {
37                         urlmr = urlmr + "/smallpic/specialpic/jpg.jpg";
38                     } else if (typeim.toLowerCase().indexOf("png") != -1) {
39                         urlmr = urlmr + "/smallpic/specialpic/png.png";
40                     }
41                 }
42                 File file2 = new File(urlmr);
43                 srcBufferImage = javax.imageio.ImageIO.read(file2);
44             } catch (Exception e3) {
45                 try {
46                     //图片溢出处理 2014-10-9
47                     Toolkit toolkit = Toolkit.getDefaultToolkit();
48                     Image srcImage = toolkit.getImage(file.getAbsolutePath()); // 构造Image对象
49                     int wideths = -1;
50                     int heights = -1;
51                     boolean flag = true;
52                     while (flag) {
53                         wideths = srcImage.getWidth(null); // 得到源图宽
54                         heights = srcImage.getHeight(null); // 得到源图长
55                         //  System.out.println("wideth:" + wideths + " height:" + heights);
56                         if (wideths > 0 && heights > 0) { // 因为 Toolkit.getImage 是异步读取,如果
57                             // wideth 和 height
58                             // 都大于0,表明图片已经加载完毕
59                             // imageCanvas.setImage(srcImage);
60                             flag = false;
61                         } else {
62                             try {
63                                 Thread.sleep(10);
64                             } catch (Exception e2) {
65                                 e2.printStackTrace();
66                             }
67                         }
68                     }
69                     // 加载完毕,输出
70                     int w = 1024;
71                     float s = (float) wideths / 1024.0f;
72                     int h = (int) ((float) heights / s);
73                     BufferedImage bufferedImage = new BufferedImage(w, h,
74                             BufferedImage.TYPE_INT_RGB);
75                     srcBufferImage = bufferedImage;
76                 } catch (Exception e1) {
77                     e1.printStackTrace();
78                 }
79
80             }
81         }
82         width = srcBufferImage.getWidth();
83         height = srcBufferImage.getHeight();
84
85     }
86
87     public BufferedImage scaleImage(int outWidth, int outHeight) {
88
89         rgbArray = srcBufferImage.getRGB(0, 0, width, height, null, 0, width);
90         if (DetermineResultSize(outWidth, outHeight) == 1) {
91             return srcBufferImage;
92         }
93         BufferedImage pbFinalOut = new BufferedImage(outWidth, outHeight,
94                 BufferedImage.TYPE_INT_RGB);
95
96         double hScale = ((double) width) / ((double) outWidth);//宽缩小的倍数
97         double vScale = ((double) height) / ((double) outHeight);//高缩小的倍数
98
99         int winX0, winY0, winX1, winY1;
100         int valueRGB = 0;
101         long R, G, B;
102         int x, y, i, j;
103         int n;
104         for (y = 0; y < outHeight; y++) {
105             winY0 = (int) (y * vScale + 0.5);//得到原图高的Y坐标
106             if (winY0 < 0) {
107                 winY0 = 0;
108             }
109             winY1 = (int) (winY0 + vScale + 0.5);
110             if (winY1 > height) {
111                 winY1 = height;
112             }
113             for (x = 0; x < outWidth; x++) {
114                 winX0 = (int) (x * hScale + 0.5);
115                 if (winX0 < 0) {
116                     winX0 = 0;
117                 }
118                 winX1 = (int) (winX0 + hScale + 0.5);
119                 if (winX1 > width) {
120                     winX1 = width;
121                 }
122                 R = 0;
123                 G = 0;
124                 B = 0;
125                 for (i = winX0; i < winX1; i++) {
126                     for (j = winY0; j < winY1; j++) {
127                         valueRGB = rgbArray[width * j + i];
128                         R += GetRedValue(valueRGB);
129                         G += GetGreenValue(valueRGB);
130                         B += GetBlueValue(valueRGB);
131                     }
132                 }
133                 n = (winX1 - winX0) * (winY1 - winY0);
134                 R = (int) (((double) R) / n + 0.5);
135                 G = (int) (((double) G) / n + 0.5);
136                 B = (int) (((double) B) / n + 0.5);
137                 valueRGB = ComRGB(Clip((int) R), Clip((int) G), Clip((int) B));
138                 pbFinalOut.setRGB(x, y, valueRGB);
139             }
140         }
141         return pbFinalOut;
142     }
143
144     /**
145      * 决定图像尺寸
146      */
147     public int DetermineResultSize(int w, int h) {
148         double scaleH, scaleV;
149         scaleH = (double) w / (double) width;
150         scaleV = (double) h / (double) height;
151         // 判断一下scaleH,scaleV,不做放大操作
152         if (scaleH >= 1.0 && scaleV >= 1.0) {
153             return 1;
154         }
155         return 0;
156
157     } // end of DetermineResultSize()
158
159     int Clip(int x) {
160         if (x < 0)
161             return 0;
162         if (x > 255)
163             return 255;
164         return x;
165     }
166
167     private int GetRedValue(int rgbValue) {
168         int temp = rgbValue & 0x00ff0000;
169         return temp >> 16;
170     }
171
172     private int GetGreenValue(int rgbValue) {
173         int temp = rgbValue & 0x0000ff00;
174         return temp >> 8;
175     }
176
177     private int GetBlueValue(int rgbValue) {
178         return rgbValue & 0x000000ff;
179     }
180
181     private int ComRGB(int redValue, int greenValue, int blueValue) {
182
183         return (redValue << 16) + (greenValue << 8) + blueValue;
184     }
185
186     public void resize(int w, int h) throws IOException {
187
188         File srcFile = new File(srcFileName);
189         srcBufferImage = javax.imageio.ImageIO.read(srcFile);
190         width = srcBufferImage.getWidth();
191         height = srcBufferImage.getHeight();
192         BufferedImage image = scaleImage(w, h);
193         BuildFormat.createImage(destFile,image);
194         //FileOutputStream out = new FileOutputStream(destFile);
195         //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
196         //encoder.encode(_image);
197        // out.close();
198     }
199
200     public void resizeByWidth(int w) throws IOException {
201         float h = ((float) height * w / width);
202         resize(w, new Float(h).intValue());
203     }
204
205
206     public void resizeByHeight(int h) throws IOException {
207         float w = ((float) width * h / height);
208         resize(new Float(w).intValue(), h);
209     }
210
211     public void resizeFix(int w, int h) throws IOException {
212         if (width <= w && height <= h) {
213             resize(width, height);
214             return;
215         }
216
217         if ((float) width / height > (float) w / h) {
218             resizeByWidth(w);
219         } else {
220             resizeByHeight(h);
221         }
222     }
223
224     public BufferedImage scaleImgFix(int outW, int outH) {
225         if (width <= outW && height <= outH) {
226             return scaleImage(width, height);
227         }
228
229         if ((float) width / height > (float) outW / outH) {
230             return scaleImage(outW, getResizeHeight(outW));
231
232         } else {
233             return scaleImage(getResizeWidth(outH), outH);
234
235         }
236     }
237
238     //    如果图片的高小于宽,计算缩小后的高度.
239     public int getResizeHeight(int w) {
240         float h = ((float) height * w / width);
241         return new Float(h).intValue();
242     }
243
244     //如果图片的高大于宽,计算缩小后的宽度.
245     public int getResizeWidth(int h) {
246         float w = ((float) width * h / height);
247         return new Float(w).intValue();
248     }
249
250     public void setDestFile(String fileName) {
251         destFile = fileName;
252     }
253
254     public String getDestFile() {
255         return destFile;
256     }
257
258     public static void main(String[] args) {
259     }
260
261     public String getSrcFileName() {
262         return srcFileName;
263     }
264
265     public void setSrcFileName(String srcFileName) {
266         this.srcFileName = srcFileName;
267     }
268
269
270 }