ccj 1 年之前
父节点
当前提交
51fc4a743d

+ 8 - 1
winsea-haixin-plugin-wangluohuoyun/src/main/java/com/yh/saas/plugin/yiliangyiyun/service/impl/PublishTaskInfoServiceImpl.java

@@ -1351,7 +1351,12 @@ public class PublishTaskInfoServiceImpl extends ServiceImpl<PublishTaskInfoMappe
         List<PublishTaskInfo> publishTaskInfoList = this.selectPublishTaskInfoList();//调用Service中的方法
         String number = "YS" + date + getPickupCode(publishTaskInfoList);
         publishTaskInfo.setTaskNo(number);
-        String qrCode =qrCodeUtil.generateQRCodeImage(publishTaskInfo.getId()+","+number,800,800,"task",publishTaskInfo.getId());
+//        String qrCode =qrCodeUtil.generateQRCodeImage(publishTaskInfo.getId()+","+number,800,800,"task",publishTaskInfo.getId());
+        String tmp = "元/吨";
+        if("1".equals(publishTaskInfo.getBillingMethod())){
+            tmp = "元/车";
+        }
+        String qrCode =qrCodeUtil.generateQRCodeImageTask(publishTaskInfo.getId()+","+number,publishTaskInfo.getId(),publishTaskInfo.getSendArea()+" - "+publishTaskInfo.getUnloadArea()+"("+publishTaskInfo.getFreightPrice()+tmp+")");
         publishTaskInfo.setQrCode(qrCode);
         publishTaskInfo.setShowHide("1");
         publishTaskInfo.setStatus(StatusEnum.TASK_REVIEWING.getName());
@@ -1411,6 +1416,8 @@ public class PublishTaskInfoServiceImpl extends ServiceImpl<PublishTaskInfoMappe
         if (commonUser != null) {
             publishTaskInfo.setCargoOwnerPortrait(commonUser.getAvatarUrl());
         }
+//        String qrCode =qrCodeUtil.generateQRCodeImageTask(publishTaskInfo.getId()+",",publishTaskInfo.getId(),publishTaskInfo.getSendArea()+" - "+publishTaskInfo.getUnloadArea()+"("+publishTaskInfo.getFreightPrice()+")");
+//        System.out.println(qrCode);
         return publishTaskInfo;
     }
 

+ 154 - 3
winsea-haixin-plugin-wangluohuoyun/src/main/java/com/yh/saas/plugin/yiliangyiyun/util/QRCodeUtil.java

@@ -10,6 +10,7 @@ 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 com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
 import lombok.Getter;
 import org.apache.http.entity.ContentType;
 import org.springframework.beans.factory.InitializingBean;
@@ -17,15 +18,23 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.mock.web.MockMultipartFile;
 import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
 import org.springframework.web.multipart.MultipartFile;
+import sun.font.FontDesignMetrics;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.font.FontRenderContext;
+import java.awt.font.LineMetrics;
+import java.awt.image.BufferedImage;
+import java.io.*;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.FileSystems;
 import java.nio.file.Path;
+import java.util.HashMap;
 import java.util.Hashtable;
+import java.util.Properties;
+
 /**
  * @Author:chengchangjiang
  * @Description: 二维码Util类
@@ -45,6 +54,7 @@ public class QRCodeUtil implements InitializingBean {
     @Value("${oss.endpoint.default:}")
     private String endpoint;
 
+    private static final int QRCODE_SIZE = 320; // 二维码尺寸,宽度和高度均是320
     private String host;
     @Override
     public void afterPropertiesSet() throws Exception {
@@ -87,6 +97,147 @@ public class QRCodeUtil implements InitializingBean {
             return "error";
         }
     }
+    /***
+     * 生成二维码图片并上传OSS
+     * @param text 二维码内容
+     * @param name 图片名字
+     * @return
+     */
+    public  String generateQRCodeImageTask(String text,String name, String bottomText){
+        try {
+            HashMap<EncodeHintType, Object> hints = new HashMap<>();
+            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");    //定义内容字符集的编码
+            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);        //定义纠错等级
+            hints.put(EncodeHintType.MARGIN, 1);
+            QRCodeWriter qrCodeWriter = new QRCodeWriter();
+            BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
+
+            int width = bitMatrix.getWidth();
+            int height = bitMatrix.getHeight();
+            int tempHeight = height;
+            if (StringUtils.hasText(bottomText)) {
+                tempHeight = tempHeight + 12;
+            }
+            BufferedImage image = new BufferedImage(width, tempHeight, BufferedImage.TYPE_INT_RGB);
+            for (int x = 0; x < width; x++) {
+                for (int y = 0; y < height; y++) {
+                    image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
+                }
+            }
+            // 判断是否添加底部文字
+            if (StringUtils.hasText(bottomText)) {
+                addFontImage(image, bottomText);
+            }
+
+            File file = new File( new String(("templates" + File.separator + name+".png").getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
+            if(!file.exists()){
+                boolean flag = file.mkdirs();
+            }
+            Path path = FileSystems.getDefault().getPath(file.getAbsoluteFile().getPath());
+
+//        ByteArrayOutputStream out = new ByteArrayOutputStream();
+//        ImageIO.write(image,"png",path);
+//        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
+
+            //创建一个ByteArrayOutputStream
+            ByteArrayOutputStream os = new ByteArrayOutputStream();
+            //把BufferedImage写入ByteArrayOutputStream
+            ImageIO.write(image, "png", os);
+            //ByteArrayOutputStream转成InputStream
+            InputStream input = new ByteArrayInputStream(os.toByteArray());
+            //InputStream转成MultipartFile
+            MultipartFile multipartFile =new MockMultipartFile("file", "file.jpg", "text/plain", input);
+
+//        FileInputStream fileInputStream = new FileInputStream(file.getAbsoluteFile());
+//        MultipartFile multipartFile = new MockMultipartFile( ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
+            String result = upload(multipartFile,name);
+            file.delete();
+            return result;
+
+//
+//
+//            QRCodeWriter qrCodeWriter = new QRCodeWriter();
+//            Hashtable<EncodeHintType, Object> hints = new Hashtable();
+//            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
+//            BitMatrix bitMatrix = qrCodeWriter.encode(type+","+text, BarcodeFormat.QR_CODE, width, height,hints);
+//
+//            File file = new File( new String(("templates" + File.separator + name+".png").getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
+//            if(!file.exists()){
+//                boolean flag = file.mkdirs();
+//            }
+//            Path path = FileSystems.getDefault().getPath(file.getAbsoluteFile().getPath());
+//
+//            MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
+//
+//            FileInputStream fileInputStream = new FileInputStream(file.getAbsoluteFile());
+//
+//            MultipartFile multipartFile = new MockMultipartFile( ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
+//            String result = upload(multipartFile,name);
+//            file.delete();
+//            return result;
+        }
+        catch (Exception e){
+            System.out.println("二维码异常:"+e.toString());
+            return "error";
+        }
+    }
+    private static void addFontImage(BufferedImage source, String declareText) {
+        //生成image
+        int defineWidth = QRCODE_SIZE;
+        int defineHeight = 20;
+        BufferedImage textImage = new BufferedImage(defineWidth, defineHeight, BufferedImage.TYPE_INT_RGB);
+        Graphics2D g2 = (Graphics2D) textImage.getGraphics();
+        //开启文字抗锯齿
+        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,   RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+        g2.setBackground(Color.WHITE);
+        g2.clearRect(0, 0, defineWidth, defineHeight);
+        g2.setPaint(Color.BLACK);
+        FontRenderContext context = g2.getFontRenderContext();
+        String font_cn = getChineseFont();
+        //部署linux需要注意 linux无此字体会显示方块
+        Font font = new Font("宋体", Font.BOLD, 18);
+
+        g2.setFont(font);
+        LineMetrics lineMetrics = font.getLineMetrics(declareText, context);
+        FontMetrics fontMetrics = FontDesignMetrics.getMetrics(font);
+        float offset = (defineWidth - fontMetrics.stringWidth(declareText)) / 2;
+        float y = (defineHeight + lineMetrics.getAscent() - lineMetrics.getDescent() - lineMetrics.getLeading()) / 2;
+        g2.drawString(declareText, (int) offset, (int) y);
+
+        Graphics2D graph = source.createGraphics();
+        //开启文字抗锯齿
+        graph.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,   RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+        //添加image
+        int width = textImage.getWidth(null);
+        int height = textImage.getHeight(null);
+
+        Image src = textImage;
+        graph.drawImage(src, 0, QRCODE_SIZE - 8, width, height, Color.WHITE, null);
+        graph.dispose();
+    }
+    /**
+     * 获取中文字体位置
+     *
+     * @return
+     * @author xxj 2017年4月28日
+     */
+    private static String getChineseFont() {
+
+        //宋体(对应css中的 属性 font-family: SimSun; /*宋体*/)
+        String font1 = "templates" + File.separator + "msyh.ttc";
+//        String font1 = "templates" + File.separator + "simsun.ttf";
+        //判断系统类型,加载字体文件
+        Properties prop = System.getProperties();
+        String osName = prop.getProperty("os.name").toLowerCase();
+        System.out.println(osName);
+        if (osName.indexOf("linux") > -1) {
+            font1 = "/usr/share/fonts/chinese/simsun.ttc";
+        }
+        else{
+            font1 = "宋体";
+        }
+        return font1;
+    }
     /**
      * 后台通过服务器间接传文件
      * @param file