# Image Compression in java

search in google for a few hours to find a good solution to compress the image using in java. This is the best mentioned:


So here i compress two libraried I used in my project ,one is from Hutool's ImgUtil.compress(testFile, outputFile, 0.5f); static method ,one is from thumbnailator, but you should know that this library had not maintenace for a long time. The code i used for the performance testing here:

        String file="D:\\yEJnna3mCMK8.jpg";
        File testFile = new File(file);
        File outputFile=new File("D:\\test1.jpg");
        File outputFile2=new File("D:\\test2.jpg");
        int count=0;
        for (int i = 0; i < 300; i++) {
            long startTime = System.currentTimeMillis();
            ImgUtil.compress(testFile, outputFile, 0.5f);
            long lostTime1 = System.currentTimeMillis() - startTime;
            System.out.println("take time1 from hutool =" + Long.toString(lostTime1));
            long startTime2 = System.currentTimeMillis();
            ImageUtils.compressImage(testFile, outputFile2, "");
            long lostTime2 = System.currentTimeMillis() - startTime2;
            System.out.println("take time2 from thumbnail =" + Long.toString(lostTime2));

        System.out.println("hutool better than thumnail count is: "+count);

At last found hutool is faster than thumbnailator, so take to use it then. over...

最后更新: 5/21/2019, 11:34:23 PM