PageRenderTime 106ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/core/java/com/chenjw/textimage/service/utils/TextImageUtils.java

https://github.com/chenjw/textimage
Java | 99 lines | 73 code | 8 blank | 18 comment | 9 complexity | a4bf2a31d0c952424b785a2e50397197 MD5 | raw file
  1. package com.chenjw.textimage.service.utils;
  2. import java.io.UnsupportedEncodingException;
  3. import java.util.Arrays;
  4. import java.util.Comparator;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7. import org.apache.commons.codec.digest.DigestUtils;
  8. import com.alibaba.fastjson.JSON;
  9. import com.chenjw.textimage.service.model.TextField;
  10. import com.chenjw.textimage.service.model.TextLine;
  11. import com.chenjw.textimage.service.model.TextMetaInfo;
  12. import com.chenjw.textimage.service.model.TextUrlInfo;
  13. /**
  14. * 一些工具方法
  15. *
  16. * @author chenjw
  17. *
  18. */
  19. public class TextImageUtils {
  20. public static TextLine cloneTextLine(TextLine line) {
  21. TextLine newTextLine = new TextLine();
  22. newTextLine.setImageIndex(line.getImageIndex());
  23. newTextLine.setX(line.getX());
  24. newTextLine.setY(line.getY());
  25. newTextLine.setWidth(line.getWidth());
  26. newTextLine.setHeight(line.getHeight());
  27. return newTextLine;
  28. }
  29. /**
  30. * md5编码成定长32位字符串
  31. *
  32. * @param input
  33. * @return
  34. */
  35. public static String encode(String input) {
  36. try {
  37. return DigestUtils.md5Hex(input.getBytes("UTF-8"));
  38. } catch (UnsupportedEncodingException e) {
  39. return null;
  40. }
  41. }
  42. public static TextUrlInfo unmarshalTextUrlInfo(String str) {
  43. return JSON.parseObject(str, TextUrlInfo.class);
  44. }
  45. public static String marshalTextUrlInfo(TextUrlInfo textUrl) {
  46. TextMetaInfo textInfo = textUrl.getTextMetaInfo();
  47. if (textInfo.getTextFieldMap() != null) {
  48. for (Entry<String, TextField> entry : textInfo.getTextFieldMap()
  49. .entrySet()) {
  50. if (entry.getValue() != null
  51. && entry.getValue().getTextLines() != null) {
  52. for (int i = 0; i < entry.getValue().getTextLines().size(); i++) {
  53. TextLine textLine = entry.getValue().getTextLines()
  54. .get(i);
  55. entry.getValue().getTextLines()
  56. .set(i, TextImageUtils.cloneTextLine(textLine));
  57. }
  58. }
  59. }
  60. }
  61. String value = JSON.toJSONString(textUrl);
  62. return value;
  63. }
  64. /**
  65. * 获得数据的版本编号
  66. *
  67. * @param textMap
  68. * @return
  69. */
  70. public static String getVersion(Map<String, String> textMap,
  71. String styleVersion) {
  72. @SuppressWarnings("unchecked")
  73. Entry<String, String>[] entrys = textMap.entrySet().toArray(
  74. new Entry[textMap.size()]);
  75. Arrays.sort(entrys, new Comparator<Entry<String, String>>() {
  76. @Override
  77. public int compare(Entry<String, String> o1,
  78. Entry<String, String> o2) {
  79. return o1.getKey().hashCode() >= o2.getKey().hashCode() ? 1
  80. : -1;
  81. }
  82. });
  83. StringBuffer sb = new StringBuffer();
  84. sb.append(styleVersion);
  85. for (Entry<String, String> entry : entrys) {
  86. sb.append(entry.getKey());
  87. sb.append(entry.getValue());
  88. }
  89. return TextImageUtils.encode(sb.toString());
  90. }
  91. }