二维码生成 引入qrcode.js ,根据表单可以自动生成二维码,例如我自己得,但是暂时扫出来不能显示我的文字,只有时间 package com.example.mesproject.Controller; import com.google.zxing.BarcodeFormat; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import org.springframework.web.bind.annotation.*; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Map; @RestController @RequestMapping("/api/qrcode") public class QRCodeController { // 生成二维码并返回图片 @PostMapping("/generate") public ResponseEntity<byte[]> generateQRCode(@RequestBody Map<String, String> formData) throws WriterException, IOException { // 获取表单提交的数据 String name = formData.get("name"); String process = formData.get("process"); String time = formData.get("time"); String startTime = formData.get("startTime"); String endTime = formData.get("endTime"); // 拼接二维码内容 String qrContent = "员工姓名: " + name + ", 工序: " + process + ", 工时: " + time + ", 开始时间: " + startTime + ", 结束时间: " + endTime; // 打印二维码内容,便于调试 System.out.println("二维码内容: " + qrContent); // 生成二维码 QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(new String(qrContent.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8), BarcodeFormat.QR_CODE, 300, 300); // 将二维码转换为字节数组 ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream(); MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream); byte[] pngData = pngOutputStream.toByteArray(); // 设置返回的响应类型为图片 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAGE_PNG); headers.setContentLength(pngData.length); return ResponseEntity.ok().headers(headers).body(pngData); } }
标签:String,10.6,formData,zxing,二维码,import,com From: https://www.cnblogs.com/kongxiangzeng/p/18658539