From befbf57e4112d07003bff18102f556a1e5a154de Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Wed, 22 Apr 2026 10:53:37 +0800
Subject: [PATCH] 1

---
 trading-order-service/src/main/java/com/yami/trading/service/user/impl/QRGenerateServiceImpl.java |   37 ++++++++++++++++++++++++-------------
 1 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/trading-order-service/src/main/java/com/yami/trading/service/user/impl/QRGenerateServiceImpl.java b/trading-order-service/src/main/java/com/yami/trading/service/user/impl/QRGenerateServiceImpl.java
index 64d1c10..158ea53 100644
--- a/trading-order-service/src/main/java/com/yami/trading/service/user/impl/QRGenerateServiceImpl.java
+++ b/trading-order-service/src/main/java/com/yami/trading/service/user/impl/QRGenerateServiceImpl.java
@@ -1,6 +1,7 @@
 package com.yami.trading.service.user.impl;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -49,11 +50,11 @@
         int width = 260;
         int height = 260;
         String format = "png";
-        Map hints = new HashMap();
+        Map<EncodeHintType, Object> hints = new HashMap<>();
         hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
         try {
             BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
-            MatrixToImageWriter.writeToFile(bitMatrix, format, file);
+            writeQrImageToFile(bitMatrix, format, file);
         } catch (Exception e) {
             log.error("write to image error:", e);
         }
@@ -89,11 +90,11 @@
         int width = 260;
         int height = 260;
         String format = "png";
-        Map hints = new HashMap();
+        Map<EncodeHintType, Object> hints = new HashMap<>();
         hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
         try {
             BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
-            MatrixToImageWriter.writeToFile(bitMatrix, format, file);
+            writeQrImageToFile(bitMatrix, format, file);
         } catch (Exception e) {
         	log.error("write to image error:", e);
         }
@@ -111,13 +112,13 @@
         int width = 185;
         int height = 185;
         String format = "png";
-        Map hints = new HashMap();
+        Map<EncodeHintType, Object> hints = new HashMap<>();
         hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
         hints.put(EncodeHintType.MARGIN, 1);// 二维码空白区域,最小为0也有白边,只是很小,最小是6像素左右
 
         try {
             BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
-            MatrixToImageWriter.writeToFile(bitMatrix, format, file);
+            writeQrImageToFile(bitMatrix, format, file);
         } catch (Exception e) {
         	log.error("write to image error:", e);
         }
@@ -151,19 +152,15 @@
     public String generateWithdraw(String content, String address) {
         String image_name = "/qr/" + content + ".png";
         String filepath = Constants.IMAGES_DIR + image_name;
-        File file=new File( Constants.IMAGES_DIR);
-        if (!file.isDirectory()){
-            file.mkdirs();
-        }
-        file = new File(filepath);
+        File file = new File(filepath);
         int width = 260;
         int height = 260;
         String format = "png";
-        Map hints = new HashMap();
+        Map<EncodeHintType, Object> hints = new HashMap<>();
         hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
         try {
             BitMatrix bitMatrix = new MultiFormatWriter().encode(address, BarcodeFormat.QR_CODE, width, height, hints);
-            MatrixToImageWriter.writeToFile(bitMatrix, format, file);
+            writeQrImageToFile(bitMatrix, format, file);
         } catch (Exception e) {
         	log.error("write to image error:", e);
         }
@@ -188,4 +185,18 @@
 
         return list_image;
     }
+
+    private void writeQrImageToFile(BitMatrix bitMatrix, String format, File file) throws IOException {
+        if (bitMatrix == null) {
+            throw new IOException("bitMatrix is null");
+        }
+        if (file == null) {
+            throw new IOException("target file is null");
+        }
+        File parent = file.getParentFile();
+        if (parent != null && !parent.exists() && !parent.mkdirs()) {
+            throw new IOException("failed to create qr image directory: " + parent.getAbsolutePath());
+        }
+        MatrixToImageWriter.writeToFile(bitMatrix, format, file);
+    }
 }

--
Gitblit v1.9.3