java后台生成二维码
需求:生成的二维码可以在微信、支付宝、等手机扫码设备可识别内容!
1.导入maven
<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.0</version> </dependency>
2.1:先举例生成二维码保存本地:
/**
* 存放本地固定路径
*/
private static final String QR_CODE_IMAGE_PATH = "F:\\kedaSoft\\appinterface\\WebRoot\\ycs.png";
private static void generateQRCodeImage(String text, Integer width, Integer height, String filePath) throws WriterException, IOException { QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); Path path = FileSystems.getDefault().getPath(filePath); MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); } public static void main(String[] args) { try { generateQRCodeImage("测试,保存在本地的二维码", 350, 350, QR_CODE_IMAGE_PATH); } catch (WriterException e) { System.out.println("写入异常 :: " + e.getMessage()); } catch (IOException e) { System.out.println("io异常 IOException :: " + e.getMessage()); } }
如图所示:找到保存的路径查看:
2.2:举例生成二维码不保存本地使用web浏览器访问:
/** * * @param text 扫码显示内容 * @param width 二维码长度 * @param height 二维码高度 * @return 返回byte数组 * @throws WriterException 写入异常 * @throws IOException io异常 */ public static byte[] getQRCodeImage(String text, Integer width, Integer height) throws WriterException, IOException{ QRCodeWriter qrCodeWriter = new QRCodeWriter(); // 解决中文乱码 HashMap<EncodeHintType, Object> hints = new HashMap<>(16); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix encode = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); MatrixToImageWriter.writeToStream(encode,"PNG",byteArrayOutputStream); return byteArrayOutputStream.toByteArray(); }
// 这里我是用的jfinal框架,如你用的是spring:加上RequestMaping(“/qrCode”)即可。
public void qrCode(){ HttpServletResponse response = this.getResponse(); //二维码内的信息 String info = "二维码内的信息"; try { byte[] qrCode = QrCodeGenerator.getQRCodeImage(info, 360, 360); OutputStream outputStream = response.getOutputStream(); outputStream.write(qrCode); response.setCharacterEncoding("utf-8"); response.setContentType("image/png"); outputStream.flush(); } catch (WriterException e) { System.out.println("写入异常 :: " + e.getMessage()); } catch (IOException e) { System.out.println("io异常 IOException :: " + e.getMessage()); } }
3.:全部放到一个工具类:
package com.wom.utils; /** * @author ycs * @date 2022/2/14 */ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Path; import java.util.HashMap; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class QrCodeGenerator { /** * 存放本地固定路径 */ private static final String QR_CODE_IMAGE_PATH = "F:\\kedaSoft\\appinterface\\WebRoot\\ycs.png"; private static void generateQRCodeImage(String text, Integer width, Integer height, String filePath) throws WriterException, IOException { QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); Path path = FileSystems.getDefault().getPath(filePath); MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path); } /** * * @param text 扫码显示内容 * @param width 二维码长度 * @param height 二维码高度 * @return 返回byte数组 * @throws WriterException 写入异常 * @throws IOException io异常 */ public static byte[] getQRCodeImage(String text, Integer width, Integer height) throws WriterException, IOException{ QRCodeWriter qrCodeWriter = new QRCodeWriter(); // 解决中文乱码 HashMap<EncodeHintType, Object> hints = new HashMap<>(16); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix encode = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); MatrixToImageWriter.writeToStream(encode,"PNG",byteArrayOutputStream); return byteArrayOutputStream.toByteArray(); } public static void main(String[] args) { try { generateQRCodeImage("测试,保存在本地的二维码", 350, 350, QR_CODE_IMAGE_PATH); } catch (WriterException e) { System.out.println("写入异常 :: " + e.getMessage()); } catch (IOException e) { System.out.println("io异常 IOException :: " + e.getMessage()); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!