回答編集履歴

1

各角度の回転処理を追記

2016/07/26 07:51

投稿

Yossi_1982
Yossi_1982

スコア95

test CHANGED
@@ -16,33 +16,57 @@
16
16
 
17
17
  ```
18
18
 
19
- try {
19
+ private byte[] rotateImage(String filePath) {
20
20
 
21
- BufferedImage inBuff = ImageIO.read(new File("ファイルパス"));
21
+ try {
22
22
 
23
- AffineTransform affine = new AffineTransform();
23
+ BufferedImage inBuff = ImageIO.read(new File(filePath));
24
24
 
25
- //Math.toRadiansにDegressを指定する
25
+ AffineTransform affine = new AffineTransform();
26
26
 
27
- affine.rotate(Math.toRadians(90), inBuff.getHeight()/2, inBuff.getHeight()/2);
27
+ //90度のときの回転処理
28
28
 
29
- AffineTransformOp op = new AffineTransformOp(affine, AffineTransformOp.TYPE_BICUBIC);
29
+ affine.setToRotation(Math.toRadians(90), inBuff.getHeight()/2, inBuff.getHeight()/2);
30
30
 
31
- BufferedImage outBuff = new BufferedImage(inBuff.getHeight(), inBuff.getWidth(), inBuff.getType());
31
+ //180度のときの回転処理
32
32
 
33
- op.filter(inBuff, outBuff);
33
+ //affine.setToRotation(Math.toRadians(180), inBuff.getWidth()/2, inBuff.getHeight()/2);
34
34
 
35
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
35
+ //270度のときの回転処理
36
36
 
37
- ImageIO.write(outBuff, "png", baos);
37
+ //affine.rotate(Math.toRadians(270), inBuff.getWidth()/2, inBuff.getWidth()/2);
38
38
 
39
- byte[] byteImg = baos.toByteArray()
40
39
 
41
- } catch (IOException e) {
42
40
 
43
- e.printStackTrace();
41
+ AffineTransformOp op = new AffineTransformOp(affine, AffineTransformOp.TYPE_BICUBIC);
44
42
 
43
+ //180度のときのアウトプット画像バッファ
44
+
45
+ //BufferedImage outBuff = new BufferedImage(inBuff.getWidth(), inBuff.getHeight(), inBuff.getType());
46
+
47
+ //90度、270度のときのアウトプット画像バッファ
48
+
49
+ BufferedImage outBuff = new BufferedImage(inBuff.getHeight(), inBuff.getWidth(), inBuff.getType());
50
+
51
+ op.filter(inBuff, outBuff);
52
+
53
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
54
+
55
+ ImageIO.write(outBuff, "jpg", baos);
56
+
57
+ return baos.toByteArray();
58
+
59
+ } catch (IOException e) {
60
+
61
+ e.printStackTrace();
62
+
45
- }
63
+ }
64
+
65
+ return null;
66
+
67
+ }
68
+
69
+
46
70
 
47
71
  ```
48
72
 
@@ -50,4 +74,12 @@
50
74
 
51
75
 
52
76
 
77
+ この返却値をBase64に変換してはどうでしょうか?
78
+
79
+
80
+
81
+ このプログラムは90度の回転です。
82
+
53
- ただし、こちらのコードでは180度回転する画像がおかしくなるのでそこはえてください。
83
+ 180度、270度のとこコメント切り替えてください。
84
+
85
+ 動的に変更したければ、switch文などでできるよう変更する必要あります。