xiatian hace 2 meses
padre
commit
73b19f7b9f

+ 5 - 5
src/main/java/cn/com/qmth/am/bean/TrackPosition.java

@@ -2,16 +2,16 @@ package cn.com.qmth.am.bean;
 
 public class TrackPosition {
 
-    private Double positionX;
+    private Integer offsetX;
 
     private Integer offsetIndex;
 
-    public Double getPositionX() {
-        return positionX;
+    public Integer getOffsetX() {
+        return offsetX;
     }
 
-    public void setPositionX(Double positionX) {
-        this.positionX = positionX;
+    public void setOffsetX(Integer offsetX) {
+        this.offsetX = offsetX;
     }
 
     public Integer getOffsetIndex() {

+ 2 - 28
src/main/java/cn/com/qmth/am/service/impl/StudentScoreServiceImpl.java

@@ -1,11 +1,8 @@
 package cn.com.qmth.am.service.impl;
 
-import java.awt.image.BufferedImage;
-import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Comparator;
@@ -16,8 +13,6 @@ import java.util.Map;
 import java.util.Set;
 import java.util.stream.Collectors;
 
-import javax.imageio.ImageIO;
-
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
@@ -206,7 +201,7 @@ public class StudentScoreServiceImpl extends ServiceImpl<StudentScoreDao, Studen
         Map<Integer, AnswerImageDto> answerImages = new LinkedHashMap<>();
         for (ImagePosition s : score.getImagePosition()) {
             AnswerImageDto sheet = getSheet(score, q, s.getPageIndex(), answerImages);
-            ImageSize is = getSize(sheet.getImage());
+            ImageSize is = ImageUtil.getSize(sheet.getImage());
             suff = sheet.getSuff();
             int x = 0;
             int y = 0;
@@ -248,27 +243,6 @@ public class StudentScoreServiceImpl extends ServiceImpl<StudentScoreDao, Studen
         }
     }
 
-    private ImageSize getSize(byte[] iamge) {
-        InputStream is = null;
-        try {
-            byte[] bytes = Arrays.copyOf(iamge, iamge.length);
-            is = new ByteArrayInputStream(bytes);
-            BufferedImage image = ImageIO.read(is);
-            int width = image.getWidth();
-            int height = image.getHeight();
-            return new ImageSize(width, height);
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        } finally {
-            if (is != null) {
-                try {
-                    is.close();
-                } catch (IOException e) {
-                }
-            }
-        }
-    }
-
     private AnswerImageDto getSheet(StudentScoreEntity score, QuestionEntity q, Integer pageIndex,
             Map<Integer, AnswerImageDto> answerImages) {
         AnswerImageDto ret = answerImages.get(pageIndex);
@@ -408,7 +382,7 @@ public class StudentScoreServiceImpl extends ServiceImpl<StudentScoreDao, Studen
         // System.out.println(score+d.substring(start,d.length()));
         // }
         // String[] items = d.split("\\[\\[[0-9](.[0-9]+){0,1}分\\]\\]");
-        String code = "23635173";
+        String code = "23635170";
         String s = "https://file.markingcloud.com/" + getMarkingCloudPath(725, getSuffix(code), code, 2, "jpg");
         System.out.println(s);
     }

+ 43 - 3
src/main/java/cn/com/qmth/am/service/impl/StudentServiceImpl.java

@@ -1,6 +1,7 @@
 package cn.com.qmth.am.service.impl;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -8,6 +9,7 @@ import java.util.Map;
 import java.util.Set;
 
 import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -15,8 +17,10 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import com.qmth.boot.core.exception.StatusException;
+import com.qmth.boot.tools.models.ByteArray;
 
 import cn.com.qmth.am.bean.ImagePosition;
+import cn.com.qmth.am.bean.ImageSize;
 import cn.com.qmth.am.bean.OcrDto;
 import cn.com.qmth.am.bean.StudentScoreVo;
 import cn.com.qmth.am.bean.StudentVo;
@@ -29,6 +33,7 @@ import cn.com.qmth.am.service.QuestionService;
 import cn.com.qmth.am.service.StmmsService;
 import cn.com.qmth.am.service.StudentScoreService;
 import cn.com.qmth.am.service.StudentService;
+import cn.com.qmth.am.utils.ImageUtil;
 
 @Service
 public class StudentServiceImpl implements StudentService {
@@ -143,19 +148,23 @@ public class StudentServiceImpl implements StudentService {
         }
         score.setMarkingScore(list.get(0));
         if (AnswerRangeType.TRACK.equals(q.getAnswerRangeType())) {
-            score.setIps(getImagePosition(score.getStudentId(), q.getMainNumber() + "." + q.getSubNumber()));
+            score.setIps(getImagePosition(q.getExamId(), score.getExamNumber(), score.getStudentId(),
+                    q.getMainNumber() + "." + q.getSubNumber()));
             studentScoreService.updateMarkingScoreAndTrack(score);
         } else {
             studentScoreService.updateMarkingScore(score);
         }
     }
 
-    private List<ImagePosition> getImagePosition(Long studentId, String questionNumber) {
+    private List<ImagePosition> getImagePosition(Long examId, String examNumber, Long studentId,
+            String questionNumber) {
         List<ImagePosition> ret = new ArrayList<>();
         List<TrackPosition> tps = stmmsService.getTrackPosition(studentId, questionNumber);
+        Map<Integer, Integer> answerImages = new HashMap<>();
         Set<String> set = new HashSet<>();
         for (TrackPosition tp : tps) {
-            Boolean left = tp.getPositionX() < 0.5;
+            Integer width = getWidth(examId, examNumber, tp.getOffsetIndex(), answerImages);
+            Boolean left = tp.getOffsetX() < (width / 2);
             String key = left.toString() + tp.getOffsetIndex();
             if (!set.contains(key)) {
                 ImagePosition ip = new ImagePosition();
@@ -166,4 +175,35 @@ public class StudentServiceImpl implements StudentService {
         }
         return ret;
     }
+
+    private Integer getWidth(Long examId, String examNumber, Integer pageIndex, Map<Integer, Integer> answerImages) {
+        Integer ret = answerImages.get(pageIndex);
+        if (ret != null) {
+            return ret;
+        }
+        String url = getImageUrl(examId, examNumber, pageIndex);
+        url = url.replace("https", "http");
+        try {
+            ImageSize is = ImageUtil.getSize(ByteArray.fromUrl(url).value());
+            answerImages.put(pageIndex, is.getWidth());
+            ret = is.getWidth();
+            return ret;
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private String getImageUrl(Long examId, String examNumber, Integer pageIndex) {
+        return sysProperty.getImageServer() + "/"
+                + getMarkingCloudPath(examId, getSuffix(examNumber), examNumber, pageIndex, "jpg");
+    }
+
+    private static String getSuffix(String input) {
+        return StringUtils.trimToEmpty(input).substring(Math.max(0, input.length() - 3));
+    }
+
+    private static String getMarkingCloudPath(Object... param) {
+        return String.format("sheet/%d/%s/%s-%d.%s", param);
+    }
+
 }

+ 376 - 329
src/main/java/cn/com/qmth/am/utils/ImageUtil.java

@@ -15,6 +15,7 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
@@ -25,352 +26,398 @@ import javax.imageio.ImageWriteParam;
 import javax.imageio.ImageWriter;
 import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
 
+import cn.com.qmth.am.bean.ImageSize;
+
 public final class ImageUtil {
 
-	/**
-	 * 图片水印
-	 * 
-	 * @param pressImg  水印图片
-	 * @param targetImg 目标图片
-	 * @param x         修正值 默认在中间
-	 * @param y         修正值 默认在中间
-	 * @param alpha     透明度
-	 */
-	public final static void pressImage(String pressImg, String targetImg, int x, int y, float alpha) {
-		try {
-			File img = new File(targetImg);
-			Image src = ImageIO.read(img);
-			int wideth = src.getWidth(null);
-			int height = src.getHeight(null);
-			BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
-			Graphics2D g = image.createGraphics();
-			g.drawImage(src, 0, 0, wideth, height, null);
-			// 水印文件
-			Image src_biao = ImageIO.read(new File(pressImg));
-			int wideth_biao = src_biao.getWidth(null);
-			int height_biao = src_biao.getHeight(null);
-			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
-			g.drawImage(src_biao, (wideth - wideth_biao) / 2, (height - height_biao) / 2, wideth_biao, height_biao,
-					null);
-			// 水印文件结束
-			g.dispose();
-			ImageIO.write((BufferedImage) image, "jpg", img);
-		} catch (Exception e) {
-			e.printStackTrace();
-		}
-	}
+    /**
+     * 图片水印
+     * 
+     * @param pressImg
+     *            水印图片
+     * @param targetImg
+     *            目标图片
+     * @param x
+     *            修正值 默认在中间
+     * @param y
+     *            修正值 默认在中间
+     * @param alpha
+     *            透明度
+     */
+    public final static void pressImage(String pressImg, String targetImg, int x, int y, float alpha) {
+        try {
+            File img = new File(targetImg);
+            Image src = ImageIO.read(img);
+            int wideth = src.getWidth(null);
+            int height = src.getHeight(null);
+            BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
+            Graphics2D g = image.createGraphics();
+            g.drawImage(src, 0, 0, wideth, height, null);
+            // 水印文件
+            Image src_biao = ImageIO.read(new File(pressImg));
+            int wideth_biao = src_biao.getWidth(null);
+            int height_biao = src_biao.getHeight(null);
+            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
+            g.drawImage(src_biao, (wideth - wideth_biao) / 2, (height - height_biao) / 2, wideth_biao, height_biao,
+                    null);
+            // 水印文件结束
+            g.dispose();
+            ImageIO.write((BufferedImage) image, "jpg", img);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
 
-	/**
-	 * 文字水印
-	 * 
-	 * @param pressText 水印文字
-	 * @param targetImg 目标图片
-	 * @param fontName  字体名称
-	 * @param fontStyle 字体样式
-	 * @param color     字体颜色
-	 * @param fontSize  字体大小
-	 * @param x         修正值
-	 * @param y         修正值
-	 * @param alpha     透明度
-	 */
-	public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color,
-			int fontSize, int x, int y, float alpha) {
-		try {
-			File img = new File(targetImg);
-			Image src = ImageIO.read(img);
-			int width = src.getWidth(null);
-			int height = src.getHeight(null);
-			BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
-			Graphics2D g = image.createGraphics();
-			g.drawImage(src, 0, 0, width, height, null);
-			g.setColor(color);
-			g.setFont(new Font(fontName, fontStyle, fontSize));
-			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
-			g.drawString(pressText, (width - (getLength(pressText) * fontSize)) / 2 + x, (height - fontSize) / 2 + y);
-			g.dispose();
-			ImageIO.write((BufferedImage) image, "jpg", img);
-		} catch (Exception e) {
-			e.printStackTrace();
-		}
-	}
+    /**
+     * 文字水印
+     * 
+     * @param pressText
+     *            水印文字
+     * @param targetImg
+     *            目标图片
+     * @param fontName
+     *            字体名称
+     * @param fontStyle
+     *            字体样式
+     * @param color
+     *            字体颜色
+     * @param fontSize
+     *            字体大小
+     * @param x
+     *            修正值
+     * @param y
+     *            修正值
+     * @param alpha
+     *            透明度
+     */
+    public static void pressText(String pressText, String targetImg, String fontName, int fontStyle, Color color,
+            int fontSize, int x, int y, float alpha) {
+        try {
+            File img = new File(targetImg);
+            Image src = ImageIO.read(img);
+            int width = src.getWidth(null);
+            int height = src.getHeight(null);
+            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+            Graphics2D g = image.createGraphics();
+            g.drawImage(src, 0, 0, width, height, null);
+            g.setColor(color);
+            g.setFont(new Font(fontName, fontStyle, fontSize));
+            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
+            g.drawString(pressText, (width - (getLength(pressText) * fontSize)) / 2 + x, (height - fontSize) / 2 + y);
+            g.dispose();
+            ImageIO.write((BufferedImage) image, "jpg", img);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
 
-	/**
-	 * 缩放
-	 * 
-	 * @param filePath    图片路径
-	 * @param newFileName 缩放完比图片路径
-	 * @param height      高度
-	 * @param width       宽度
-	 * @param bb          比例不对时是否需要补白
-	 */
-	@SuppressWarnings("static-access")
-	public static void resize(String filePath, String newFileName, int height, int width, String formatName,
-			boolean bb) {
-		try {
-			double ratio = 0.0; // 缩放比例
-			File f = new File(filePath);
-			BufferedImage bi = ImageIO.read(f);
-			Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
-			// 计算比例
-			if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
-				if (bi.getHeight() > bi.getWidth()) {
-					ratio = (new Integer(height)).doubleValue() / bi.getHeight();
-				} else {
-					ratio = (new Integer(width)).doubleValue() / bi.getWidth();
-				}
-				AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
-				itemp = op.filter(bi, null);
-			}
-			if (bb) {
-				BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
-				Graphics2D g = image.createGraphics();
-				g.setColor(Color.white);
-				g.fillRect(0, 0, width, height);
-				if (width == itemp.getWidth(null))
-					g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null),
-							itemp.getHeight(null), Color.white, null);
-				else
-					g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null),
-							itemp.getHeight(null), Color.white, null);
-				g.dispose();
-				itemp = image;
-			}
-			BufferedImage outbi = null;
-			if (itemp instanceof BufferedImage)
-				outbi = (BufferedImage) itemp;
-			else
-				outbi = convertImageToBuffer(itemp);
+    /**
+     * 缩放
+     * 
+     * @param filePath
+     *            图片路径
+     * @param newFileName
+     *            缩放完比图片路径
+     * @param height
+     *            高度
+     * @param width
+     *            宽度
+     * @param bb
+     *            比例不对时是否需要补白
+     */
+    @SuppressWarnings("static-access")
+    public static void resize(String filePath, String newFileName, int height, int width, String formatName,
+            boolean bb) {
+        try {
+            double ratio = 0.0; // 缩放比例
+            File f = new File(filePath);
+            BufferedImage bi = ImageIO.read(f);
+            Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
+            // 计算比例
+            if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
+                if (bi.getHeight() > bi.getWidth()) {
+                    ratio = (new Integer(height)).doubleValue() / bi.getHeight();
+                } else {
+                    ratio = (new Integer(width)).doubleValue() / bi.getWidth();
+                }
+                AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
+                itemp = op.filter(bi, null);
+            }
+            if (bb) {
+                BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+                Graphics2D g = image.createGraphics();
+                g.setColor(Color.white);
+                g.fillRect(0, 0, width, height);
+                if (width == itemp.getWidth(null))
+                    g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null),
+                            itemp.getHeight(null), Color.white, null);
+                else
+                    g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null),
+                            itemp.getHeight(null), Color.white, null);
+                g.dispose();
+                itemp = image;
+            }
+            BufferedImage outbi = null;
+            if (itemp instanceof BufferedImage)
+                outbi = (BufferedImage) itemp;
+            else
+                outbi = convertImageToBuffer(itemp);
 
-			File newFile = null;
-			if (newFileName == null || newFileName.length() == 0) {
-				newFile = f;
-			} else {
-				newFile = new File(newFileName);
-				if (!newFile.getParentFile().exists())
-					newFile.getParentFile().mkdirs();
-			}
-			ImageIO.write(outbi, formatName, newFile);
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
-	}
+            File newFile = null;
+            if (newFileName == null || newFileName.length() == 0) {
+                newFile = f;
+            } else {
+                newFile = new File(newFileName);
+                if (!newFile.getParentFile().exists())
+                    newFile.getParentFile().mkdirs();
+            }
+            ImageIO.write(outbi, formatName, newFile);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
 
-	/**
-	 * 对流缩放
-	 * 
-	 * @param input      图片流
-	 * @param formatName 图片格式
-	 * @param height     高度
-	 * @param width      宽度
-	 * @param bb         比例不对时是否需要补白
-	 */
-	@SuppressWarnings("static-access")
-	public static InputStream MemoryResize(InputStream input, String formatName, int height, int width, boolean bb) {
-		try {
-			double ratio = 0.0; // 缩放比例
-			BufferedImage bi = ImageIO.read(input);
-			Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
-			// 计算比例
-			if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
-				if (bi.getHeight() > bi.getWidth()) {
-					ratio = (new Integer(height)).doubleValue() / bi.getHeight();
-				} else {
-					ratio = (new Integer(width)).doubleValue() / bi.getWidth();
-				}
-				AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
-				itemp = op.filter(bi, null);
-			}
-			if (bb) {
-				BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
-				Graphics2D g = image.createGraphics();
-				g.setColor(Color.white);
-				g.fillRect(0, 0, width, height);
-				if (width == itemp.getWidth(null))
-					g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null),
-							itemp.getHeight(null), Color.white, null);
-				else
-					g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null),
-							itemp.getHeight(null), Color.white, null);
-				g.dispose();
-				itemp = image;
-			}
-			ByteArrayOutputStream output = new ByteArrayOutputStream();
+    /**
+     * 对流缩放
+     * 
+     * @param input
+     *            图片流
+     * @param formatName
+     *            图片格式
+     * @param height
+     *            高度
+     * @param width
+     *            宽度
+     * @param bb
+     *            比例不对时是否需要补白
+     */
+    @SuppressWarnings("static-access")
+    public static InputStream MemoryResize(InputStream input, String formatName, int height, int width, boolean bb) {
+        try {
+            double ratio = 0.0; // 缩放比例
+            BufferedImage bi = ImageIO.read(input);
+            Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
+            // 计算比例
+            if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
+                if (bi.getHeight() > bi.getWidth()) {
+                    ratio = (new Integer(height)).doubleValue() / bi.getHeight();
+                } else {
+                    ratio = (new Integer(width)).doubleValue() / bi.getWidth();
+                }
+                AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
+                itemp = op.filter(bi, null);
+            }
+            if (bb) {
+                BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+                Graphics2D g = image.createGraphics();
+                g.setColor(Color.white);
+                g.fillRect(0, 0, width, height);
+                if (width == itemp.getWidth(null))
+                    g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null),
+                            itemp.getHeight(null), Color.white, null);
+                else
+                    g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null),
+                            itemp.getHeight(null), Color.white, null);
+                g.dispose();
+                itemp = image;
+            }
+            ByteArrayOutputStream output = new ByteArrayOutputStream();
 
-			BufferedImage outbi = null;
-			if (itemp instanceof BufferedImage)
-				outbi = (BufferedImage) itemp;
-			else
-				outbi = convertImageToBuffer(itemp);
+            BufferedImage outbi = null;
+            if (itemp instanceof BufferedImage)
+                outbi = (BufferedImage) itemp;
+            else
+                outbi = convertImageToBuffer(itemp);
 
-			ImageIO.write(outbi, formatName, output);
-			byte[] buff = output.toByteArray();
-			return new ByteArrayInputStream(buff);
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
-		return null;
-	}
+            ImageIO.write(outbi, formatName, output);
+            byte[] buff = output.toByteArray();
+            return new ByteArrayInputStream(buff);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
 
+    public static byte[] cutImage(byte[] img, String suff, int x, int y, int width, int height) {
+        InputStream in = null;
+        BufferedImage bi = null;
+        ByteArrayOutputStream output = null;
+        try {
+            in = new ByteArrayInputStream(img);
+            bi = ImageIO.read(in);
+            height = Math.min(height, bi.getHeight());
+            width = Math.min(width, bi.getWidth());
+            if (height <= 0)
+                height = bi.getHeight();
+            if (width <= 0)
+                width = bi.getWidth();
+            x = Math.min(Math.max(0, x), bi.getWidth() - width);
+            y = Math.min(Math.max(0, y), bi.getHeight() - height);
 
-	public static byte[] cutImage(byte[] img, String suff, int x,int y, int width, int height) {
-		InputStream in = null;
-		BufferedImage bi = null;
-		ByteArrayOutputStream output = null;
-		try {
-			in = new ByteArrayInputStream(img);
-			bi = ImageIO.read(in);
-			height = Math.min(height, bi.getHeight());
-			width = Math.min(width, bi.getWidth());
-			if (height <= 0)
-				height = bi.getHeight();
-			if (width <= 0)
-				width = bi.getWidth();
-			x = Math.min(Math.max(0, x), bi.getWidth() - width);
-			y = Math.min(Math.max(0, y), bi.getHeight() - height);
+            BufferedImage bi_cropper = bi.getSubimage(x, y, width, height);
+            output = new ByteArrayOutputStream();
+            ImageIO.write(bi_cropper, suff, output);
+            byte[] ret = output.toByteArray();
+            return ret;
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        } finally {
+            try {
+                if (in != null) {
+                    in.close();
+                }
+            } catch (IOException e) {
+            }
+            try {
+                if (output != null) {
+                    output.close();
+                }
+            } catch (IOException e) {
+            }
+        }
+    }
 
-			BufferedImage bi_cropper = bi.getSubimage(x, y, width, height);
-			output = new ByteArrayOutputStream();
-			ImageIO.write(bi_cropper, suff, output);
-			byte[] ret = output.toByteArray();
-			return ret;
-		} catch (Exception e) {
-			throw new RuntimeException(e);
-		} finally {
-			try {
-				if (in != null) {
-					in.close();
-				}
-			} catch (IOException e) {
-			}
-			try {
-				if (output != null) {
-					output.close();
-				}
-			} catch (IOException e) {
-			}
-		}
-	}
+    public static int getLength(String text) {
+        int length = 0;
+        for (int i = 0; i < text.length(); i++) {
+            if (new String(text.charAt(i) + "").getBytes().length > 1) {
+                length += 2;
+            } else {
+                length += 1;
+            }
+        }
+        return length / 2;
+    }
 
-	public static int getLength(String text) {
-		int length = 0;
-		for (int i = 0; i < text.length(); i++) {
-			if (new String(text.charAt(i) + "").getBytes().length > 1) {
-				length += 2;
-			} else {
-				length += 1;
-			}
-		}
-		return length / 2;
-	}
+    public static String getExtension(File f) {
+        return (f != null) ? getExtension(f.getName()) : "";
+    }
 
-	public static String getExtension(File f) {
-		return (f != null) ? getExtension(f.getName()) : "";
-	}
+    public static String getExtension(String filename) {
+        return getExtension(filename, "");
+    }
 
-	public static String getExtension(String filename) {
-		return getExtension(filename, "");
-	}
+    public static String getExtension(String filename, String defExt) {
+        if ((filename != null) && (filename.length() > 0)) {
+            int i = filename.lastIndexOf('.');
 
-	public static String getExtension(String filename, String defExt) {
-		if ((filename != null) && (filename.length() > 0)) {
-			int i = filename.lastIndexOf('.');
+            if ((i > -1) && (i < (filename.length() - 1))) {
+                return filename.substring(i + 1);
+            }
+        }
+        return defExt;
+    }
 
-			if ((i > -1) && (i < (filename.length() - 1))) {
-				return filename.substring(i + 1);
-			}
-		}
-		return defExt;
-	}
+    public static String trimExtension(String filename) {
+        if ((filename != null) && (filename.length() > 0)) {
+            int i = filename.lastIndexOf('.');
+            if ((i > -1) && (i < (filename.length()))) {
+                return filename.substring(0, i);
+            }
+        }
+        return filename;
+    }
 
-	public static String trimExtension(String filename) {
-		if ((filename != null) && (filename.length() > 0)) {
-			int i = filename.lastIndexOf('.');
-			if ((i > -1) && (i < (filename.length()))) {
-				return filename.substring(0, i);
-			}
-		}
-		return filename;
-	}
+    /**
+     * 转化Image 为 BufferedImage
+     * 
+     * @param img
+     * @return
+     */
+    private static BufferedImage convertImageToBuffer(Image img) {
+        BufferedImage bufferedImage = new BufferedImage(img.getWidth(null), img.getHeight(null),
+                BufferedImage.TYPE_INT_RGB);
+        Graphics g = bufferedImage.createGraphics();
+        g.drawImage(img, 0, 0, null);
+        g.dispose();
+        return bufferedImage;
+    }
 
-	/**
-	 * 转化Image 为 BufferedImage
-	 * 
-	 * @param img
-	 * @return
-	 */
-	private static BufferedImage convertImageToBuffer(Image img) {
-		BufferedImage bufferedImage = new BufferedImage(img.getWidth(null), img.getHeight(null),
-				BufferedImage.TYPE_INT_RGB);
-		Graphics g = bufferedImage.createGraphics();
-		g.drawImage(img, 0, 0, null);
-		g.dispose();
-		return bufferedImage;
-	}
+    // 拼接图片
+    public static byte[] joinImages(List<byte[]> slices, String suff) {
+        boolean horizontal = false;
+        List<BufferedImage> imageList = new ArrayList<>();
+        for (byte[] slice : slices) {
+            InputStream in = null;
+            try {
+                in = new ByteArrayInputStream(slice);
+                BufferedImage image = ImageIO.read(in);
+                imageList.add(image);
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            } finally {
+                try {
+                    if (in != null) {
+                        in.close();
+                    }
+                } catch (IOException e) {
+                }
+            }
+        }
+        int height = imageList.get(0).getHeight();
+        int width = imageList.get(0).getWidth();
+        if (horizontal) {
+            height = imageList.stream().mapToInt(BufferedImage::getHeight).max().getAsInt();
+            width = imageList.stream().mapToInt(BufferedImage::getWidth).sum();
+        } else {
+            width = imageList.stream().mapToInt(BufferedImage::getWidth).max().getAsInt();
+            height = imageList.stream().mapToInt(BufferedImage::getHeight).sum();
+        }
+        // 创建拼接后的图片画布,参数分别为宽,高,类型,这里我们使用RGB3元色类型
+        BufferedImage resultImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+        Graphics graphics = resultImage.getGraphics();
+        int previousWidth = 0;
+        int previousHeight = 0;
+        for (BufferedImage image : imageList) {
+            // 向画布上画图片
+            graphics.drawImage(image, previousWidth, previousHeight, null);
+            if (horizontal) {
+                previousWidth += image.getWidth();
+            } else {
+                previousHeight += image.getHeight();
+            }
+        }
+        ByteArrayOutputStream output = new ByteArrayOutputStream();
+        try {
+            Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(suff);
+            ImageWriter imageWriter = iter.next();
+            JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(Locale.getDefault());
+            jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
+            jpegParams.setCompressionQuality(1.0F);
+            imageWriter.setOutput(ImageIO.createImageOutputStream(output));
+            imageWriter.write(null, new IIOImage(resultImage, null, null), jpegParams);
+            byte[] ret = output.toByteArray();
+            return ret;
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        } finally {
+            try {
+                if (output != null) {
+                    output.close();
+                }
+            } catch (IOException e) {
+            }
+        }
+    }
 
-	// 拼接图片
-	public static byte[] joinImages(List<byte[]> slices, String suff) {
-		boolean horizontal = false;
-		List<BufferedImage> imageList = new ArrayList<>();
-		for (byte[] slice : slices) {
-			InputStream in = null;
-			try {
-				in = new ByteArrayInputStream(slice);
-				BufferedImage image = ImageIO.read(in);
-				imageList.add(image);
-			} catch (Exception e) {
-				throw new RuntimeException(e);
-			} finally {
-				try {
-					if (in != null) {
-						in.close();
-					}
-				} catch (IOException e) {
-				}
-			}
-		}
-		int height = imageList.get(0).getHeight();
-		int width = imageList.get(0).getWidth();
-		if (horizontal) {
-			height = imageList.stream().mapToInt(BufferedImage::getHeight).max().getAsInt();
-			width = imageList.stream().mapToInt(BufferedImage::getWidth).sum();
-		} else {
-			width = imageList.stream().mapToInt(BufferedImage::getWidth).max().getAsInt();
-			height = imageList.stream().mapToInt(BufferedImage::getHeight).sum();
-		}
-		// 创建拼接后的图片画布,参数分别为宽,高,类型,这里我们使用RGB3元色类型
-		BufferedImage resultImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
-		Graphics graphics = resultImage.getGraphics();
-		int previousWidth = 0;
-		int previousHeight = 0;
-		for (BufferedImage image : imageList) {
-			// 向画布上画图片
-			graphics.drawImage(image, previousWidth, previousHeight, null);
-			if (horizontal) {
-				previousWidth += image.getWidth();
-			} else {
-				previousHeight += image.getHeight();
-			}
-		}
-		ByteArrayOutputStream output = new ByteArrayOutputStream();
-		try {
-			Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName(suff);
-			ImageWriter imageWriter = iter.next();
-			JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(Locale.getDefault());
-			jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
-			jpegParams.setCompressionQuality(1.0F);
-			imageWriter.setOutput(ImageIO.createImageOutputStream(output));
-			imageWriter.write(null, new IIOImage(resultImage, null, null), jpegParams);
-			byte[] ret = output.toByteArray();
-			return ret;
-		} catch (Exception e) {
-			throw new RuntimeException(e);
-		} finally {
-			try {
-				if (output != null) {
-					output.close();
-				}
-			} catch (IOException e) {
-			}
-		}
-	}
+    public static ImageSize getSize(byte[] iamge) {
+        InputStream is = null;
+        try {
+            byte[] bytes = Arrays.copyOf(iamge, iamge.length);
+            is = new ByteArrayInputStream(bytes);
+            BufferedImage image = ImageIO.read(is);
+            int width = image.getWidth();
+            int height = image.getHeight();
+            return new ImageSize(width, height);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        } finally {
+            if (is != null) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                }
+            }
+        }
+    }
 }

+ 1 - 1
src/main/resources/mapper/StmmsMapper.xml

@@ -19,7 +19,7 @@
 	</select>
 	<select id="getTrackPosition"
 		resultType="cn.com.qmth.am.bean.TrackPosition">
-		select t.position_x,t.offset_index
+		select t.offset_x,t.offset_index
 		from  m_track t 
 		where
 		t.student_id=#{studentId} and t.question_number =#{questionNumber}