使用Zxing二维码生成
1、引用依赖
1 2 3 4 5 6 7 8 9 10 | <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.3</version> </dependency><br> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.11</version> </dependency> |
2、生成二维码工具类QrCodeUtil
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | package com.qingfeng.qianduan.utils; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.util.Hashtable; import javax.imageio.ImageIO; import com.alibaba.druid.util.StringUtils; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; /** * 生成二维码 */ public class QrCodeUtil { private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; private static final int margin = 0; private static final int LogoPart = 4; /** * 设置生成二维码矩阵信息 * @param content 二维码图片内容 * @param width 二维码图片宽度 * @param height 二维码图片高度 * @throws WriterException */ public static BitMatrix setBitMatrix(String content, int width, int height) throws WriterException { BitMatrix bitMatrix = null ; Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8" ); // 指定编码方式,避免中文乱码 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 指定纠错等级 如果二维码里面的内容比较多的话推荐使用H 容错率30%, 这样可以避免一些扫描不出来的问题 hints.put(EncodeHintType.MARGIN, margin); // 指定二维码四周白色区域大小 官方的这个方法目前没有没有作用默认设置为0 bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); return bitMatrix; } /** * @param matrix * @param format * @param outStream * @param logoPath logo图片 * @param reduceWhiteArea 二维码空白区域设置 * @throws IOException */ public static void writeToFile(BitMatrix matrix, String format, OutputStream outStream, String logoPath, int reduceWhiteArea) throws IOException { BufferedImage image = toBufferedImage(matrix, reduceWhiteArea); // 如果设置了二维码里面的logo 加入LOGO水印 if (!StringUtils.isEmpty(logoPath)) { image = addLogo(image, logoPath); } ImageIO.write(image, format, outStream); } /** * * @param matrix * @param reduceWhiteArea * @return */ public static BufferedImage toBufferedImage(BitMatrix matrix, int reduceWhiteArea) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width - 2 * reduceWhiteArea, height - 2 * reduceWhiteArea, BufferedImage.TYPE_3BYTE_BGR); for ( int x = reduceWhiteArea; x < width - reduceWhiteArea; x++) { for ( int y = reduceWhiteArea; y < height - reduceWhiteArea; y++) { image.setRGB(x - reduceWhiteArea, y - reduceWhiteArea, matrix. get (x, y) ? BLACK : WHITE); } } return image; } /** * 给二维码图片中绘制logo信息 非必须 * @param image 二维码图片 * @param logoPath logo图片路径 */ public static BufferedImage addLogo(BufferedImage image, String logoPath) throws IOException { Graphics2D g = image.createGraphics(); BufferedImage logoImage = ImageIO.read( new File(logoPath)); // 计算logo图片大小,可适应长方形图片,根据较短边生成正方形 int width = image.getWidth() < image.getHeight() ? image.getWidth() / LogoPart : image.getHeight() / LogoPart; int height = width; // 计算logo图片放置位置 int x = (image.getWidth() - width) / 2; int y = (image.getHeight() - height) / 2; // 在二维码图片上绘制中间的logo g.drawImage(logoImage, x, y, width, height, null ); // 绘制logo边框,可选 g.setStroke( new BasicStroke(2)); // 画笔粗细 g.setColor(Color.WHITE); // 边框颜色 g.drawRect(x, y, width, height); // 矩形边框 logoImage.flush(); g.dispose(); return image; } } |
3、测试生成二维码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //http://localhost:10000/QrCode @RequestMapping( "/QrCode" ) public void QrCode() throws WriterException { //二维码内容 String content = "生成二维码" ; String logoPath = "D:/logo.png" ; // 二维码中间的logo信息 非必须 String format = "jpg" ; int width = 120; // 二维码宽度 int height = 120; // 二维码高度 // 设置二维码矩阵的信息 BitMatrix bitMatrix = QrCodeUtil.setBitMatrix(content, width, height); // 设置输出流 OutputStream outStream = null ; String path = "D:/QrCode/" + new Date().getTime() + ".png" ; //设置二维码的文件名 try { outStream = new FileOutputStream( new File(path)); // 目前 针对容错等级为H reduceWhiteArea 二维码空白区域的大小 根据实际情况设置,如果二维码内容长度不固定的话 需要自己根据实际情况计算reduceWhiteArea的大小 QrCodeUtil.writeToFile(bitMatrix, format, outStream, null , 5); outStream.close(); } catch (Exception e) { e.printStackTrace(); } } |
参考:https://blog.csdn.net/xishining/article/details/128700068
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战