01.<pre class="java" name="code">/**
02. * 改变图片的大小到宽为size,然后高随着宽等比例变化
03. * @param is 上传的图片的输入流
04. * @param os 改变了图片的大小后,把图片的流输出到目标OutputStream
05. * @param size 新图片的宽
06. * @param format 新图片的格式
07. * @throws IOException
08. */
09.public static void resizeImage(InputStream is, OutputStream os, int size, String format) throws IOException {
10. BufferedImage prevImage = ImageIO.read(is);
11. double width = prevImage.getWidth();
12. double height = prevImage.getHeight();
13. double percent = size/width;
14. int newWidth = (int)(width * percent);
15. int newHeight = (int)(height * percent);
16. BufferedImage image = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_BGR);
17. Graphics graphics = image.createGraphics();
18. graphics.drawImage(prevImage, 0, 0, newWidth, newHeight, null);
19. ImageIO.write(image, format, os);
20. os.flush();
21. is.close();
22. os.close();
23.} </pre><br>
24.<br>
25.<pre></pre>
26.<pre class="java" name="code"></pre><pre class="java" name="code"></pre><pre class="java" name="code"></pre><pre class="java" name="code"></pre><pre class="java" name="code">package graphicsTest;
27.
28.import java.awt.Image;
29.import java.awt.image.BufferedImage;
30.import java.io.File;
31.import java.io.FileOutputStream;
32.
33.import javax.imageio.ImageIO;
34.
35.import com.sun.image.codec.jpeg.JPEGCodec;
36.import com.sun.image.codec.jpeg.JPEGImageEncoder;
37.
38.public class GraphicsTest1 {
39.
40. // 图片宽和高的最大尺寸
41. public static final int IMAGEMAXBIG = 2000;
42. // 图片宽和高的最小尺寸
43. public static final int IMAGEMINBIG = 10;
44. // 按原图大小生成新图
45. public static final int CREATENEWIMAGETYPE_0 = 0;
46. // 按指定的大小生成新图
47. public static final int CREATENEWIMAGETYPE_1 = 1;
48. // 按原图宽高比例生成新图-按指定的宽度
49. public static final int CREATENEWIMAGETYPE_2 = 2;
50. // 按原图宽高比例生成新图-按指定的高度
51. public static final int CREATENEWIMAGETYPE_3 = 3;
52. // 按原图宽高比例生成新图-按指定的宽和高中较大的尺寸
53. public static final int CREATENEWIMAGETYPE_4 = 4;
54. // 按原图宽高比例生成新图-按指定的宽和高中较小的尺寸
55. public static final int CREATENEWIMAGETYPE_5 = 5;
56.
57. /**
58. *
59. * @param _file
60. * 原图片
61. * @param createType
62. * 处理类型
63. * @param newW
64. * 新宽度
65. * @param newH
66. * 新高度
67. * @return
68. * @throws Exception
69. */
70. public static String createNewImage(File _file, int createType, int newW,
71. int newH) throws Exception {
72. if (_file == null)
73. return null;
74. String fileName = _file.getPath();
75. if (fileName == null || "".equals(fileName)
76. || fileName.lastIndexOf(".") == -1)
77. return null;
78. String newFileName = "_NEW";
79. /*
80. * else newFileName = "_" + newFileName;
81. */
82.
83. String outFileName = fileName.substring(0, fileName.lastIndexOf("."))
84. + newFileName
85. + fileName.substring(fileName.lastIndexOf("."), fileName
86. .length());
87. String fileExtName = fileName.substring(
88. (fileName.lastIndexOf(".") + 1), fileName.length());
89. if (newW < IMAGEMINBIG)
90. newW = IMAGEMINBIG;
91. else if (newW > IMAGEMAXBIG)
92. newW = IMAGEMAXBIG;
93.
94. if (newH < IMAGEMINBIG)
95. newH = IMAGEMINBIG;
96. else if (newH > IMAGEMAXBIG)
97. newH = IMAGEMAXBIG;
98.
99. // 得到原图信息
100. if (!_file.exists() || !_file.isAbsolute() || !_file.isFile()
101. || !checkImageFile(fileExtName))
102. return null;
103. if ((new File(outFileName)).exists()) {
104. System.out.println("file [" + outFileName + "] already exists");
105. throw new Exception();
106. }
107. Image src = ImageIO.read(_file);
108. int w = src.getWidth(null);
109. int h = src.getHeight(null);
110.
111. // 确定目标图片的大小
112. int nw = w;
113. int nh = h;
114. if (createType == CREATENEWIMAGETYPE_0)
115. ;
116. else if (createType == CREATENEWIMAGETYPE_1) {
117. nw = newW;
118. nh = newH;
119. } else if (createType == CREATENEWIMAGETYPE_2) {
120. nw = newW;
121. nh = (int) ((double) h / (double) w * nw);
122. } else if (createType == CREATENEWIMAGETYPE_3) {
123. nh = newH;
124. nw = (int) ((double) w / (double) h * nh);
125. } else if (createType == CREATENEWIMAGETYPE_4) {
126. if ((double) w / (double) h >= (double) newW / (double) newH) {
127. nh = newH;
128. nw = (int) ((double) w / (double) h * nh);
129. } else {
130. nw = newW;
131. nh = (int) ((double) h / (double) w * nw);
132. }
133. } else if (createType == CREATENEWIMAGETYPE_5) {
134. if ((double) w / (double) h <= (double) newW / (double) newH) {
135. nh = newH;
136. nw = (int) ((double) w / (double) h * nh);
137. } else {
138. nw = newW;
139. nh = (int) ((double) h / (double) w * nw);
140. }
141. }
142.
143. // 构造目标图片
144. BufferedImage tag = new BufferedImage(nw, nh,
145. BufferedImage.TYPE_INT_RGB);
146.
147. // 得到目标图片输出流
148. FileOutputStream out = new FileOutputStream(outFileName);
149.
150. // 根据需求画出目标图片 方式1
151. tag.getGraphics().drawImage(src, 0, 0, nw, nh, null);
152.
153. // 将画好的目标图输出到输出流 方式1
154. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
155. encoder.encode(tag);
156. out.close();
157. return outFileName;
158. }
159.
160. public static boolean checkImageFile(String extName) {
161.
162. if ("jpg".equalsIgnoreCase(extName))
163. return true;
164. if ("gif".equalsIgnoreCase(extName))
165. return true;
166. if ("bmp".equalsIgnoreCase(extName))
167. return true;
168. if ("jpeg".equalsIgnoreCase(extName))
169. return true;
170. if ("png".equalsIgnoreCase(extName))
171. return true;
172. return false;
173. }
174.
175.} </pre>
176.<p><br>
177.</p>
178.<p><br>
179.</p>
180.<p><br>
181.</p>
Java改变图片的大小
最新推荐文章于 2024-04-22 10:53:47 发布