Browse Source

新增图片遮盖、图片裁切、轨迹重写等工具

yin 6 months ago
parent
commit
656b8c561d

+ 26 - 0
pom.xml

@@ -0,0 +1,26 @@
+package com.qmth.test.image;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.springframework.util.FileCopyUtils;
+
+public class ImageTest {
+
+    public static void main(String[] args) throws IOException {
+        File file = new File("/Users/ting.yin/Desktop/121");
+        int i = 1;
+        for (File f : file.listFiles()) {
+            if (f.isDirectory()) {
+                for (File image : f.listFiles()) {
+                    if (image.isFile()) {
+                        String name = "/Users/ting.yin/Desktop/121/" + i + ".jpg";
+                        File newImage = new File(name);
+                        FileCopyUtils.copy(image, newImage);
+                        i++;
+                    }
+                }
+            }
+        }
+    }
+}

+ 94 - 0
src/main/java/com/qmth/test/image/SheetCover.java

@@ -0,0 +1,94 @@
+package com.qmth.test.image;
+
+import java.io.*;
+import java.util.*;
+
+import org.apache.commons.dbcp.BasicDataSource;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.support.rowset.SqlRowSet;
+
+import com.qmth.test.image.model.Coordinate;
+import com.qmth.test.image.model.Student;
+import com.qmth.test.image.utils.CoverUtil;
+
+public class SheetCover {
+
+    public static Properties config = new Properties();
+
+    private static JdbcTemplate template;
+
+    public static List<Coordinate> coordinateList = new LinkedList<Coordinate>();
+
+    public static Map<String, Student> studentMap = new HashMap<>();
+
+    public static void main(String[] args) throws IOException {
+         init(new FileInputStream(args[0]));
+//        init(SheetTrack.class.getClassLoader().getResourceAsStream("cover.properties"));
+
+        int examId = Integer.parseInt(config.getProperty("examId"));
+        loadCoordinate(config.getProperty("cover.config"));
+        loadStudent(examId, config.getProperty("student.file"));
+        System.out.println("examId=" + examId + ", student count=" + studentMap.size());
+        startCover(config.getProperty("sheet.path"), config.getProperty("output.path"));
+    }
+
+    private static void startCover(String sheetPath, String outPath) throws IOException {
+        int count = 0;
+        for (Student s : studentMap.values()) {
+            coverStudent(s, sheetPath, outPath);
+            count++;
+        }
+        System.out.println(count + " student cover finish");
+    }
+
+    public static void coverStudent(Student s, String sheetRoot, String sliceRoot) throws IOException {
+        for (Coordinate c : coordinateList) {
+            String examNumberEnd3 = s.examNumber.substring(s.examNumber.length() - 3, s.examNumber.length());
+            File sheetFile = new File(new File(new File(sheetRoot, String.valueOf(s.examId)), examNumberEnd3),
+                    s.examNumber + "-" + c.index + ".jpg");
+            String secretNumberEnd3 = s.secretNumber.substring(s.secretNumber.length() - 3, s.secretNumber.length());
+            File sliceFile = new File(new File(new File(sliceRoot, String.valueOf(s.examId)), secretNumberEnd3),
+                    s.secretNumber + "-" + c.index + ".jpg");
+            sliceFile.getParentFile().mkdirs();
+            CoverUtil.coverImage(sheetFile, sliceFile, c.top, c.left, c.width, c.height);
+            System.out.println(sheetFile.getAbsolutePath() + " -> " + sliceFile.getAbsolutePath());
+        }
+    }
+
+    private static void loadStudent(int examId, String file) throws IOException {
+        BufferedReader reader = new BufferedReader(new FileReader(file));
+        String line = null;
+        while ((line = reader.readLine()) != null) {
+            SqlRowSet rs = template.queryForRowSet(
+                    "select s.id,s.exam_number,s.sheet_count,s.secret_number "
+                            + "from eb_exam_student s where s.exam_id=? "
+                            + "and s.is_upload=1 and s.is_absent=0 and s.is_breach=0 " + "and s.exam_number=? ",
+                    examId, line);
+            while (rs.next()) {
+                Student s = new Student();
+                s.examId = examId;
+                s.examNumber = rs.getString("exam_number");
+                s.sheetCount = rs.getInt("sheet_count");
+                s.secretNumber = rs.getString("secret_number");
+                studentMap.put(s.examNumber, s);
+            }
+        }
+        reader.close();
+    }
+
+    private static void loadCoordinate(String config) {
+        coordinateList = Coordinate.build(config);
+    }
+
+    private static void init(InputStream ins) throws IOException {
+        config.load(ins);
+
+        BasicDataSource ds = new BasicDataSource();
+        ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
+        ds.setUrl(config.getProperty("mysql.url"));
+        ds.setUsername(config.getProperty("mysql.username"));
+        ds.setPassword(config.getProperty("mysql.password"));
+
+        template = new JdbcTemplate(ds);
+    }
+}

+ 97 - 39
src/main/java/com/qmth/test/image/SheetTrack.java

@@ -1,13 +1,5 @@
 package com.qmth.test.image;
 
-import com.qmth.test.image.model.*;
-import com.qmth.test.image.model.Image;
-import org.apache.commons.dbcp.BasicDataSource;
-import org.apache.commons.lang.StringUtils;
-import org.springframework.jdbc.core.JdbcTemplate;
-import org.springframework.jdbc.support.rowset.SqlRowSet;
-
-import javax.imageio.ImageIO;
 import java.awt.*;
 import java.awt.image.BufferedImage;
 import java.io.File;
@@ -18,6 +10,16 @@ import java.text.DecimalFormat;
 import java.util.*;
 import java.util.List;
 
+import javax.imageio.ImageIO;
+
+import org.apache.commons.dbcp.BasicDataSource;
+import org.apache.commons.lang.StringUtils;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.support.rowset.SqlRowSet;
+
+import com.qmth.test.image.model.*;
+import com.qmth.test.image.model.Image;
+
 public class SheetTrack {
 
     private static Properties p = new Properties();
@@ -36,34 +38,85 @@ public class SheetTrack {
 
         int examId = Integer.parseInt(p.getProperty("examId"));
         Set<String> subjects = findSubjectCode(examId);
-        System.out.println("examId=" + examId + ", subject count=" + subjects.size());
+        Set<String> examNumbers = findExamNumbers();
+        System.out.println("examId=" + examId + ", subject count=" + subjects.size() + ", student count="
+                + examNumbers.size());
         for (String subjectCode : subjects) {
-            process(examId, subjectCode);
+            for (String examNumber : examNumbers) {
+                processStudent(examId, subjectCode, examNumber);
+            }
         }
     }
 
-    private static void process(int examId, String subjectCode) throws IOException {
+    // private static void process(int examId, String subjectCode) throws
+    // IOException {
+    // List<Integer> groups = new ArrayList<>();
+    // List<Question> questions = new ArrayList<>();
+    // buildGroupQuestion(examId, subjectCode, groups, questions);
+    //
+    // int totalCount = 0;
+    // SqlRowSet crs = template
+    // .queryForRowSet(
+    // "select count(*) as count from eb_exam_student where exam_id=? and subject_code=? and is_upload=1 and is_absent=0 and is_breach=0",
+    // examId, subjectCode);
+    // while (crs.next()) {
+    // totalCount = crs.getInt("count");
+    // }
+    //
+    // int count = 0;
+    // SqlRowSet rs = template
+    // .queryForRowSet(
+    // "select s.id,b.id as campus_id,s.exam_number,s.sheet_count,s.slice_count,s.subjective_score,s.subjective_score_list "
+    // +
+    // "from eb_exam_student s,b_campus b where s.school_id=b.school_id and s.campus_name=b.name and s.exam_id=? "
+    // +
+    // "and s.subject_code=? and s.is_upload=1 and s.is_absent=0 and s.is_breach=0 "
+    // // + "and s.exam_number='402414432'"
+    // , examId, subjectCode);
+    // while (rs.next()) {
+    // Student s = new Student();
+    // s.examId = examId;
+    // s.subjectCode = subjectCode;
+    // s.id = rs.getInt("id");
+    // s.campusId = rs.getInt("campus_id");
+    // s.examNumber = rs.getString("exam_number");
+    // s.sheetCount = rs.getInt("sheet_count");
+    // s.sliceCount = rs.getInt("slice_count");
+    // s.score = rs.getDouble("subjective_score");
+    // s.scoreDetail = rs.getString("subjective_score_list");
+    // s.scoreList = s.getScoreList();
+    // s.groups = groups;
+    // s.questions = questions;
+    //
+    // List<Tag> tags = new LinkedList<>();
+    // for (Integer groupNumber : groups) {
+    // tags.addAll(buildGroupTag(s, groupNumber, questions));
+    // }
+    // processSheet(p.getProperty("output.path"), s, transform(s, tags));
+    //
+    // count++;
+    // // if (count % 10 == 0) {
+    // System.out.print("\r" + "examId=" + examId + ", subjectCode=" +
+    // subjectCode + ": " + count + "/"
+    // + totalCount);
+    // // }
+    // }
+    // System.out.print("\r" + "examId=" + examId + ", subjectCode=" +
+    // subjectCode + ": " + count + "/" + totalCount);
+    // }
+
+    private static void processStudent(int examId, String subjectCode, String examNumber) throws IOException {
         List<Integer> groups = new ArrayList<>();
         List<Question> questions = new ArrayList<>();
         buildGroupQuestion(examId, subjectCode, groups, questions);
 
-        int totalCount = 0;
-        SqlRowSet crs = template
-                .queryForRowSet(
-                        "select count(*) as count from eb_exam_student where exam_id=? and subject_code=? and is_upload=1 and is_absent=0 and is_breach=0",
-                        examId, subjectCode);
-        while (crs.next()) {
-            totalCount = crs.getInt("count");
-        }
-
         int count = 0;
         SqlRowSet rs = template
                 .queryForRowSet(
                         "select s.id,b.id as campus_id,s.exam_number,s.sheet_count,s.slice_count,s.subjective_score,s.subjective_score_list "
                                 + "from eb_exam_student s,b_campus b where s.school_id=b.school_id and s.campus_name=b.name and s.exam_id=? "
                                 + "and s.subject_code=? and s.is_upload=1 and s.is_absent=0 and s.is_breach=0 "
-                        // + "and s.exam_number='402414432'"
-                        , examId, subjectCode);
+                                + "and s.exam_number=? ", examId, subjectCode, examNumber);
         while (rs.next()) {
             Student s = new Student();
             s.examId = examId;
@@ -78,20 +131,16 @@ public class SheetTrack {
             s.scoreList = s.getScoreList();
             s.groups = groups;
             s.questions = questions;
-
+            s.markerName = new HashMap<Integer, String>();
             List<Tag> tags = new LinkedList<>();
             for (Integer groupNumber : groups) {
                 tags.addAll(buildGroupTag(s, groupNumber, questions));
             }
-            processSheet(p.getProperty("output.path"), s, transform(s, tags));
+            processSheet(p.getProperty("output.path"), s, new ArrayList<Track>());
 
             count++;
-            // if (count % 10 == 0) {
-            System.out.print("\r" + "examId=" + examId + ", subjectCode=" + subjectCode + ": " + count + "/"
-                    + totalCount);
-            // }
         }
-        System.out.print("\r" + "examId=" + examId + ", subjectCode=" + subjectCode + ": " + count + "/" + totalCount);
+        System.out.print("\r" + "examId=" + examId + ", subjectCode=" + subjectCode + ": " + count);
     }
 
     private static void processSheet(String outputPath, Student s, List<Track> tracks) throws IOException {
@@ -143,7 +192,8 @@ public class SheetTrack {
                 }
                 Question q = s.questions.get(i);
                 double score = s.scoreList.get(i);
-                String content = q.mainNumber + "-" + q.subNumber + ":" + format.format(score);
+                String content = q.mainNumber + "-" + q.subNumber + ":" + format.format(score) + " "
+                        + s.markerName.get(q.groupNumber);
                 maxWidth = Math.max(maxWidth, content.length() * fontSize);
                 g.drawString(content, left, top);
             }
@@ -223,21 +273,20 @@ public class SheetTrack {
                 totalScore += s.scoreList.get(i);
             }
         }
+
         return findTags(findLibrary(s, groupNumber, totalScore));
     }
 
     private static Library findLibrary(Student s, int groupNumber, double totalScore) {
-        SqlRowSet rs = template.queryForRowSet(
-                "select id,marker_score from m_library where student_id=? and group_number=?", s.id, groupNumber);
+        SqlRowSet rs = template
+                .queryForRowSet(
+                        "select m.name from eb_marker m where m.id in (select marker_id from m_library where student_id=? and group_number=?)",
+                        s.id, groupNumber);
+        String name = "";
         while (rs.next()) {
-            double score = rs.getDouble("marker_score");
-            if (score == totalScore) {
-                Library library = new Library();
-                library.id = rs.getInt("id");
-                library.markerScore = score;
-                return library;
-            }
+            name = name + "," + rs.getString("name");
         }
+        s.markerName.put(groupNumber, name);
         return null;
     }
 
@@ -300,6 +349,15 @@ public class SheetTrack {
         return subjects;
     }
 
+    private static Set<String> findExamNumbers() {
+        String config = p.getProperty("examNumbers");
+        Set<String> examNumbers = new HashSet<>();
+        if (StringUtils.isNotBlank(config)) {
+            Collections.addAll(examNumbers, config.split(","));
+        }
+        return examNumbers;
+    }
+
     private static void init(InputStream ins) throws IOException {
         p.load(ins);
 

+ 70 - 0
src/main/java/com/qmth/test/image/SheetTrackFix.java

@@ -0,0 +1,70 @@
+package com.qmth.test.image;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.text.DecimalFormat;
+
+public class SheetTrackFix {
+
+    private static DecimalFormat format = new DecimalFormat("###.#");
+
+    public static void main(String[] args) throws IOException {
+        File file = new File("/Users/ting.yin/Downloads/237.txt");
+        BufferedReader reader = null;
+        Integer width = 2457;
+        Integer height = 1755;
+        BufferedWriter writer = null;
+        try {
+            reader = new BufferedReader(new FileReader(file));
+            writer = new BufferedWriter(new FileWriter("/Users/ting.yin/Downloads/237_new.txt"));
+            String tempStr;
+            while ((tempStr = reader.readLine()) != null) {
+                StringBuffer sbf = new StringBuffer();
+                String[] ss = tempStr.split(",");
+                Integer id = Integer.parseInt(ss[0]);
+                Double positionX = Double.parseDouble(ss[3]);
+                Double positionY = Double.parseDouble(ss[4]);
+                Integer index = Integer.parseInt(ss[5]);
+                Double offsetX = positionX * width * 0.55;
+                Double offsetY = positionY * height * 4;
+                if ((positionY > 0.25 && positionY < 0.5) || (positionY > 0.75 && positionY < 1)) {
+                    offsetX = offsetX + width * 0.45;
+                }
+                if ((positionY > 0.25 && positionY < 0.5)) {
+                    offsetY = offsetY - height;
+                }
+                if ((positionY > 0.5 && positionY < 0.75)) {
+                    offsetY = offsetY - height * 2;
+                }
+                if ((positionY > 0.75 && positionY < 1)) {
+                    offsetY = offsetY - height * 3;
+                }
+                if (positionY < 0.5) {
+                    index = 1;
+                } else {
+                    index = 2;
+                }
+                sbf.append(id).append(",").append(index).append(",").append(format.format(offsetX)).append(",")
+                        .append(format.format(offsetY));
+                writer.write(sbf.toString());
+                writer.newLine();
+            }
+            reader.close();
+            writer.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            if (reader != null) {
+                try {
+                    reader.close();
+                } catch (IOException e1) {
+                    e1.printStackTrace();
+                }
+            }
+        }
+    }
+}

+ 62 - 0
src/main/java/com/qmth/test/image/SheetTrackFixGroup.java

@@ -0,0 +1,62 @@
+package com.qmth.test.image;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.text.DecimalFormat;
+
+public class SheetTrackFixGroup {
+
+    private static DecimalFormat format = new DecimalFormat("###.#");
+
+    public static void main(String[] args) throws IOException {
+        File file = new File("/Users/ting.yin/Downloads/226-2.txt");
+        BufferedReader reader = null;
+        Integer width = 1273;
+        Integer height = 2803;
+        BufferedWriter writer = null;
+        try {
+            reader = new BufferedReader(new FileReader(file));
+            writer = new BufferedWriter(new FileWriter("/Users/ting.yin/Downloads/226-2-new1.txt"));
+            String tempStr;
+            while ((tempStr = reader.readLine()) != null) {
+                StringBuffer sbf = new StringBuffer();
+                String[] ss = tempStr.split(",");
+                Integer id = Integer.parseInt(ss[0]);
+                Double positionX = Double.parseDouble(ss[1]);
+                Double positionY = Double.parseDouble(ss[2]);
+                Integer index = 1;
+                Double offsetX = positionX * width;
+                Double offsetY = positionY * height;
+                double x = Double.valueOf(1070) / height;
+                if (positionY < x) {
+                    offsetX = offsetX + 1181;
+                    offsetY = offsetY + 684;
+                } else {
+                    offsetX = offsetX + 42;
+                    offsetY = offsetY + 21;
+                    index = 2;
+                }
+                sbf.append(id).append(",").append(positionX).append(",").append(positionY).append(",").append(index)
+                        .append(",").append(format.format(offsetX)).append(",").append(format.format(offsetY));
+                writer.write(sbf.toString());
+                writer.newLine();
+            }
+            reader.close();
+            writer.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            if (reader != null) {
+                try {
+                    reader.close();
+                } catch (IOException e1) {
+                    e1.printStackTrace();
+                }
+            }
+        }
+    }
+}

+ 9 - 4
src/main/java/com/qmth/test/image/model/Coordinate.java

@@ -1,13 +1,12 @@
 package com.qmth.test.image.model;
 
-import org.apache.commons.lang.StringUtils;
-
 import java.text.DecimalFormat;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.stream.Collectors;
+
+import org.apache.commons.lang.StringUtils;
 
 public class Student {
 
@@ -37,6 +36,10 @@ public class Student {
 
     public List<Question> questions;
 
+    public Map<Integer, String> markerName;
+
+    public String secretNumber;
+
     public List<Double> getScoreList() {
         List<Double> list = new ArrayList<>();
         String[] values = StringUtils.split(scoreDetail, ";");
@@ -68,7 +71,9 @@ public class Student {
                     content.append("+");
                 }
                 content.append(format.format(score.stream().mapToDouble(s -> s).sum()));
-                //content.append(score.stream().map(s -> format.format(s)).collect(Collectors.joining("+", "(", ")")));
+                // content.append(score.stream().map(s ->
+                // format.format(s)).collect(Collectors.joining("+", "(",
+                // ")")));
             }
         }
         return content.toString();

+ 40 - 0
src/main/java/com/qmth/test/image/model/Tag.java

@@ -0,0 +1,40 @@
+package com.qmth.test.image.utils;
+
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.util.regex.Pattern;
+
+import javax.imageio.ImageIO;
+
+public class CoverUtil {
+
+    public static void coverImage(File src, File dest, int x, int y, int w, int h) throws IOException {
+        try {
+            BufferedImage image = ImageIO.read(src);
+            Graphics2D g2d = image.createGraphics();
+            // 设置白色矩形框
+            g2d.setColor(Color.WHITE);
+            g2d.fillRect(x, y, w, h);
+            ImageIO.write(image, "jpg", dest);
+            g2d.dispose();
+        } catch (Exception e) {
+            throw new RuntimeException(src + ":" + x + "," + y + "," + w + "," + h + " crop error", e);
+        }
+
+    }
+
+    public static void main(String[] args) throws IOException {
+        Pattern pattern = Pattern.compile("(\\d+).jpg");
+        File input = new File("/Users/yin/Downloads/static/823");
+        File output = new File("/Users/yin/Downloads/static");
+
+        for (File file : input.listFiles()) {
+            if (file.isFile()) {
+                    coverImage(file, new File(output,   "111.jpg"), 1, 1, 1000, 2000);
+            }
+        }
+    }
+
+}

+ 8 - 0
src/main/java/com/qmth/test/image/utils/CropUtil.java

@@ -0,0 +1,8 @@
+mysql.url=jdbc:mysql://localhost:3309/stmms_ft?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT
+mysql.username=scan
+mysql.password=scan
+examId=1
+sheet.path=/Users/yin/Downloads/static/stmms-ft/sheet
+output.path=/Users/yin/Downloads/static/stmms-ft/out
+student.file=/Users/yin/Downloads/static/student.txt
+cover.config=1:100:100:500:500,3:100:100:500:500

+ 8 - 7
src/main/resources/crop.properties

@@ -1,10 +1,11 @@
-mysql.url=jdbc:mysql://localhost:3306/c?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT
+mysql.url=jdbc:mysql://localhost:3306/dbdx?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT
 mysql.username=root
 mysql.password=root
 
-examId=1
-subjects=432
-sheet.path=/Users/luoshi/test/sheet-track/sheet
-slice.path=/Users/luoshi/test/sheet-track/slice
-output.path=/Users/luoshi/test/sheet-track/output
-slice.config=
+examId=6
+subjects=
+sheet.path=/Users/ting.yin/work/static/ft-sheet
+slice.path=/Users/ting.yin/work/static/ft-slice
+output.path=/Users/ting.yin/work/static/output
+slice.config=
+examNumbers=2100001,2100002

+ 0 - 38
src/test/java/com/qmth/test/ImageDownload/AppTest.java

@@ -1,38 +0,0 @@
-package com.qmth.test.ImageDownload;
-
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-/**
- * Unit test for simple App.
- */
-public class AppTest 
-    extends TestCase
-{
-    /**
-     * Create the test case
-     *
-     * @param testName name of the test case
-     */
-    public AppTest( String testName )
-    {
-        super( testName );
-    }
-
-    /**
-     * @return the suite of tests being tested
-     */
-    public static Test suite()
-    {
-        return new TestSuite( AppTest.class );
-    }
-
-    /**
-     * Rigourous Test :-)
-     */
-    public void testApp()
-    {
-        assertTrue( true );
-    }
-}