Re-sizing an image without losing quality

前端 未结 9 1424
温柔的废话
温柔的废话 2020-11-29 01:45

I made this code to resize images with two factors. It works, but the quality of image is very bad after it is resized! Can you help me?

This is the code

<         


        
9条回答
  •  情歌与酒
    2020-11-29 02:11

    The do while loop used in The Perils of Image.getScaledInstance() will run into an infinite loop, given those values, w = 606; h = 505, targetWidth = 677, targetHeight = 505

    Here is a simplied testing code, you can try it.

    public class LoopTest {
    
        public static void main(String[] args) {
            new LoopTest(true, 606, 505, 677, 505);
        }
    
        public LoopTest(boolean higherQuality, int w, int h, int targetWidth, int targetHeight) {
            do {
                if (higherQuality && w > targetWidth) {
                    w /= 2;
                    if (w < targetWidth) {
                        w = targetWidth;
                    }
                }
    
                if (higherQuality && h > targetHeight) {
                    h /= 2;
                    if (h < targetHeight) {
                        h = targetHeight;
                    }
                }
            } while (w != targetWidth || h != targetHeight);        // TODO Auto-generated constructor stub
        }
    }
    

    A quick work around: define an index for loop count. If the index is >=10, break out of loop.

提交回复
热议问题