亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何正確調整圖像大???

如何正確調整圖像大???

斯蒂芬大帝 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 關注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號