java 图片太大了,Java改变图片的大小

01.

/**

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.}

24.

25.

 
 

26.

 
 
 
 
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.}

176.

177.

178.

179.

180.

181.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值