【问题标题】:Java-AffineTransform API:Scale an image to maximum of 300*100 pixels with?Java-AffineTransform API:将图像缩放到最大 300*100 像素?
【发布时间】:2011-03-31 16:20:52
【问题描述】:

我知道做简单的缩放,但是如何将图像缩放到最大 300*100 像素?我搜索了 AffineTransform API 类,但找不到此方法。

提前谢谢

【问题讨论】:

    标签: java 2d affinetransform


    【解决方案1】:

    我认为您需要通过确定什么比例可以使您的高度达到 300 像素和宽度达到 100 来“回到”您的比例因子。

    有点像

    double xScale = 100/image.getWidth();
    double yScale = 300/image.getHeight();
    
    AffineTransform transform = new AffineTransform();
    transform.setToScale( xScale, yScale );
    //paint code goes here
    

    但这不会均匀地缩放它。为此,您需要对 x 和 y 使用相同的比例。因此,如果您试图确保您的图像适合屏幕/显示器/页面,您将采用最宽尺寸的比例。 所以使用上面的例子......

    double xScale = 100/image.getWidth();
    double yScale = 300/image.getHeight();
    double theScale = xScale > yScale ? xScale : yScale;
    
    AffineTransform transform = new AffineTransform();
    transform.setToScale( theScale , theScale );
    //paint code goes here
    

    这只是一个建议,希望对您有所帮助。

    【讨论】:

    • 非常感谢...我也使用AffineTransformOp过滤图像..thx
    【解决方案2】:

    根据相对于目标宽度/高度的图像宽度/高度计算变换因子。

    编写一些代码,如果遇到问题,请提出具体问题。

    【讨论】:

      猜你喜欢
      • 2022-11-18
      • 1970-01-01
      • 2018-09-16
      • 2013-07-30
      • 1970-01-01
      • 1970-01-01
      • 2012-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多