質問編集履歴
3
変更点追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,71 +14,77 @@
|
|
14
14
|
|
15
15
|
```java
|
16
16
|
|
17
|
-
public static BufferedImage resizeImage(BufferedImage
|
17
|
+
public static BufferedImage resizeImage(BufferedImage srcImage, int resizeWidth,
|
18
18
|
|
19
19
|
int resizeHeight,int resizeDpi) {
|
20
20
|
|
21
21
|
|
22
22
|
|
23
|
-
|
23
|
+
int w = resizeWidth;
|
24
24
|
|
25
|
-
int
|
25
|
+
int h = resizeHeight;
|
26
26
|
|
27
27
|
|
28
28
|
|
29
|
-
|
29
|
+
int imageWidth = srcImage.getWidth();
|
30
30
|
|
31
|
-
|
31
|
+
int imageHeight = srcImage.getHeight();
|
32
32
|
|
33
33
|
|
34
34
|
|
35
|
-
i
|
35
|
+
if ((imageWidth > imageHeight) && (resizeWidth < resizeHeight)) {
|
36
36
|
|
37
|
-
i
|
37
|
+
w = resizeHeight;
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
if (rateRsize == rateImg) {
|
42
|
-
|
43
|
-
|
39
|
+
h = resizeWidth;
|
44
|
-
|
45
|
-
h = resizeHeight;
|
46
|
-
|
47
|
-
}else if (rateRsize > rateImg) {
|
48
|
-
|
49
|
-
w = (int) (resizeHeight * rateImg);
|
50
|
-
|
51
|
-
h = resizeHeight;
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
}else{
|
56
|
-
|
57
|
-
w = resizeWidth;
|
58
|
-
|
59
|
-
h = (int) (resizeWidth / rateImg);
|
60
40
|
|
61
41
|
}
|
62
42
|
|
63
43
|
|
64
44
|
|
65
|
-
|
45
|
+
double sx = (double) w / imageWidth;
|
66
46
|
|
67
|
-
ImageProducer p = new FilteredImageSource(bufferedImage.getSource(), filter);
|
68
|
-
|
69
|
-
java.awt.Image dstImage = Toolkit.getDefaultToolkit().createImage(p);
|
70
|
-
|
71
|
-
|
47
|
+
double sy = (double) h / imageHeight;
|
72
|
-
|
73
|
-
Graphics2D g = dst.createGraphics();
|
74
|
-
|
75
|
-
g.drawImage(dstImage, 0, 0, null);
|
76
|
-
|
77
|
-
g.dispose();
|
78
48
|
|
79
49
|
|
80
50
|
|
51
|
+
AffineTransform trans = AffineTransform.getScaleInstance(sx, sy);
|
52
|
+
|
53
|
+
if (sx == sy) {
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
}else if (sx > sy) {
|
58
|
+
|
59
|
+
w = (int) (sy * imageWidth);
|
60
|
+
|
61
|
+
trans = AffineTransform.getScaleInstance(sy, sy);
|
62
|
+
|
63
|
+
}else{
|
64
|
+
|
65
|
+
h = (int) (sx * imageHeight);
|
66
|
+
|
67
|
+
trans = AffineTransform.getScaleInstance(sx, sx);
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
BufferedImage dstImage = new BufferedImage(w, h, srcImage.getType());
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
Graphics2D g2 = (Graphics2D)dstImage.createGraphics();
|
78
|
+
|
79
|
+
g2.drawImage(srcImage, trans, null);
|
80
|
+
|
81
|
+
g2.dispose();
|
82
|
+
|
83
|
+
|
84
|
+
|
81
|
-
|
85
|
+
return dstImage;
|
86
|
+
|
87
|
+
|
82
88
|
|
83
89
|
}
|
84
90
|
|
2
内容追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -7,6 +7,8 @@
|
|
7
7
|
|
8
8
|
|
9
9
|
DPIはどう設定しますか?
|
10
|
+
|
11
|
+
サイズとdpiを別々に変更するではなく、一回だけリサイズできないでしょうか?
|
10
12
|
|
11
13
|
|
12
14
|
|
1
内容追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -3,6 +3,10 @@
|
|
3
3
|
既存のbufferedImageより、指定width、height、dpiのbufferedImageを取得したいですが、
|
4
4
|
|
5
5
|
何かいい方法がありますでしょうか?
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
DPIはどう設定しますか?
|
6
10
|
|
7
11
|
|
8
12
|
|
@@ -14,7 +18,65 @@
|
|
14
18
|
|
15
19
|
|
16
20
|
|
17
|
-
|
21
|
+
int imageWidth = bufferedImage.getWidth();
|
22
|
+
|
23
|
+
int imageHeight = bufferedImage.getHeight();
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
double rateImg = imageWidth/imageHeight;
|
28
|
+
|
29
|
+
double rateRsize = resizeWidth/resizeHeight;
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
int w;
|
34
|
+
|
35
|
+
int h;
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
if (rateRsize == rateImg) {
|
40
|
+
|
41
|
+
w = resizeWidth;
|
42
|
+
|
43
|
+
h = resizeHeight;
|
44
|
+
|
45
|
+
}else if (rateRsize > rateImg) {
|
46
|
+
|
47
|
+
w = (int) (resizeHeight * rateImg);
|
48
|
+
|
49
|
+
h = resizeHeight;
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
}else{
|
54
|
+
|
55
|
+
w = resizeWidth;
|
56
|
+
|
57
|
+
h = (int) (resizeWidth / rateImg);
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
ImageFilter filter = new AreaAveragingScaleFilter(w, h);
|
64
|
+
|
65
|
+
ImageProducer p = new FilteredImageSource(bufferedImage.getSource(), filter);
|
66
|
+
|
67
|
+
java.awt.Image dstImage = Toolkit.getDefaultToolkit().createImage(p);
|
68
|
+
|
69
|
+
BufferedImage dst = new BufferedImage(w, h, bufferedImage.getType());
|
70
|
+
|
71
|
+
Graphics2D g = dst.createGraphics();
|
72
|
+
|
73
|
+
g.drawImage(dstImage, 0, 0, null);
|
74
|
+
|
75
|
+
g.dispose();
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
return dst;
|
18
80
|
|
19
81
|
}
|
20
82
|
|