Эх сурвалжийг харах

美术阅卷10月新增需求-分数图片优化

wangliang 5 жил өмнө
parent
commit
81682a1ab1

+ 8 - 4
stmms-ms-admin/src/main/java/cn/com/qmth/stmms/ms/admin/api/ScoreApi.java

@@ -237,7 +237,7 @@ public class ScoreApi {
 
     @PostMapping("/watermark")
     public void watermark(@RequestParam("workId") Long workId) {
-
+        watermarking = false;
         if (!watermarking) {
             watermarking = true;
             watermarkTotal = 0;
@@ -254,14 +254,17 @@ public class ScoreApi {
                     watermarkFinished = i + 1;
                     Paper paper = papers.get(i);
                     Double score = paper.getInspectScore() == null ? paper.getScore() : paper.getInspectScore();
+                    if (Objects.isNull(score)) {
+                        continue;
+                    }
                     String examNumber = paper.getExamNumber();
                     String areaCode = paper.getAreaCode();
                     Subject subject = paper.getSubject();
                     Student student = studentRepo.findByWorkIdAndExamNumber(workId, examNumber);
                     String imageMd5 = MD5Util.getImageRuleMd5(workId, subject.ordinal(), areaCode, examNumber, student.getId());
 
-                    String imageFile = imagePath + "/" + workId + "/" + subject.toString() + "/" + areaCode + "/" + imageMd5 + ".jpg";
-                    String watermarkFile = watermark + "/" + workId + "/" + subject.toString() + "/" + areaCode + "/" + imageMd5 + ".jpg";
+                    String imageFile = imagePath + "/" + workId + "/" + subject.toString() + "/" + areaCode + "/" + imageMd5;
+                    String watermarkFile = watermark + "/" + workId + "/" + subject.toString() + "/" + areaCode + "/" + imageMd5;
 
                     File targetFile = new File(watermarkFile);
                     targetFile.getParentFile().mkdirs();//创建父级目录
@@ -269,7 +272,8 @@ public class ScoreApi {
                     String text = new String(txt.getBytes(), Charset.forName("UTF-8"));
 //                    WaterMarkUtils.addWaterMark(imageFile, watermarkFile, text, color, font);
                     try {
-                        WaterMarkUtils.addTextWatermark(text, "jpg", imageFile, watermarkFile);
+//                        WaterMarkUtils.addTextWatermark(text, "jpg", imageFile, watermarkFile);
+                        WaterMarkUtils.addTextWatermarkNew(imageMd5, text, "jpg", imageFile, watermarkFile);
                         System.out.println(watermarkFile + "生成成功");
                     } catch (IOException e) {
                         System.out.println(watermarkFile + "生成失败");

+ 1 - 1
stmms-ms-admin/src/main/java/cn/com/qmth/stmms/ms/admin/service/DataUploadService.java

@@ -121,7 +121,7 @@ public class DataUploadService {
 //            outputStreamTemp = new FileOutputStream(thumbFileTemp);
 //            ImageIO.write(bufferedImage, "jpg", outputStreamTemp);
             ByteArrayOutputStream os = new ByteArrayOutputStream();
-            ImageIO.write(bufferedImage, "gif", os);
+            ImageIO.write(bufferedImage, "jpg", os);
             in = new ByteArrayInputStream(os.toByteArray());
 
             //原图删除

+ 63 - 3
stmms-ms-admin/src/main/java/cn/com/qmth/stmms/ms/admin/utils/WaterMarkUtils.java

@@ -1,7 +1,6 @@
 package cn.com.qmth.stmms.ms.admin.utils;
 
 import cn.com.qmth.stmms.ms.commons.constant.SystemConstant;
-import cn.com.qmth.stmms.ms.commons.utils.MD5Util;
 import org.slf4j.LoggerFactory;
 
 import javax.imageio.IIOImage;
@@ -10,8 +9,6 @@ import javax.imageio.ImageWriteParam;
 import javax.imageio.ImageWriter;
 import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
 import javax.imageio.stream.FileImageOutputStream;
-import javax.imageio.stream.ImageInputStream;
-import javax.imageio.stream.ImageOutputStream;
 import java.awt.*;
 import java.awt.geom.Rectangle2D;
 import java.awt.image.BufferedImage;
@@ -131,6 +128,69 @@ public class WaterMarkUtils {
         return g.getFontMetrics(g.getFont()).getHeight();
     }
 
+    /**
+     * 生成水印图(新)
+     *
+     * @param md5
+     * @param text
+     * @param type
+     * @param sourcePath
+     * @param destinationPath
+     * @throws IOException
+     */
+    public static void addTextWatermarkNew(String md5, String text, String type, String sourcePath, String destinationPath) throws IOException {
+        long start = System.currentTimeMillis();
+        LOGGER.info("准备生成水印图:{}", start);
+        String sourceTempPath = sourcePath + "temp.jpg";
+        //读取指定路径下面的文件
+        InputStream inputStream = new FileInputStream(sourcePath + ".jpg");
+        OutputStream outputStream = new FileOutputStream(sourceTempPath);
+        SystemConstant.writeStream(inputStream, outputStream);
+        File source = new File(sourceTempPath);
+        File destinationTemp = new File(destinationPath + "temp.jpg");
+        BufferedImage image = ImageIO.read(source);
+        int width = image.getWidth();
+        int height = image.getHeight();
+
+        BufferedImage watermarked = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
+
+        Graphics2D w = (Graphics2D) watermarked.getGraphics();
+        w.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+        w.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+
+        w.drawImage(image.getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_SMOOTH), 0, 0, null);
+        AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f);
+        w.setComposite(alphaChannel);
+        w.setColor(Color.red);
+        w.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 60));
+        FontMetrics fontMetrics = w.getFontMetrics();
+        Rectangle2D rect = fontMetrics.getStringBounds(text, w);
+
+        int x = 0;
+        int y = image.getHeight() - (int) rect.getHeight() + 50;
+
+        w.drawString(text, x, y);
+
+        JPEGImageWriteParam jepgParams = new JPEGImageWriteParam(null);
+        jepgParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
+        jepgParams.setCompressionQuality(1f);
+        final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
+        writer.setOutput(new FileImageOutputStream(destinationTemp));
+        writer.write(null, new IIOImage(watermarked, null, null), jepgParams);
+        writer.dispose();
+        w.dispose();
+        source.delete();
+
+        inputStream = new FileInputStream(destinationTemp);
+        destinationTemp.delete();
+        outputStream.flush();
+        File watermarkFile = new File(destinationPath + ".jpg");
+        outputStream = new FileOutputStream(watermarkFile);
+        SystemConstant.writeStream(inputStream, outputStream);
+        long end = System.currentTimeMillis();
+        LOGGER.info("生成水印图耗时:{}", (end - start) / 1000 + "s");
+    }
+
     public static void main(String[] args) throws IOException {
 //        Font font = new Font("Symbol", Font.PLAIN, 60);                     //水印字体
         long start = System.currentTimeMillis();

+ 7 - 30
stmms-ms-collect/src/main/java/cn/com/qmth/stmms/ms/collect/api/CollectApi.java

@@ -141,7 +141,7 @@ public class CollectApi {
         return list;
     }
 
-    File srcFile = new File("/Users/king/stmms-ms/static/images/6/SM/1/1901040150.jpg"); //初始文件
+    File srcFile = new File("/Users/king/stmms-ms/static/images/1/SM/1/1901130045.jpg"); //初始文件
 
     /**
      * 上传原图(图片加密)
@@ -179,7 +179,7 @@ public class CollectApi {
             File sheetFile = new File(sheetDir + File.separator + imageMd5 + ".jpg");
             LOGGER.info("sheet存放目录:{}", sheetFile.getPath());
             outputStream = new FileOutputStream(sheetFile);
-            writeStream(inputStream, outputStream);
+            SystemConstant.writeStream(inputStream, outputStream);
             long end = System.currentTimeMillis();
             LOGGER.info("生成原图耗时:{}", (end - start) / 1000 + "s");
 
@@ -241,8 +241,8 @@ public class CollectApi {
             outputStream = new FileOutputStream(imageFile);
             outputStreamTemp = new FileOutputStream(imageFileTemp);
             int index = 0;
-            byte[] bytes = new byte[1024];
-            byte[] bytesEnc = new byte[1024];
+            byte[] bytes = new byte[1024 * 8];
+            byte[] bytesEnc = new byte[1024 * 8];
             while ((index = inputStream.read(bytes)) != -1) {
                 //将字节数组的数据全部写入到输出流中
                 for (int i = 0; i < index; i++) {
@@ -269,7 +269,7 @@ public class CollectApi {
 //            outputStreamTemp = new FileOutputStream(thumbFileTemp);
 //            ImageIO.write(bufferedImage, "jpg", outputStreamTemp);
             ByteArrayOutputStream os = new ByteArrayOutputStream();
-            ImageIO.write(bufferedImage, "gif", os);
+            ImageIO.write(bufferedImage, "jpg", os);
             inputStream = new ByteArrayInputStream(os.toByteArray());
 
             //原图删除
@@ -277,7 +277,7 @@ public class CollectApi {
 //            inputStream = PictureUtil.getInput(thumbFileTemp);
             //缩略图删除
 //            thumbFileTemp.delete();
-            writeStream(inputStream, outputStream);
+            SystemConstant.writeStream(inputStream, outputStream);
             long end = System.currentTimeMillis();
             LOGGER.info("生成原图和缩略图耗时:{}", (end - start) / 1000 + "s");
 
@@ -355,7 +355,7 @@ public class CollectApi {
             //读取指定路径下面的文件
             inputStream = new FileInputStream(file);
             outputStream = new BufferedOutputStream(response.getOutputStream());
-            writeStream(inputStream, outputStream);
+            SystemConstant.writeStream(inputStream, outputStream);
             long end = System.currentTimeMillis();
             LOGGER.info("读取图片耗时:{}", (end - start) / 1000 + "s");
         } catch (Exception e) {
@@ -471,27 +471,4 @@ public class CollectApi {
         markSubjectRepo.save(markSubject);
         return true;
     }
-
-    /**
-     * 流写入
-     *
-     * @param inputStream
-     * @param outputStream
-     * @return
-     * @throws IOException
-     */
-    public OutputStream writeStream(InputStream inputStream, OutputStream outputStream) throws IOException {
-        int index = 0;
-        byte[] bytes = new byte[1024];
-        byte[] bytesEnc = new byte[1024];
-        while ((index = inputStream.read(bytes)) != -1) {
-            //将字节数组的数据全部写入到输出流中
-            for (int i = 0; i < index; i++) {
-                //通过异或运算加密
-                bytesEnc[i] = (byte) (bytes[i] ^ SystemConstant.SECRET_KEY);
-            }
-            outputStream.write(bytesEnc, 0, index);
-        }
-        return outputStream;
-    }
 }

+ 27 - 0
stmms-ms-commons/src/main/java/cn/com/qmth/stmms/ms/commons/constant/SystemConstant.java

@@ -1,5 +1,9 @@
 package cn.com.qmth.stmms.ms.commons.constant;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
 /**
  * @Description: 系统常量
  * @Param:
@@ -13,4 +17,27 @@ public class SystemConstant {
             AES_MODE = "AES/CBC/PKCS5Padding";//用这个模式,规则必须为16位
     //    public static final String AES_RULE = "0102030405060708";
     public static final int SECRET_KEY = 0x99;//加密解密秘钥
+
+    /**
+     * 流写入
+     *
+     * @param inputStream
+     * @param outputStream
+     * @return
+     * @throws IOException
+     */
+    public static OutputStream writeStream(InputStream inputStream, OutputStream outputStream) throws IOException {
+        int index = 0;
+        byte[] bytes = new byte[1024 * 8];
+        byte[] bytesEnc = new byte[1024 * 8];
+        while ((index = inputStream.read(bytes)) != -1) {
+            //将字节数组的数据全部写入到输出流中
+            for (int i = 0; i < index; i++) {
+                //通过异或运算加密
+                bytesEnc[i] = (byte) (bytes[i] ^ SystemConstant.SECRET_KEY);
+            }
+            outputStream.write(bytesEnc, 0, index);
+        }
+        return outputStream;
+    }
 }