为了账号安全,请及时绑定邮箱和手机立即绑定

如何正确调整图像大小?

如何正确调整图像大小?

斯蒂芬大帝 2023-02-23 10:24:10
我在获取正确尺寸的图像时遇到问题。为此,我有这种方法来调整图像的大小,不知何故逻辑需要一个小的改变来获得正确的图像大小:private final Integer width = 170;private final Integer height = 140;private final Integer maxFeatImageHeight = 600;private final Integer maxFeatImageWidth = 600;   /** * @param featureImage . * @return byte[] * @throws Exception */private byte[] resizeFeatureImage(MultipartFile featureImage) throws Exception{    try    {        BufferedImage originalImage = ImageIO.read(featureImage.getInputStream());        ByteArrayOutputStream baos = new ByteArrayOutputStream();        double featImageWidth = originalImage.getWidth();        double featImageHeight = originalImage.getHeight();        if (featImageHeight > maxFeatImageHeight || featImageWidth > maxFeatImageWidth)        {         // Sanity check on the input (division by zero, infinity):            if (featImageWidth <= 1 || featImageHeight <= 1) {                throw new IllegalArgumentException("Error ..." + featureImage);            }            // The scaling factors to reach to maxima on width and height:            double xScale = maxFeatImageWidth   / featImageWidth;            double yScale = maxFeatImageHeight  / featImageHeight;            // Proportional (scale width and height by the same factor):            double scale = Math.min(xScale, yScale);            // (Possibly) Do not enlarge:            scale = Math.min(1.0, scale);            int finalWidth = Math.min((int) Math.round(scale * featImageWidth), maxFeatImageWidth);            int finalHeight = Math.min((int) Math.round(scale * featImageHeight), maxFeatImageHeight);            double ratio = featImageWidth / featImageHeight;            // width is bigger then height            if (ratio > 1)            {                finalWidth = maxFeatImageWidth;                finalHeight = (int) Math.round(maxFeatImageHeight / ratio);            }所以每张图片都显示得比她在下图中的空间小,你可以看到有很多空白没有被使用,这绝对意味着我在我的方法中做错了什么导致了这个,我已经检查了很多次这种方法,但我真的不知道为什么会发生这种情况,如果需要的话,任何人都可以帮助我编辑这种方法我真的很感激。图像显示如下:
查看完整描述

3 回答

?
蛊毒传说

TA贡献1895条经验 获得超3个赞

private static final int MAX_FEAT_IMAGE_WIDTH = 600;

private static final int MAX_FEAT_IMAGE_WIDTH = 600;


double featImageWidth = originalImage.getWidth();

double featImageHeight = originalImage.getHeight();


// Sanity check on the input (division by zero, infinity):

if (featImageWidth <= 1 || featImageHeight <= 1) {

    throw new IllegalArgumentException("..." + featureImage);

}


// The scaling factors to reach to maxima on width and height:

double xScale = MAX_FEAT_IMAGE_WIDTH / featImageWidth;

double yScale = MAX_FEAT_IMAGE_HEIGHT / featImageHeight;


// Proportional (scale width and height by the same factor):

double scale = Math.min(xScale, yScale);


// (Possibly) Do not enlarge:

scale = Math.min(1.0, scale);


int finalWidth = Math.min((int) Math.round(scale * featImageWidth), MAX_FEAT_IMAGE_WIDTH);

int finalHeight = Math.min((int) Math.round(scale * featImageHeigth), MAX_FEAT_IMAGE_HEIGHT);

如您所见,我扭转了两件事,以保持比例缩放。在心理上使用比率 ( /) 而不是比例因子 ( *) 似乎更难。


分别确定宽度和高度的缩放比例让我们选择最小缩放比例。


一个人也可以决定不放大小图片。


查看完整回答
反对 回复 2023-02-23
?
茅侃侃

TA贡献1842条经验 获得超21个赞

您只考虑方向(ratio< 1 表示垂直,否则为水平或正方形)。这还不够;您必须考虑目标宽度/高度:


        int sw = originalImage.getWidth();

        int sh = originalImage.getHeight();

        int swdh = sw * maxFeatImageHeight;

        int shdw = sh * maxFeatImageWidth;

        if (swdh < shdw) {

            finalWidth = swdh / sh;

            finalHeight = maxFeatImageHeight;

        } else {

            finalWidth = maxFeatImageWidth;

            finalHeight = shdw / sw;

        }

更新:


好的,让我们从天平开始:


        double xScale = maxFeatImageWidth/featImageWidth;

        double yScale = maxFeatImageHeight/featImageHeight;

你可以写:


在 yScale < xScale 的情况下,我们需要使用 yScale:


finalWidth = featImageWidth*yScale = featImageWidth*maxFeatImageHeight/featImageHeight;

finalHeight = maxFeatImageHeight;

否则,我们可以使用 xScale:


finalWidth = maxFeatImageWidth;

finalHeight = featImageHeight*xScale = featImageHeight*maxFeatImageWidth/featImageWidth;

由于所有宽度和高度都 > 0,因此 yScale < xScale 的结果与:


featImageWidth*featImageHeight*yScale < featImageWidth*featImageHeight*xScale

所以


featImageWidth*featImageHeight*maxFeatImageHeight/featImageHeight < featImageWidth*featImageHeight*maxFeatImageWidth/featImageWidth


maxFeatImageHeight*featImageWidth < maxFeatImageHeight*featImageWidth

我将这两个值保存为 swdh 和 shdw,因为它们可以在以后重复使用。


int它避免了从到double和从double到的转换int。


查看完整回答
反对 回复 2023-02-23
?
慕神8447489

TA贡献1780条经验 获得超1个赞

很可能你应该知道要调整大小的图像的大小,然后基于该值 if 和 else 语句应该起作用,然后调用调整大小函数你将能够调整它的大小。我希望这有帮助。并且当您调整大小时,请确保您也能够按照用户定义的方式减少像素。



查看完整回答
反对 回复 2023-02-23
  • 3 回答
  • 0 关注
  • 102 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信