java实现image图片旋转

private static BufferedImage rotateImage(BufferedImage image, int degree)
throws IOException {


BufferedImage rotatedImage = null;
try {
// 元イメージ広い
int originalImWidth = image.getWidth();
// 元イメージ高い
int originalImHeight = image.getHeight();


// 回転広い
int rotateImWidth = 0;
// 回転高い
int rotateImHeight = 0;


// ドットX
int x = 0;
// ドットY
int y = 0;


// 角度を計算する
degree = degree % 360;
if (degree < 0) {
degree = 360 + degree;
}


// 角度を弧度に変換する
double radian = Math.toRadians(degree);


// 角度が 0 または 180 または360
if (degree == 180 || degree == 0 || degree == 360) {
rotateImWidth = originalImWidth;
rotateImHeight = originalImHeight;


// 角度が 90 または 270
} else if (degree == 90 || degree == 270) {
rotateImWidth = originalImHeight;
rotateImHeight = originalImWidth;


// 上記以外
} else {
double cosVal = Math.abs(Math.cos(radian));
double sinVal = Math.abs(Math.sin(radian));
rotateImWidth = (int)(sinVal * originalImHeight) + (int)(cosVal * originalImWidth);
rotateImHeight = (int)(sinVal * originalImWidth) + (int)(cosVal * originalImHeight);
}


// ドット座標を計算する
x = (rotateImWidth / 2) - (originalImWidth / 2);
y = (rotateImHeight / 2) - (originalImHeight / 2);


// 回転イメージを作成する
rotatedImage = new BufferedImage(rotateImWidth, rotateImHeight, image.getType());
Graphics2D gs = (Graphics2D) rotatedImage.getGraphics();
gs.fillRect(0, 0, rotateImWidth, rotateImHeight);


// イメージを回転する
AffineTransform at = new AffineTransform();
at.rotate(radian, rotateImWidth / 2, rotateImHeight / 2);
at.translate(x, y);
AffineTransformOp op = new AffineTransformOp(at,
AffineTransformOp.TYPE_BICUBIC);
op.filter(image, rotatedImage);
} catch (Exception e) {
F2Logger.getLogger().log(PhotoConstants.EFUP0002, new Object[] { e.getMessage() });
throw e;
}
return rotatedImage;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值