Java - 图像旋转 [英] Java - Image Rotation

查看:22
本文介绍了Java - 图像旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试旋转图像.我正在使用此 Java 代码:

I am trying to rotate image. I am using this Java code:

BufferedImage oldImage = ImageIO.read(new FileInputStream("C:\workspace\test\src\10.JPG"));
BufferedImage newImage = new BufferedImage(oldImage.getHeight(), oldImage.getWidth(), oldImage.getType());
Graphics2D graphics = (Graphics2D) newImage.getGraphics();
graphics.rotate(Math.toRadians(90), newImage.getWidth() / 2, newImage.getHeight() / 2);
graphics.drawImage(oldImage, 0, 0, oldImage.getWidth(), oldImage.getHeight(), null);
ImageIO.write(newImage, "JPG", new FileOutputStream("C:\workspace\test\src\10_.JPG"));

但我看到奇怪的结果:

来源:

结果:

你能帮我解决这个问题吗?

Can you please help me with this problem?

推荐答案

切换图片的宽度和高度是不够的.您正在使用图像的中心作为旋转原点进行旋转.只需在一张纸上尝试相同的方法,您就会发现它的工作方式相同.您还必须稍微移动纸张,这意味着应用变换来解决此问题.因此,在轮换调用后立即执行以下操作:

It is not enough to switch the width and height of the image. You are rotating using the center of the image as the origin of rotation. Just try the same with a sheet of paper and you will see it works the same way. You must also move the paper a little bit, which means to apply a transform to fix this. So, immediately after the rotate call, do this:

  graphics.translate((newImage.getWidth() - oldImage.getWidth()) / 2, (newImage.getHeight() - oldImage.getHeight()) / 2);

这篇关于Java - 图像旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆