Java to achieve text stacking pictures?

accidentally stroll around Zhihu and see a code that realizes stacking pictures with text. The effect is as follows:
(figure invading and deleting @ xlzd)

clipboard.png

clipboard.png

source code is written by py, but did not learn python, probably read some ideas, assuming that the font is 10px, then use a word instead of 10px pixels, the color is the average RGB color of these 100 pixels, I tried to achieve it with java, and found that there are some problems, thinking for a long time, did not find the problem, there is no boss under ah
.

the following is the source code:

"+ chars +"</font>");
                    if (j == colBlockNum-1)
                        bufferedWriter.write("<br/>\n");
                    //
                    rgb[0] = 0;
                    rgb[1] = 0;
                    rgb[2] = 0;
                }
            }
            bufferedWriter.write("\n</div>");
            bufferedWriter.write("</body>\n");
            bufferedWriter.write("</html>\n");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            bufferedWriter.close();
        }
    }

    public static String toHex(int num){
        String hex = Integer.toHexString(num);
        if(hex.length() == 2){
            return hex;
        }else {
            return "0"+hex;
        }
    }

    public static int[] getAvg(int[] rgb,int fontSize) {
        int[] avg = new int[3];
        avg[0] = rgb[0] / (fontSize * fontSize);
        avg[1] = rgb[1] / (fontSize * fontSize);
        avg[2] = rgb[2] / (fontSize * fontSize);
        return avg;
    }

    public static String rgb2hex(int[] rgbAvg){
        String rgbHex = "-sharp" + toHex(rgbAvg[0]) + toHex(rgbAvg[1]) + toHex(rgbAvg[2]);
        return rgbHex;
    }
}
Mar.12,2021
Menu