当前位置: 首页 > 知识库问答 >
问题:

Java文档图像缩放和旋转

盖诚
2023-03-14

我有不同维度的文档图像,我希望能够有效地缩放和旋转他们在以下方式(标准的“旋转”和“缩放”逻辑)。我怎么做?

一幅图像是H像素高,W像素宽。最初,它应该缩放到600像素宽。在每次旋转时,面板的宽度和高度应该互换,缩放图像应该旋转90度。在每次缩放时,图像应按因子“比例”缩放。

以下是我到目前为止在BufferedImage...得到的BufferedImage会缩放和旋转,但不会平移(旋转90度后位于面板顶部的中心):

double scale = zoom * 600.0 / img.getWidth();
rotation = (rotation + degrees) % 360;
int scaledWidth = (int)(scale * img.getWidth());
int scaledHeight = (int)(scale * img.getHeight());
BufferedImage bufferedImage = new BufferedImage(scaledWidth, scaledHeight, img.getType());
if (rotation % 180 == 0)
    bufferedImage = new BufferedImage(scaledWidth, scaledHeight, img.getType());
else
    bufferedImage = new BufferedImage(scaledHeight, scaledWidth, img.getType());

AffineTransform transform = AffineTransform.getRotateInstance(Math.toRadians(rotation), scaledWidth/2, scaledHeight/2);
transform.scale(scale, scale);
AffineTransformOp operation = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
scaledImage = operation.filter(img, bufferedImage);
imagePanel.setPreferredSize(new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight()));

共有1个答案

胡元忠
2023-03-14

啊哈!关键(JavaDoc很混乱)在于意识到在AffineTransformRotate()和其他方法上转换的是矩阵,而不是图像!下面的代码是自动工作的!

/**
 * Transforms the image efficiently without losing image quality.
 * Scales the image to a width of (600 * scale) pixels, rotates the image,
 * and translates (moves) the image to recenter it if rotated 90 or 270 degrees.
 */
protected BufferedImage transformImage(BufferedImage image)
{
    int scaledWidth = (int)(scale * image.getWidth());
    int scaledHeight = (int)(scale * image.getHeight());

    // Methods AffineTransform.rotate(), AffineTransform.scale() and AffineTransform.translate()
    // transform AffineTransform's transformation matrix to multiply with the buffered image.
    // Therefore those methods are called in a counterintuitive sequence.
    AffineTransform transform;
    if (rotation % 180 == 0)
    {
        // First scale and second rotate image
        transform = AffineTransform.getRotateInstance(Math.toRadians(rotation), scaledWidth/2, scaledHeight/2);
        transform.scale(scale, scale);
    }
    else
    {
        // First scale, second rotate, and third translate image
        transform = AffineTransform.getTranslateInstance((scaledHeight-scaledWidth)/2, (scaledWidth-scaledHeight)/2);
        transform.rotate(Math.toRadians(rotation), scaledWidth/2, scaledHeight/2);
        transform.scale(scale, scale);
    }
    AffineTransformOp operation = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);
    BufferedImage transformedImage = operation.createCompatibleDestImage(image, image.getColorModel());
    return operation.filter(image, transformedImage);
}
 类似资料:
  • 选择要变换的文字 您可以像对其他对象一样,对文字进行旋转、镜像、比例缩放和倾斜。但是,您选择文字的方式会影响变换结果: 要变换文字及其边框路径,请选择文字对象,然后使用旋转工具旋转对象和文本。 要只变换边框路径(而不变换它所包含的文字),请使用选择工具选择文字对象并进行拖移。 旋转文字路径(左图)与旋转文字和路径(右图)的对比图 另请参阅 第 197 页的 “变换对象 ” 调整文字缩放比例 您可以

  • 我有一个被JScrollPane包围的JPanel。该JPanel用于显示图像。我需要提供zoomIn、zoomOut、顺时针旋转和逆时针旋转等功能。所有这些功能单独运行良好。对于缩放,我称之为图形对象的缩放。它发生在从左上到右下的基础上。对于旋转,我重置比例,然后平移、旋转 因此,问题是当面板坐标更改时,图像的坐标不会改变。有人可以帮助我更改图像的坐标吗?

  • 实际上,我正在用JavaFX编写游戏《暗黑破坏神》的“简单”版本。 目前,屏幕尺寸为绝对800x600,一切正常。现在我们的团队希望在每个桌面上使用全屏。 我的问题是:我们正在使用绝对尺寸为25x25的墙、地板等图像。如果我们想在全屏模式下工作,我们需要扩展它们。 但据我所知,您只能缩放ImageView或BufferedImage。当然,我们正在重复使用图像。例如,一个墙壁纹理在窗格上出现了20

  • 问题内容: 我使用Java中的Graphics2D缩放和旋转绘制的图片。现在,我想知道当单击图片中的某个点时的原始坐标是什么。因此,鉴于旋转坐标和缩放坐标,我想计算原始坐标。有没有简单的方法可以做到这一点? 问题答案: 如果保留在绘制图像时使用的副本,则可以使用 AffineTransform.inverseTransform(Point2D ptSrc,Point2D ptDst) 将设备空间坐

  • 问题内容: 我需要能够单独旋转图像(在Java中)。到目前为止,我发现的唯一东西是g2d.drawImage(image,affinetransform,ImageObserver)。不幸的是,我需要在特定点绘制图像,并且没有一种方法带有参数1.分别旋转图像和2.允许我设置x和y。任何帮助表示赞赏 问题答案: 这就是你可以做到的。这段代码假设存在一个名为“ image”的缓冲图像(如你的评论所说)