我是靠谱客的博主 精明白猫,这篇文章主要介绍java 图片缩放和截取,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package test.PIC; import java.awt.Image; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Date; import java.util.Iterator; import javax.imageio.ImageIO; import javax.imageio.ImageReadParam; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class Test { /** * 截取图片 * * @param src * @param dest * @param w * @param h * @param format * @throws Exception */ public void readUsingImageReader(String src, int w, int h, String format) throws Exception { int outputWidth = 61; int outputHeight = 61; File f = new File(src); Image img = ImageIO.read(f); double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1; double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1; // 根据缩放比率小的进行缩放控制 double rate = rate1 > rate2 ? rate2 : rate1; int newWidth = (int) (((double) img.getWidth(null)) / rate); int newHeight = (int) (((double) img.getHeight(null)) / rate); BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB); /* * Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢 */ tag.getGraphics().drawImage( img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null); FileOutputStream out = new FileOutputStream(f); // JPEGImageEncoder可适用于其他图片类型的转换 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); // 取得图片读入器 Iterator readers = ImageIO.getImageReadersByFormatName(format); ImageReader reader = (ImageReader) readers.next(); // 取得图片读入流 InputStream source = new FileInputStream(f); ImageInputStream iis = ImageIO.createImageInputStream(source); reader.setInput(iis, true); // 图片参数 ImageReadParam param = reader.getDefaultReadParam(); // 默认截取从0,0 开始,可以自己定义设置 Rectangle rect = new Rectangle(0, 0, w, h); param.setSourceRegion(rect); BufferedImage bi = reader.read(0, param); ImageIO.write(bi, format, f); } public static void main(String[] args) throws Exception { Test t= new Test(); System.out.println(); long time = new Date().getTime(); t.readUsingImageReader("d:\out\test01.jpg", 60, 60, "jpg"); System.out.println("用时:"+(new Date().getTime()-time)+"毫秒"); } }
复制代码
1
2
复制代码
1
2
复制代码
1
2
复制代码
1
2
复制代码
1
2
复制代码
1
2
复制代码
1
2
复制代码
1
2
复制代码
1
2
复制代码
1
2
复制代码
1
2
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<pre name="code" class="java">/** * 缩略图实现,将图片(jpg、bmp、png、gif等等)真实的变成想要的大小 */ import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import javax.imageio.ImageIO; import javax.imageio.ImageReadParam; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; import java.awt.Rectangle; /******************************************************************************* * 缩略图类(通用) 本java类能将jpg、bmp、png、gif图片文件,进行等比或非等比的大小转换。 具体使用方法 * compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true)) * 图片截取 readUsingImageReader */ public class CompressPicDemo { private File file = null; // 文件对象 private String inputDir; // 输入图路径 private String outputDir; // 输出图路径 private String inputFileName; // 输入图文件名 private String outputFileName; // 输出图文件名 private int outputWidth = 120; // 默认输出图片宽 private int outputHeight = 120; // 默认输出图片高 private boolean proportion = true; // 是否等比缩放标记(默认为等比缩放) public CompressPicDemo() { // 初始化变量 inputDir = ""; outputDir = ""; inputFileName = ""; outputFileName = ""; outputWidth = 120; outputHeight = 120; } public void setInputDir(String inputDir) { this.inputDir = inputDir; } public void setOutputDir(String outputDir) { this.outputDir = outputDir; } public void setInputFileName(String inputFileName) { this.inputFileName = inputFileName; } public void setOutputFileName(String outputFileName) { this.outputFileName = outputFileName; } public void setOutputWidth(int outputWidth) { this.outputWidth = outputWidth; } public void setOutputHeight(int outputHeight) { this.outputHeight = outputHeight; } public void setWidthAndHeight(int width, int height) { this.outputWidth = width; this.outputHeight = height; } /* * 获得图片大小 传入参数 String path :图片路径 */ public long getPicSize(String path) { file = new File(path); return file.length(); } // 图片处理 public String compressPic() { try { // 获得源文件 file = new File(inputDir + inputFileName); if (!file.exists()) { return ""; } Image img = ImageIO.read(file); // 判断图片格式是否正确 if (img.getWidth(null) == -1) { System.out.println(" can't read,retry!" + "<BR>"); return "no"; } else { int newWidth; int newHeight; // 判断是否是等比缩放 if (this.proportion == true) { // 为等比缩放计算输出的图片宽度及高度 double rate1 = ((double) img.getWidth(null)) / (double) outputWidth + 0.1; double rate2 = ((double) img.getHeight(null)) / (double) outputHeight + 0.1; // 根据缩放比率大的进行缩放控制 double rate = rate1 > rate2 ? rate1 : rate2; newWidth = (int) (((double) img.getWidth(null)) / rate); newHeight = (int) (((double) img.getHeight(null)) / rate); } else { newWidth = outputWidth; // 输出的图片宽度 newHeight = outputHeight; // 输出的图片高度 } BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB); /* * Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢 */ tag.getGraphics().drawImage( img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null); FileOutputStream out = new FileOutputStream(outputDir + outputFileName); // JPEGImageEncoder可适用于其他图片类型的转换 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.close(); } } catch (IOException ex) { ex.printStackTrace(); } return "ok"; } public String compressPic(String inputDir, String outputDir, String inputFileName, String outputFileName) { // 输入图路径 this.inputDir = inputDir; // 输出图路径 this.outputDir = outputDir; // 输入图文件名 this.inputFileName = inputFileName; // 输出图文件名 this.outputFileName = outputFileName; return compressPic(); } public String compressPic(String inputDir, String outputDir, String inputFileName, String outputFileName, int width, int height, boolean gp) { // 输入图路径 this.inputDir = inputDir; // 输出图路径 this.outputDir = outputDir; // 输入图文件名 this.inputFileName = inputFileName; // 输出图文件名 this.outputFileName = outputFileName; // 设置图片长宽 setWidthAndHeight(width, height); // 是否是等比缩放 标记 this.proportion = gp; return compressPic(); } /** * 截取图片 * @param src * @param dest * @param w * @param h * @param format * @throws Exception */ public void readUsingImageReader(String src, String dest, int w, int h,String format) throws Exception { // 取得图片读入器 Iterator readers = ImageIO.getImageReadersByFormatName(format); ImageReader reader = (ImageReader) readers.next(); // 取得图片读入流 InputStream source = new FileInputStream(src); ImageInputStream iis = ImageIO.createImageInputStream(source); reader.setInput(iis, true); // 图片参数 ImageReadParam param = reader.getDefaultReadParam(); //默认截取从0,0 开始,可以自己定义设置 Rectangle rect = new Rectangle(0, 0, w, h); param.setSourceRegion(rect); BufferedImage bi = reader.read(0, param); ImageIO.write(bi, format, new File(dest)); } // main测试 // compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true)) public static void main(String[] arg) { CompressPicDemo mypic = new CompressPicDemo(); System.out.println("输入的图片大小:" + mypic.getPicSize("e:\1.jpg") / 1024 + "KB"); int count = 0; // 记录全部图片压缩所用时间 File f = new File("D:\PIC\1\"); File[] files = f.listFiles(); int i = 1; for (File file : files) { int start = (int) System.currentTimeMillis(); // 开始时间 mypic.compressPic("D:\PIC\1\", "D:\PIC\2\", file.getName(), file.getName(), mypic.outputHeight, mypic.outputWidth, true); int end = (int) System.currentTimeMillis(); // 结束时间 int re = end - start; // 但图片生成处理时间 count += re; System.out.println("第" + (i + 1) + "张图片压缩处理使用了: " + re + "毫秒"); i++; } System.out.println("总共用了:" + count + "毫秒"); CompressPicDemo t = new CompressPicDemo(); try { t.readUsingImageReader("d://test01.png", "d://3.png", 60, 60,"png"); } catch (Exception e) { e.printStackTrace(); } } }


复制代码
1


最后

以上就是精明白猫最近收集整理的关于java 图片缩放和截取的全部内容,更多相关java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(80)

评论列表共有 0 条评论

立即
投稿
返回
顶部