生成二维码 傷城~ 2022-10-20 13:55 503阅读 0赞 相关博客:[统一返回结果][Link 1] ## 创建SpringBoot项目 ## <!-- 二维码生成 --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.4.1</version> </dependency> ## 第一步:Base64Util工具类 ## 参看:[Base64Util工具类][Base64Util] ## 第二步:二维码工具类 ## import com.github.xiaoymin.knife4j.core.util.StrUtil; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.hc.utils.encrypt.Base64Util; import lombok.experimental.UtilityClass; import lombok.extern.slf4j.Slf4j; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import java.awt.BasicStroke; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Shape; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.HashMap; /** * 二维码工具类 * * @author 梁云亮 */ @Slf4j @UtilityClass public class QRCodeUtil { /** * 默认宽度 */ private static final Integer WIDTH = 140; /** * 默认高度 */ private static final Integer HEIGHT = 140; /** * LOGO 默认宽度 */ private static final Integer LOGO_WIDTH = 22; /** * LOGO 默认高度 */ private static final Integer LOGO_HEIGHT = 22; /** * 图片格式 */ private static final String IMAGE_FORMAT = "png"; private static final String CHARSET = "utf-8"; /** * 原生转码前面没有 data:image/png;base64 这些字段,返回给前端是无法被解析 */ private static final String BASE64_IMAGE = "data:image/png;base64,%s"; /** * 生成二维码,使用默认尺寸 * * @param content 内容 * @return */ public String getBase64QRCode(String content) { return getBase64Image(content, WIDTH, HEIGHT, null, null, null); } /** * 生成二维码,使用默认尺寸二维码,插入默认尺寸logo * * @param content 内容 * @param logoUrl logo地址 * @return */ public String getBase64QRCode(String content, String logoUrl) { return getBase64Image(content, WIDTH, HEIGHT, logoUrl, LOGO_WIDTH, LOGO_HEIGHT); } /** * 生成二维码 * * @param content 内容 * @param width 二维码宽度 * @param height 二维码高度 * @param logoUrl logo 在线地址 * @param logoWidth logo 宽度 * @param logoHeight logo 高度 * @return */ public String getBase64QRCode(String content, Integer width, Integer height, String logoUrl, Integer logoWidth, Integer logoHeight) { return getBase64Image(content, width, height, logoUrl, logoWidth, logoHeight); } private String getBase64Image(String content, Integer width, Integer height, String logoUrl, Integer logoWidth, Integer logoHeight) { ByteArrayOutputStream os = new ByteArrayOutputStream(); BufferedImage bufferedImage = crateQRCode(content, width, height, logoUrl, logoWidth, logoHeight); try { ImageIO.write(bufferedImage, IMAGE_FORMAT, os); } catch (IOException e) { log.error("[生成二维码,错误{}]", e); } // 转出即可直接使用 return String.format(BASE64_IMAGE, new String(Base64Util.encode(os.toByteArray()))); } /** * 生成二维码 * * @param content 内容 * @param width 二维码宽度 * @param height 二维码高度 * @param logoUrl logo 在线地址 * @param logoWidth logo 宽度 * @param logoHeight logo 高度 * @return */ private BufferedImage crateQRCode(String content, Integer width, Integer height, String logoUrl, Integer logoWidth, Integer logoHeight) { if (StrUtil.isNotBlank(content)) { ServletOutputStream stream = null; HashMap<EncodeHintType, Comparable> hints = new HashMap<>(4); // 指定字符编码为utf-8 hints.put(EncodeHintType.CHARACTER_SET, CHARSET); // 指定二维码的纠错等级为中级 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); // 设置图片的边距 hints.put(EncodeHintType.MARGIN, 2); try { QRCodeWriter writer = new QRCodeWriter(); BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints); BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } if (StrUtil.isNotBlank(logoUrl)) { insertLogo(bufferedImage, width, height, logoUrl, logoWidth, logoHeight); } return bufferedImage; } catch (Exception e) { e.printStackTrace(); } finally { if (stream != null) { try { stream.flush(); stream.close(); } catch (IOException e) { e.printStackTrace(); } } } } return null; } /** * 二维码插入logo * * @param source 二维码 * @param width 二维码宽度 * @param height 二维码高度 * @param logoUrl logo 在线地址 * @param logoWidth logo 宽度 * @param logoHeight logo 高度 * @throws Exception */ private void insertLogo(BufferedImage source, Integer width, Integer height, String logoUrl, Integer logoWidth, Integer logoHeight) throws Exception { // logo 源可为 File/InputStream/URL Image src = ImageIO.read(new URL(logoUrl)); // 插入LOGO Graphics2D graph = source.createGraphics(); int x = (width - logoWidth) / 2; int y = (height - logoHeight) / 2; graph.drawImage(src, x, y, logoWidth, logoHeight, null); Shape shape = new RoundRectangle2D.Float(x, y, logoWidth, logoHeight, 6, 6); graph.setStroke(new BasicStroke(3f)); graph.draw(shape); graph.dispose(); } /** * 获取二维码 * * @param content 内容 * @param output 输出流 * @throws IOException */ public void getQRCode(String content, OutputStream output) throws IOException { BufferedImage image = crateQRCode(content, WIDTH, HEIGHT, null, null, null); ImageIO.write(image, IMAGE_FORMAT, output); } /** * 获取二维码 * * @param content 内容 * @param logoUrl logo资源 * @param output 输出流 * @throws Exception */ public void getQRCode(String content, String logoUrl, OutputStream output) throws Exception { BufferedImage image = crateQRCode(content, WIDTH, HEIGHT, logoUrl, LOGO_WIDTH, LOGO_HEIGHT); ImageIO.write(image, IMAGE_FORMAT, output); } } ## 二维码控制器 ## @RestController @RequestMapping("/qrcode") public class QRCodeController { /** * 根据 content 生成二维码 * 测试:http://localhost/blog/portals/qrcode/getQRCode?content=www.hcshow.online&logoUrl=http://pic.51yuansu.com/pic3/cover/03/84/85/5c11a40a32429_610.jpg * * @param content * @param width * @param height * @return */ @GetMapping("/getQRCodeBase64") public Result<String> getQRCode(@RequestParam("content") String content, @RequestParam(value = "logoUrl", required = false) String logoUrl, @RequestParam(value = "width", required = false) Integer width, @RequestParam(value = "height", required = false) Integer height) { String base64QRCode = QRCodeUtil.getBase64QRCode(content, logoUrl); System.out.println(base64QRCode); return ResultUtil.success(200, base64QRCode); } /** * 根据 content 生成二维码 * 测试:http://localhost/blog/portals/qrcode/getQRCode?content=www.hcshow.online */ @GetMapping(value = "/getQRCode") public void getQRCode(HttpServletResponse response, @RequestParam("content") String content, @RequestParam(value = "logoUrl", required = false) String logoUrl) throws Exception { try ( ServletOutputStream stream = response.getOutputStream();) { QRCodeUtil.getQRCode(content, logoUrl, stream); } catch (Exception e) { e.getStackTrace(); } } } ## 测试 ## * 不带logo二维码 url:http://localhost/blog/portals/qrcode/getQRCode?content=www.hcshow.online 效果: ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpYW5naGVjYWk1MjE3MTMxNA_size_16_color_FFFFFF_t_70] * 带logo二维码 url:http://localhost/blog/portals/qrcode/getQRCodeBase64?content=www.hcshow.online&logoUrl=http://localhost/blog/portals/mm.jpg 结果: ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpYW5naGVjYWk1MjE3MTMxNA_size_16_color_FFFFFF_t_70 1] * 打开base64图片工具网页:[http://tool.chinaz.com/tools/imgtobase/][http_tool.chinaz.com_tools_imgtobase] ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpYW5naGVjYWk1MjE3MTMxNA_size_16_color_FFFFFF_t_70 2] [Link 1]: https://blog.csdn.net/lianghecai52171314/article/details/103160934?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162130978516780261915279%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=162130978516780261915279&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_v2~rank_v29-2-103160934.nonecase&utm_term=%E7%BB%9F%E4%B8%80&spm=1018.2226.3001.4450 [Base64Util]: https://hcshow.blog.csdn.net/article/details/116977328 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpYW5naGVjYWk1MjE3MTMxNA_size_16_color_FFFFFF_t_70]: /images/20221020/e0a5607bdebe4cc59ff8aefc8622383b.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpYW5naGVjYWk1MjE3MTMxNA_size_16_color_FFFFFF_t_70 1]: /images/20221020/917d3ae5127f46c88348504f86d5975c.png [http_tool.chinaz.com_tools_imgtobase]: http://tool.chinaz.com/tools/imgtobase/ [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpYW5naGVjYWk1MjE3MTMxNA_size_16_color_FFFFFF_t_70 2]: /images/20221020/e2bdc36129aa455881a71e34549fd675.png
相关 生成二维码 ,需要依赖: <dependency> <groupId>com.google.zxing</groupId> ... 不念不忘少年蓝@/ 2024年04月18日 16:19/ 0 赞/ 233 阅读
相关 生成二维码 生成二维码 提供技术支持 public String createUrl1(OrderForm obj, HttpServletRequest request){ 拼搏现实的明天。/ 2023年10月05日 15:36/ 0 赞/ 41 阅读
相关 生成二维码 引入依赖 <!-- https://mvnrepository.com/artifact/com.google.zxing/core --> 缺乏、安全感/ 2023年07月15日 12:52/ 0 赞/ 170 阅读
相关 二维码生成源码 二维码生成源码 开发前准备第三方包:QRCode.jar 下载链接:[QRCode.jar百度云下载链接][QRCode.jar] 提取码:1pvs ![在这里插入图 蔚落/ 2023年06月27日 06:17/ 0 赞/ 199 阅读
相关 生成二维码 相关博客:[统一返回结果][Link 1] 创建SpringBoot项目 <!-- 二维码生成 --> <dependency> <gr 傷城~/ 2022年10月20日 13:55/ 0 赞/ 504 阅读
相关 swift生成二维码,扫描二维码 ~~~写在前面的话~~~ 我之前打算做一个APP,然后把电话号码生成二维码 或者条形码, 用手机扫描,这样,就不用担心会输入错误电话号码了。 在下面是实现的扫描二维码的功能 墨蓝/ 2022年09月25日 13:19/ 0 赞/ 708 阅读
相关 生成二维码 一个命名空间 namespace BarCode \{ public static class TwoDimensionCode \{ 悠悠/ 2022年08月05日 01:17/ 0 赞/ 526 阅读
相关 生成二维码 文档: [http://blog.csdn.net/yuxmdef1/article/details/17793461][http_blog.csdn.net_yuxmdef1 男娘i/ 2022年06月13日 06:38/ 0 赞/ 554 阅读
相关 生成二维码 生成二维码 / 生成二维码(QRCode)图片的公共方法 @param content 存储内容 @param imgTyp 小鱼儿/ 2022年03月31日 05:10/ 0 赞/ 539 阅读
相关 二维码生成 maven依赖 <!-- 二维码 --> <dependency> <groupId>com.google.zxing</groupId> 悠悠/ 2021年12月10日 15:39/ 0 赞/ 683 阅读
还没有评论,来说两句吧...