CardFile.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package cn.com.qmth.scancentral.bean.card;
  2. import java.awt.Image;
  3. import java.io.ByteArrayInputStream;
  4. import java.io.IOException;
  5. import java.util.Base64;
  6. import java.util.LinkedList;
  7. import java.util.List;
  8. import javax.imageio.ImageIO;
  9. import com.fasterxml.jackson.annotation.JsonIgnore;
  10. import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  11. import com.qmth.boot.core.exception.ParameterException;
  12. /**
  13. * 卡格式文件内容结构
  14. */
  15. @JsonIgnoreProperties(ignoreUnknown = true)
  16. public class CardFile {
  17. private String version;
  18. private boolean adapted;
  19. private List<CardPageWrapper> pages;
  20. @JsonIgnore
  21. private SliceConfig sliceConfig;
  22. @JsonIgnore
  23. private List<String> sliceName;
  24. public String getVersion() {
  25. return version;
  26. }
  27. public void setVersion(String version) {
  28. this.version = version;
  29. }
  30. public boolean isAdapted() {
  31. return adapted;
  32. }
  33. public void setAdapted(boolean adapted) {
  34. this.adapted = adapted;
  35. }
  36. public List<CardPageWrapper> getPages() {
  37. return pages;
  38. }
  39. public void setPages(List<CardPageWrapper> pages) {
  40. this.pages = pages;
  41. }
  42. /**
  43. * 解析卡格式文件中的裁切图坐标,用于云阅卷同步
  44. *
  45. * @return
  46. */
  47. public SliceConfig getSliceConfig() throws IOException {
  48. if (sliceConfig == null) {
  49. sliceConfig = new SliceConfig();
  50. if (pages != null) {
  51. int pageNumber = 0;
  52. for (CardPageWrapper pageWrapper : pages) {
  53. pageNumber++;
  54. CardPage page = pageWrapper.getExchange();
  55. // 遮盖模式
  56. if (page.getInfoArea() != null && page.getInfoArea().length > 0) {
  57. sliceConfig.addConfig(pageNumber, 0, 0, 0, 0);
  58. }
  59. // 裁切模式
  60. else if (page.getAnswerArea() != null && !page.getAnswerArea().isEmpty()) {
  61. // 获取卡格式图片原始尺寸
  62. // 此处图片base64编码特殊,需要采用Mime标准
  63. Image image = ImageIO
  64. .read(new ByteArrayInputStream(Base64.getMimeDecoder().decode(page.getPageImage())));
  65. int width = image.getWidth(null);
  66. int height = image.getHeight(null);
  67. for (AnswerArea area : page.getAnswerArea()) {
  68. sliceConfig.addConfig(pageNumber, (int) (area.getArea()[0] * width),
  69. (int) (area.getArea()[1] * height), (int) (area.getArea()[2] * width),
  70. (int) (area.getArea()[3] * height));
  71. }
  72. }
  73. }
  74. }
  75. }
  76. return sliceConfig;
  77. }
  78. public List<String> getSliceName() throws IOException {
  79. List<String> sliceName = new LinkedList<String>();
  80. if (pages != null) {
  81. for (CardPageWrapper pageWrapper : pages) {
  82. CardPage page = pageWrapper.getExchange();
  83. for (AnswerArea area : page.getAnswerArea()) {
  84. if (area.getMainNumber() == null) {
  85. throw new ParameterException("CET模式下裁切图必须指定大题号");
  86. }
  87. sliceName.add(area.getMainNumber().toString());
  88. }
  89. }
  90. }
  91. return sliceName;
  92. }
  93. }