{"id":1440,"date":"2023-04-25T17:06:22","date_gmt":"2023-04-25T09:06:22","guid":{"rendered":"https:\/\/blog.ttwow.top\/?p=1440"},"modified":"2026-06-19T17:41:05","modified_gmt":"2026-06-19T09:41:05","slug":"%e5%a4%9a%e4%b8%aabase64%e5%9b%be%e7%89%87%e5%90%88%e5%b9%b6%e6%88%90%e4%b8%80%e4%b8%aa","status":"publish","type":"post","link":"https:\/\/blog.ttwow.top\/?p=1440","title":{"rendered":"\u591a\u4e2abase64\u56fe\u7247\u5408\u5e76\u6210\u4e00\u4e2a"},"content":{"rendered":"<pre><code class=\"language-java\">    \/**\n     * \u5408\u5e76\u4efb\u6570\u91cf\u7684\u56fe\u7247\u6210\u4e00\u5f20\u56fe\u7247\n     *\n     * @param isHorizontal\n     *            true\u4ee3\u8868\u6c34\u5e73\u5408\u5e76\uff0cfasle\u4ee3\u8868\u5782\u76f4\u5408\u5e76\n     * @param imgStr\n     *            \u5f85\u5408\u5e76\u7684\u56fe\u7247\u6570\u7ec4\n     * @return\n     * @throws IOException\n     *\/\n    private static String mergeImage(boolean isHorizontal, List&lt;String&gt; imgStr) {\n        \/\/ \u521b\u5efaBase64\u89e3\u7801\u5668\n        Base64.Decoder decoder = Base64.getDecoder();\n        \/\/ \u751f\u6210\u65b0\u56fe\u7247\n        BufferedImage destImage = null;\n        \/\/ \u8ba1\u7b97\u65b0\u56fe\u7247\u7684\u957f\u548c\u9ad8\n        int allw = 0, allh = 0, allwMax = 0, allhMax = 0;\n        \/\/ \u83b7\u53d6\u603b\u957f\u3001\u603b\u5bbd\u3001\u6700\u957f\u3001\u6700\u5bbd\n        for (int i = 0; i &lt; imgStr.size(); i++) {\n            \/\/ \u5c06Base64\u7f16\u7801\u7684\u56fe\u7247\u89e3\u7801\u4e3a\u5b57\u8282\u6570\u7ec4\n            byte[] imageBytes = decoder.decode(imgStr.get(i));\n            \/\/ \u521b\u5efa\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4\u8f93\u5165\u6d41\n            ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);\n            \/\/ \u8bfb\u53d6\u5b57\u8282\u6570\u7ec4\u8f93\u5165\u6d41\u4e3a\u4e00\u4e2aBufferedImage\u5bf9\u8c61\n            BufferedImage img = null;\n            try {\n                img = ImageIO.read(bis);\n            } catch (IOException e) {\n                throw new RuntimeException(e);\n            }\n\n            allw += img.getWidth();\n            allh += img.getHeight();\n            if (img.getWidth() &gt; allwMax) {\n                allwMax = img.getWidth();\n            }\n            if (img.getHeight() &gt; allhMax) {\n                allhMax = img.getHeight();\n            }\n        }\n        \/\/ \u521b\u5efa\u65b0\u56fe\u7247\n        if (isHorizontal) {\n            destImage = new BufferedImage(allw, allhMax, Transparency.TRANSLUCENT);\n        } else {\n            destImage = new BufferedImage(allwMax, allh, Transparency.TRANSLUCENT);\n        }\n\n        \/\/ \u5408\u5e76\u6240\u6709\u5b50\u56fe\u7247\u5230\u65b0\u56fe\u7247\n        int wx = 0, wy = 0;\n        for (int i = 0; i &lt; imgStr.size(); i++) {\n            \/\/ \u5c06Base64\u7f16\u7801\u7684\u56fe\u7247\u89e3\u7801\u4e3a\u5b57\u8282\u6570\u7ec4\n            byte[] imageBytes = decoder.decode(imgStr.get(i));\n            \/\/ \u521b\u5efa\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4\u8f93\u5165\u6d41\n            ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);\n            \/\/ \u8bfb\u53d6\u5b57\u8282\u6570\u7ec4\u8f93\u5165\u6d41\u4e3a\u4e00\u4e2aBufferedImage\u5bf9\u8c61\n            BufferedImage img = null;\n            try {\n                img = ImageIO.read(bis);\n            } catch (IOException e) {\n                throw new RuntimeException(e);\n            }\n\n            int w1 = img.getWidth();\n            int h1 = img.getHeight();\n            \/\/ \u4ece\u56fe\u7247\u4e2d\u8bfb\u53d6RGB\n            int[] ImageArrayOne = new int[w1 * h1];\n            ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); \/\/ \u9010\u884c\u626b\u63cf\u56fe\u50cf\u4e2d\u5404\u4e2a\u50cf\u7d20\u7684RGB\u5230\u6570\u7ec4\u4e2d\n            if (isHorizontal) { \/\/ \u6c34\u5e73\u65b9\u5411\u5408\u5e76\n                destImage.setRGB(wx, 0, w1, h1, ImageArrayOne, 0, w1); \/\/ \u8bbe\u7f6e\u4e0a\u534a\u90e8\u5206\u6216\u5de6\u534a\u90e8\u5206\u7684RGB\n            } else { \/\/ \u5782\u76f4\u65b9\u5411\u5408\u5e76\n                destImage.setRGB(0, wy, w1, h1, ImageArrayOne, 0, w1); \/\/ \u8bbe\u7f6e\u4e0a\u534a\u90e8\u5206\u6216\u5de6\u534a\u90e8\u5206\u7684RGB\n            }\n            wx += w1;\n            wy += h1;\n        }\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4\u8f93\u51fa\u6d41\n        ByteArrayOutputStream bos = new ByteArrayOutputStream();\n        \/\/ \u5c06combined\u5bf9\u8c61\u5199\u5165\u5b57\u8282\u6570\u7ec4\u8f93\u51fa\u6d41\n        byte[] imageBytes = null;\n        try {\n            ImageIO.write(destImage, &quot;png&quot;, bos);\n            imageBytes = bos.toByteArray();\n            bos.close();\n        } catch (IOException e) {\n            throw new RuntimeException(e);\n        }\n\n        \/\/ \u521b\u5efaBase64\u7f16\u7801\u5668\n        Base64.Encoder encoder = Base64.getEncoder();\n        \/\/ \u5c06\u5b57\u8282\u6570\u7ec4\u7f16\u7801\u4e3aBase64\u5b57\u7b26\u4e32\n        String base64String = encoder.encodeToString(imageBytes);\n\n        return base64String;\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/** * \u5408\u5e76\u4efb\u6570\u91cf\u7684\u56fe\u7247\u6210\u4e00\u5f20\u56fe\u7247 * * @param isHorizontal * true\u4ee3\u8868\u6c34\u5e73\u5408 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[135],"tags":[147],"class_list":["post-1440","post","type-post","status-publish","format-standard","hentry","category-code-snippets","tag-147"],"_links":{"self":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/1440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1440"}],"version-history":[{"count":1,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/1440\/revisions"}],"predecessor-version":[{"id":1441,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/1440\/revisions\/1441"}],"wp:attachment":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}