二维码接口
This commit is contained in:
		
							
								
								
									
										56
									
								
								src/main/java/com/sczx/user/controller/QrCodeController.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								src/main/java/com/sczx/user/controller/QrCodeController.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,56 @@ | ||||
| package com.sczx.user.controller; | ||||
|  | ||||
|  | ||||
| import com.ruoyi.common.core.domain.AjaxResult; | ||||
| 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; | ||||
| import org.springframework.web.bind.annotation.*; | ||||
|  | ||||
| import java.io.ByteArrayOutputStream; | ||||
| import java.io.IOException; | ||||
| import java.util.Base64; | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| import com.sczx.user.common.Result; | ||||
| /** | ||||
|  * 二维码生成控制器 | ||||
|  */ | ||||
| @RestController | ||||
| @RequestMapping("/qrcode") | ||||
| public class QrCodeController { | ||||
|  | ||||
|     /** | ||||
|      * 生成二维码并返回Base64编码 | ||||
|      * | ||||
|      * @return 包含Base64编码的二维码图片 | ||||
|      */ | ||||
|     @PostMapping("/generateOrderCarChangeBase64") | ||||
|     public Result<String> generateOrderCarChangeQrCodeBase64(@RequestParam(value = "qrCodeStr", required = true) String qrCodeStr) { | ||||
|         try { | ||||
|  | ||||
|             // 设置二维码参数 | ||||
|             Map<EncodeHintType, Object> hints = new HashMap<>(); | ||||
|             hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); | ||||
|             hints.put(EncodeHintType.MARGIN, 1); | ||||
|  | ||||
|             // 生成二维码 | ||||
|             QRCodeWriter qrCodeWriter = new QRCodeWriter(); | ||||
|             BitMatrix bitMatrix = qrCodeWriter.encode(qrCodeStr.toString(), BarcodeFormat.QR_CODE, 300, 300, hints); | ||||
|  | ||||
|             // 将二维码转换为Base64编码 | ||||
|             ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||||
|             MatrixToImageWriter.writeToStream(bitMatrix, "PNG", outputStream); | ||||
|             byte[] qrCodeBytes = outputStream.toByteArray(); | ||||
|             String base64Image = Base64.getEncoder().encodeToString(qrCodeBytes); | ||||
|  | ||||
|             // 返回Base64编码的图片数据 | ||||
|             return Result.ok(base64Image); | ||||
|         } catch (WriterException | IOException e) { | ||||
|             return Result.fail("99999","二维码生成失败"); | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 19173159168
					19173159168