java 生成二维码后叠加LOGO并转换成base64
1.代码
见文末推荐
2.测试
测试1:生成base64码
public static void main(String[] args) throws Exception { String data = "http://www.cnblogs.com/Marydon20170307"; File logoFile = new File(QRcodeUtils.class.getResource("ewm.jpg").getPath()); BufferedImage image = QRcodeUtils.createQRCodeWithLogo(data, logoFile); String base64 = QRcodeUtils.writeToString(image); System.out.println(base64); }
页面调用:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJYAAACWCAYAAAA8AXHiAAARAUlEQVR42u2d23NT1xXGeWkmQHxj TBPIYxmSvqcMkDwF3LcGQi/PIeTW9g8AkkAymabTp870McFIlm0uzQUDAduQNwwOAWwD1tUk8UUX OwMYSzq62ZJW17e2jpJMyxRiHckya8+ckXx8fM5B+8da3/722kcrSJs2B9oK/Qi0KVjaFCxtCpY2 bQqWNgVLm4KlTZuCpU3B0qZgadOmYGlTsLQpWNq0KVjaFCxtCpY2bQqWNgVLm4K1iJOtWFHx7X7n f9j9D3L+BznnYo6v5r9rMZ+zgqVgKVgKloJVEbCcOE+l4KjU+St1/05D7/R5FCwFS8FSsBSsimud h93/sJqvXvSK05pSwVKwFCwFS8GqO7AqpVcWo2mcuJYT//EULAVLwVKwFKxlD5bT2qtWmk81loKl YClYCtayBKtWzvti7rNSmqlSkKnzrmApWAqWgqX1WES6vw5mCxQsBUvBUoAecbCcbk473U445rXo 1CXRVwqWgqVgKVgKlhOOsBP1W9WcFHfiP4zT96ZgKVgKloKlYDnqaFfKfa4mcEvB8a/U7EXdaCwF S8FSsBSs+hwVOj0Ur9U5F9ORTpcdOz15r2ApWAqWgqVgVQ2saloMtdKL1Vysu6ycdwVLwVKwFKz6 dN4rNSyvFWRLQTNV06ZRsBQsBUvBUrDqApr/92EVshGanJwkX+AK+cYGaWy8j8Ym++jWRB/NRftl S3u7KeU9Vt4XD/dSPHKeUoEusrxdlPMe55/7aZb34/f3In1kTZ2n677DNOw/TKFIB4X4b4PJThqL d1HEOktzODbcJ/vHxnvl3KG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+Byd8cOLD cl18urRUHj4VJpUBkowaS8vlBSqsKZSVPgYqQGpHoh+g9pQjVSDsKQOH+0oVrilYlQTLiQ/ifx2L B5vNLgxIJxrIzJNnAJX9ADZTaWAil1gSpcgTlOdlHS1XocoSe36F445XRDacwx4YBEsC3QYqWBoZ YkGH/TvzSCWzAjpX9P/kwWsP+2+vFBzL9svGnQTLqQftO100p2ApWApWrcGq1D/S6Q+0ml9b4sT+ Sv2tgqVgKVgKloLlaM524tuqnP4STSc6yYnnvDv9NSoKloKlYClYClbFh69OrtJZios5q/nkvlo3 BUvBUrAULAXLkWG807DW6t6qqbGW7RP9FCwFS8FSsJYGWI7n7TqxFWrV8UtJqylYCpaCpWA9omDV 6nuOnf4Qq2k9OK1Zl+2jIhUsBUvBUrDq03mv1Xmq+YwJJ8qjK3UPde+8K1gKloKlYNWn816rb7eq 5oqaat5/NcFVsBQsBUvBUrCWFFiL2V9rV7ran6eCpWApWAqWgvXIgvWwH+hS0H+1sjYULAVLwVKw FKy6c96d7iQnLAanZwVqDZOCpWApWAqWglVVZ7yaiz9rtTi2VjMECpaCpWApWAqWNm0KljYFS5uC pU2bgqVNwdKmYGnTpmBpU7C0KVjatClY2hQsbQqWNm0KljYFS5uCpU2bgqVNwdKmYGnTpmBpWwrt PzS0PEBfvLClAAAAAElFTkSuQmCC" />
效果展示:
说明:源代码生成的图片格式已经改成了JPG,这里测试的是PNG,自己记得修改!
2018/11/29
测试2:生成图片
public static void main(String[] args) { String data = "Marydon"; // 不带logo BufferedImage image = createQRCode(data); String fileName = "博客园"; File qrcodeFile = new File("d:/" + fileName + ".png"); ZxingQrcode.writeToFile(image, qrcodeFile); // 带logo File logoFile = new File("D:\\ewm.jpg"); image = createQRCodeWithLogo(data, logoFile); fileName = "博客园2"; qrcodeFile = new File("d:/" + fileName + ".png"); ZxingQrcode.writeToFile(image, qrcodeFile); }
说明:生成的二维码图片如果它的上级目录不存在,会创建失败!
qrcodeFile = new File("d:/测试/" + fileName + ".png"); // 文件夹不存在,自动创建文件夹 if (!qrcodeFile.exists()) { qrcodeFile.mkdirs(); }
本文来自博客园,作者:Marydon,转载请注明原文链接:https://www.cnblogs.com/Marydon20170307/p/9543657.html