DataUtils.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package fcb.project.manager.base.utils;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.google.zxing.BarcodeFormat;
  4. import com.google.zxing.EncodeHintType;
  5. import com.google.zxing.MultiFormatWriter;
  6. import com.google.zxing.common.BitMatrix;
  7. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  8. import fdage.back.sdk.utils.FileUtils;
  9. import lombok.extern.log4j.Log4j2;
  10. import org.springframework.stereotype.Component;
  11. import org.springframework.util.CollectionUtils;
  12. import org.springframework.util.StringUtils;
  13. import javax.imageio.ImageIO;
  14. import java.awt.image.BufferedImage;
  15. import java.io.File;
  16. import java.io.IOException;
  17. import java.math.BigDecimal;
  18. import java.security.MessageDigest;
  19. import java.security.NoSuchAlgorithmException;
  20. import java.util.*;
  21. import static com.google.zxing.client.j2se.MatrixToImageConfig.BLACK;
  22. import static com.google.zxing.client.j2se.MatrixToImageConfig.WHITE;
  23. /**
  24. * @author abnerhou
  25. * @date 2020/4/23 17:35
  26. * @desciption
  27. */
  28. @Component
  29. @Log4j2
  30. public class DataUtils {
  31. public static BigDecimal getBigDecimalObj(Object data) {
  32. if (null == data) {
  33. return null;
  34. } else if (data instanceof String) {
  35. String dataStr = (String) data;
  36. if (!StringUtils.isEmpty(dataStr.trim())) {
  37. return new BigDecimal(dataStr.trim());
  38. }
  39. } else if (data instanceof Long) {
  40. Long dataLong = (Long) data;
  41. return BigDecimal.valueOf(dataLong);
  42. } else if (data instanceof Integer) {
  43. Integer dataInt = (Integer) data;
  44. return BigDecimal.valueOf(dataInt);
  45. } else if (data instanceof Double) {
  46. Double dataDouble = (Double) data;
  47. return BigDecimal.valueOf(dataDouble);
  48. }
  49. return null;
  50. }
  51. public static Integer getInteger(Object object) {
  52. if (null == object) {
  53. return new Integer(0);
  54. }
  55. if (object instanceof String) {
  56. String ojStr = (String) object;
  57. if (StringUtils.isEmpty(ojStr)) {
  58. //TODO:在斟酌这里的处理方式
  59. return new Integer(0);
  60. } else {
  61. return new Integer(Integer.parseInt(ojStr.trim()));
  62. }
  63. } else if (object instanceof Integer) {
  64. return (Integer) object;
  65. } else if (object instanceof Long) {
  66. return (Integer) object;
  67. } else if (object instanceof Double) {
  68. return (Integer) object;
  69. } else {
  70. return new Integer(0);
  71. }
  72. }
  73. public static Integer getIntegerWithDefault(Object object, boolean withDefault) {
  74. if (null == object) {
  75. return withDefault ? new Integer(0) : null;
  76. }
  77. if (object instanceof String) {
  78. String ojStr = (String) object;
  79. if (StringUtils.isEmpty(ojStr)) {
  80. return withDefault ? new Integer(0) : null;
  81. } else {
  82. return new Integer(Integer.parseInt(ojStr.trim()));
  83. }
  84. } else if (object instanceof Integer) {
  85. return (Integer) object;
  86. } else if (object instanceof Long) {
  87. return (Integer) object;
  88. } else if (object instanceof Double) {
  89. return (Integer) object;
  90. } else {
  91. return withDefault ? new Integer(0) : null;
  92. }
  93. }
  94. public static Map<String, Object> assembleResult(long totalNum, long totalPageNum, long currPageNum, Object list) {
  95. Map<String, Object> resultMap = new HashMap<>();
  96. resultMap.put("totalNum", totalNum);
  97. resultMap.put("totalPageNum", totalPageNum);
  98. resultMap.put("curPage", currPageNum);
  99. resultMap.put("list", list);
  100. return resultMap;
  101. }
  102. public static <T> void assembleTimeQueryWrapper(Map<String, Object> constantQuery, String dynamicQuery,
  103. QueryWrapper<T> queryWrapper, String idQuery,
  104. String secondQuery, String thirdQuery) {
  105. assembleConstantQuery(queryWrapper, constantQuery);
  106. if (org.apache.commons.lang3.StringUtils.isNotBlank(dynamicQuery)) {
  107. queryWrapper.and(wrapper -> wrapper
  108. .or().like(org.apache.commons.lang3.StringUtils.isNotBlank(secondQuery), secondQuery, dynamicQuery)
  109. .or().like(org.apache.commons.lang3.StringUtils.isNotBlank(thirdQuery), thirdQuery, dynamicQuery)
  110. .or().like(dynamicQuery.length() <= 32 && org.apache.commons.lang3.StringUtils.isNotBlank(idQuery), idQuery, dynamicQuery));
  111. }
  112. }
  113. public static <T> void assembleConstantQuery(QueryWrapper<T> queryWrapper, Map<String, Object> constantQuery) {
  114. if (null != queryWrapper && !CollectionUtils.isEmpty(constantQuery)) {
  115. for (Map.Entry<String, Object> entry : constantQuery.entrySet()) {
  116. queryWrapper.eq(entry.getKey(), entry.getValue());
  117. }
  118. }
  119. }
  120. /**
  121. * 根据map的key进行字典升序排序
  122. * @param map
  123. * @return map
  124. */
  125. public static Map<String, Object> sortMapByKey(Map<String, Object>map) {
  126. Map<String, Object> treemap = new TreeMap<String, Object>(map);
  127. List<Map.Entry<String, Object>> list = new ArrayList<Map.Entry<String, Object>>(treemap.entrySet());
  128. Collections.sort(list, new Comparator<Map.Entry<String, Object>>() {
  129. @Override
  130. public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
  131. return org.apache.commons.lang3.StringUtils.compare(o1.getKey() , o2.getKey());
  132. }
  133. });
  134. return treemap;
  135. }
  136. /**
  137. *
  138. * 生成MD5 32位小写字符的密码串
  139. * */
  140. public static String md5Encryption(String jsonStr) {
  141. String re_md5 = new String();
  142. try {
  143. MessageDigest md = MessageDigest.getInstance("MD5");
  144. md.update(jsonStr.getBytes());
  145. byte b[] = md.digest();
  146. StringBuffer buf = new StringBuffer("");
  147. int i;
  148. for (int offset = 0; offset < b.length; offset++) {
  149. i = b[offset];
  150. if (i < 0){
  151. i += 256;
  152. }
  153. if (i < 16){
  154. buf.append("0");
  155. }
  156. buf.append(Integer.toHexString(i));
  157. }
  158. re_md5 = buf.toString();
  159. } catch (NoSuchAlgorithmException e) {
  160. log.info("生成MD5密文出错了:{}" , e);
  161. e.printStackTrace();
  162. }
  163. return re_md5.toLowerCase();
  164. }
  165. /**
  166. *
  167. * 根据上传的照片生成二维码
  168. * **/
  169. public static boolean createQRCode(String url, String outPath, String logoPath) throws Exception {
  170. // 生成二维码
  171. int width = 300; // 二维码图片宽度 300
  172. int height = 300; // 二维码图片高度300
  173. String format = "jpg";// 二维码的图片格式 gif
  174. Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
  175. // 指定纠错等级,纠错级别(L 7%、M 15%、Q 25%、H 30%)
  176. hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
  177. // 内容所使用字符集编码
  178. hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
  179. // hints.put(EncodeHintType.MAX_SIZE, 350);//设置图片的最大值
  180. // hints.put(EncodeHintType.MIN_SIZE, 100);//设置图片的最小值
  181. hints.put(EncodeHintType.MARGIN, 1);//设置二维码边的空度,非负数
  182. BitMatrix bitMatrix = new MultiFormatWriter().encode(url,
  183. //编码类型,目前zxing支持:Aztec 2D,CODABAR 1D format,Code 39 1D,Code 93 1D ,Code 128 1D,
  184. //Data Matrix 2D , EAN-8 1D,EAN-13 1D,ITF (Interleaved Two of Five) 1D,
  185. //MaxiCode 2D barcode,PDF417,QR Code 2D,RSS 14,RSS EXPANDED,UPC-A 1D,UPC-E 1D,UPC/EAN extension,UPC_EAN_EXTENSION
  186. BarcodeFormat.QR_CODE,
  187. width, //条形码的宽度
  188. height, //条形码的高度
  189. hints);//生成条形码时的一些配置,此项可选
  190. //File outputFile = new File("d:" + File.separator + "new-1.gif");//指定输出路径
  191. File outputFile = new File(outPath);//指定输出路径
  192. FileUtils.deleteFile(outPath);
  193. return writeToFile(bitMatrix, format, outputFile, logoPath);
  194. }
  195. public static BufferedImage toBufferedImage(BitMatrix matrix) {
  196. int width = matrix.getWidth();
  197. int height = matrix.getHeight();
  198. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  199. for (int x = 0; x < width; x++) {
  200. for (int y = 0; y < height; y++) {
  201. image.setRGB(x, y, (matrix.get(x, y) ? BLACK : WHITE));
  202. // image.setRGB(x, y, (matrix.get(x, y) ? Color.YELLOW.getRGB() : Color.CYAN.getRGB()));
  203. }
  204. }
  205. return image;
  206. }
  207. public static boolean writeToFile(BitMatrix matrix, String format, File file, String logoPath) throws IOException {
  208. BufferedImage image = toBufferedImage(matrix);
  209. //设置logo图标
  210. LogoConfig logoConfig = new LogoConfig();
  211. image = logoConfig.LogoMatrix(image, logoPath);
  212. File parFile = new File(file.getParent() + File.separator);
  213. if (!parFile.exists()){
  214. parFile.mkdirs();
  215. }
  216. if (!ImageIO.write(image, format, file)) {
  217. //throw new IOException("Could not write an image of format " + format + " to " + file);
  218. log.info("Could not write an image of format " + format + " to " + file);
  219. return false;
  220. } else {
  221. log.info("二维码生成成功!");
  222. return true;
  223. }
  224. }
  225. public static void main(String[] args) throws Exception{
  226. createQRCode("https://www.4dkankan.com/spc.html?m=t-pnj0IJX", "C:/Users/4dage/Desktop/logo-file/t-pnj0IJX1.png", null);
  227. }
  228. }