{"id":1749,"date":"2023-03-26T21:25:29","date_gmt":"2023-03-26T13:25:29","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1749"},"modified":"2023-04-23T21:28:06","modified_gmt":"2023-04-23T13:28:06","slug":"convert-text-to-qr-code-image-base64-encoding","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/26\/convert-text-to-qr-code-image-base64-encoding\/","title":{"rendered":"\u5c06\u6587\u672c\u8f6c\u4e3a\u4e8c\u7ef4\u7801\u56fe\u7247Base64\u7f16\u7801"},"content":{"rendered":"<pre><code>import com.google.zxing.BarcodeFormat;\nimport com.google.zxing.EncodeHintType;\nimport com.google.zxing.MultiFormatWriter;\nimport com.google.zxing.WriterException;\nimport com.google.zxing.common.BitMatrix;\nimport com.lianpay.globalpay.common.exception.BizFailException;\nimport sun.misc.BASE64Encoder;\n\nimport javax.imageio.ImageIO;\nimport java.awt.image.BufferedImage;\nimport java.io.ByteArrayOutputStream;\nimport java.io.IOException;\nimport java.util.Hashtable;\n\npublic class QrcodeUtil {\n\n    public static String creatRrCode(String contents, int width, int height) {\n        String binary = null;\n        Hashtable hints = new Hashtable();\n        hints.put(EncodeHintType.CHARACTER_SET, &quot;utf-8&quot;);\n        try {\n\n            BitMatrix bitMatrix = new MultiFormatWriter().encode(\n                    contents, BarcodeFormat.QR_CODE, width, height, hints);\n\n            \/\/ 1\u3001\u8bfb\u53d6\u6587\u4ef6\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\n            ByteArrayOutputStream out = new ByteArrayOutputStream();\n            BufferedImage image = toBufferedImage(bitMatrix);\n            \/\/\u8f6c\u6362\u6210png\u683c\u5f0f\u7684IO\u6d41\n            ImageIO.write(image, &quot;png&quot;, out);\n            byte[] bytes = out.toByteArray();\n            \/\/ 2\u3001\u5c06\u5b57\u8282\u6570\u7ec4\u8f6c\u4e3a\u4e8c\u8fdb\u5236\n            BASE64Encoder encoder = new BASE64Encoder();\n            binary = encoder.encodeBuffer(bytes).trim();\n        } catch (Exception e) {\n            throw new BizFailException(&quot;product qrcode fail&quot;);\n        }\n        return binary;\n    }\n\n    \/**\n     * image\u6d41\u6570\u636e\u5904\u7406\n     *\n     * @author ianly\n     *\/\n    public static BufferedImage toBufferedImage(BitMatrix matrix) {\n        int width = matrix.getWidth();\n        int height = matrix.getHeight();\n        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);\n        for (int x = 0; x &lt; width; x++) {\n            for (int y = 0; y &lt; height; y++) {\n                image.setRGB(x, y, matrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);\n            }\n        }\n        return image;\n    }\n\n    public static void main(String[] args) {\n        String base64 = QrcodeUtil.creatRrCode(&quot;|010556008962106\\n&quot; +\n                &quot;132022120604337025\\n&quot; +\n                &quot;142019050800009001\\n&quot; +\n                &quot;2211&quot;, 480, 480);\n        System.out.println(base64);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import com.google.zxing.BarcodeFormat; import com.googl [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-1749","post","type-post","status-publish","format-standard","hentry","category-java-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1749","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1749"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1749\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}