猿问

如何正确调整图像大小?

我在获取正确尺寸的图像时遇到问题。


为此,我有这种方法来调整图像的大小,不知何故逻辑需要一个小的改变来获得正确的图像大小:


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);

            }

所以每张图片都显示得比她在下图中的空间小,你可以看到有很多空白没有被使用,这绝对意味着我在我的方法中做错了什么导致了这个,我已经检查了很多次这种方法,但我真的不知道为什么会发生这种情况,如果需要的话,任何人都可以帮助我编辑这种方法我真的很感激。图像显示如下:

斯蒂芬大帝
浏览 196回答 3
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) {&nbsp; &nbsp; 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);如您所见,我扭转了两件事,以保持比例缩放。在心理上使用比率 ( /) 而不是比例因子 ( *) 似乎更难。分别确定宽度和高度的缩放比例让我们选择最小缩放比例。一个人也可以决定不放大小图片。

茅侃侃

您只考虑方向(ratio< 1 表示垂直,否则为水平或正方形)。这还不够;您必须考虑目标宽度/高度:&nbsp; &nbsp; &nbsp; &nbsp; int sw = originalImage.getWidth();&nbsp; &nbsp; &nbsp; &nbsp; int sh = originalImage.getHeight();&nbsp; &nbsp; &nbsp; &nbsp; int swdh = sw * maxFeatImageHeight;&nbsp; &nbsp; &nbsp; &nbsp; int shdw = sh * maxFeatImageWidth;&nbsp; &nbsp; &nbsp; &nbsp; if (swdh < shdw) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finalWidth = swdh / sh;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finalHeight = maxFeatImageHeight;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finalWidth = maxFeatImageWidth;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finalHeight = shdw / sw;&nbsp; &nbsp; &nbsp; &nbsp; }更新:好的,让我们从天平开始:&nbsp; &nbsp; &nbsp; &nbsp; double xScale = maxFeatImageWidth/featImageWidth;&nbsp; &nbsp; &nbsp; &nbsp; 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。

慕神8447489

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

相关分类

Java
我要回答