Java中的旋转缓冲区映像
我一直在尝试使用for循环旋转图像。我的代码确实有效,但这种方法似乎没有必要,而且图像在旋转时会丢失像素。有没有更简单的方法Java中的旋转缓冲区映像,java,for-loop,methods,graphics,bufferedimage,Java,For Loop,Methods,Graphics,Bufferedimage,我一直在尝试使用for循环旋转图像。我的代码确实有效,但这种方法似乎没有必要,而且图像在旋转时会丢失像素。有没有更简单的方法 //rotates image around center a degrees public void drawRotatedImage(Graphics g, BufferedImage image, double a, int x, int y, int pixelSize) { //origin point for rotation int rX =
//rotates image around center a degrees
public void drawRotatedImage(Graphics g, BufferedImage image, double a, int x, int y, int pixelSize) {
//origin point for rotation
int rX = image.getWidth() / 2;
int rY = image.getHeight() / 2;
for(int x1 = 0; x1 < image.getWidth(); x1++) {
for(int y1 = 0; y1 < image.getHeight(); y1++) {
int c = image.getRGB(x1, y1);
//rotating point
int nX = (int) (((x1-rX) * Math.cos(a)) - ((y1-rY) * Math.sin(a)));
int nY = (int) (((x1-rX) * Math.sin(a)) + ((y1-rY) * Math.cos(a)));
g.setColor(new Color(c));
//drawing each pixel
g.fillRect((nX*pixelSize) + x, (nY*pixelSize) + y, pixelSize, pixelSize);
}
}
}
我创建了一个简单的示例程序,改编自 尝试使用仿射变换 获取一个旋转的实例 或者直接画出来
static void rotate(Graphics2d g2d, BufferedImage img, double rotate)
{
// rotate around the center
AffineTransform trans
= AffineTransform.getRotateInstance(rotate,
from.getWidth()/2, from.getHeight()/2);
g2d.drawImage(img, trans, null);
}
这实际上并不是旋转图像,这似乎是问题的jist,而是旋转用于绘制图像的图形上下文,而不是重复已经说过的内容,如果问题实际上是重复的,它应该标记为可能的重复。这不使用像素操作,而是使用仿射变换-这个例子和其他答案之间的区别?它将调整图像大小,使内容适合,这似乎是您缺少/正在寻找的一件事,但是-您需要调整目标图像的大小,以便旋转的源图像适合,而无需剪裁原始图像
static void rotate(BufferedImage from, BufferedImage to, double rotate)
{
// rotate around the center
AffineTransform trans
= AffineTransform.getRotateInstance(rotate,
from.getWidth()/2, from.getHeight()/2);
AffineTransformOp op = new AffineTransformOp(trans,
AffineTransformOp.TYPE_BILINEAR);
op.filter(from, to);
}
static void rotate(Graphics2d g2d, BufferedImage img, double rotate)
{
// rotate around the center
AffineTransform trans
= AffineTransform.getRotateInstance(rotate,
from.getWidth()/2, from.getHeight()/2);
g2d.drawImage(img, trans, null);
}