DotMatrixConversionUtil.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package com.yeechart.dotMatrix.canvas;
  2. import java.nio.ByteBuffer;
  3. import java.util.concurrent.ForkJoinPool;
  4. import java.util.stream.IntStream;
  5. /**
  6. * 点阵图 转换 工具
  7. */
  8. public class DotMatrixConversionUtil {
  9. /**
  10. * 二进制点阵转换为16进制点阵数组
  11. *
  12. * @param twoBytes 字节型
  13. * @return
  14. */
  15. public static byte[] twoTo16Lattice(byte[] twoBytes) {
  16. int byteCount = twoBytes.length / 8;
  17. if (twoBytes.length % 8 > 0) {
  18. byteCount = byteCount + 1;
  19. }
  20. ByteBuffer byteBuffer = ByteBuffer.allocate(byteCount);
  21. int byteValue = 0;
  22. int bitCount = 0;
  23. for (int i = 0; i < twoBytes.length; i++) {
  24. int bit = twoBytes[i];
  25. byteValue = (byteValue << 1) | bit;
  26. bitCount++;
  27. if (bitCount == 8) {
  28. byteBuffer.put((byte) byteValue);
  29. byteValue = 0;
  30. bitCount = 0;
  31. }
  32. }
  33. if (bitCount > 0) {
  34. byteBuffer.put((byte) byteValue);
  35. }
  36. byteBuffer.flip();
  37. byte[] byteArray = new byte[byteBuffer.remaining()];
  38. byteBuffer.get(byteArray);
  39. return byteArray;
  40. }
  41. /**
  42. * 16进制点阵图转换为二进制点阵图
  43. *
  44. * @param sixteenBytes
  45. * @return
  46. */
  47. public static byte[] sixteenTo2Lattice(byte[] sixteenBytes) {
  48. int length = sixteenBytes.length * 8;
  49. byte[] result = new byte[length];
  50. int batchSize = 1024; // 每批次处理的字节数
  51. int numBatches = (sixteenBytes.length + batchSize - 1) / batchSize;
  52. ForkJoinPool.commonPool().submit(() -> IntStream.range(0, numBatches)
  53. .parallel()
  54. .forEach(batch -> {
  55. int start = batch * batchSize;
  56. int end = Math.min(start + batchSize, sixteenBytes.length);
  57. for (int i = start; i < end; i++) {
  58. for (int b = 7, k = i * 8; b >= 0; b--, k++) {
  59. result[k] = (byte) ((sixteenBytes[i] & (1 << b)) != 0 ? 1 : 0);
  60. }
  61. }
  62. })).join();
  63. return result;
  64. }
  65. /**
  66. * 二进制点阵转换为16进制点阵数组
  67. *
  68. * @param canvasBitmapArrays 二位素组
  69. * @return
  70. */
  71. public static byte[] twoTo16Lattice(int[][] canvasBitmapArrays) {
  72. int[] twoBytes = new int[canvasBitmapArrays.length * canvasBitmapArrays[0].length];
  73. for (int i = 0; i < canvasBitmapArrays.length; i++) {
  74. for (int j = 0; j < canvasBitmapArrays[i].length; j++) {
  75. twoBytes[i * canvasBitmapArrays[i].length + j] = canvasBitmapArrays[i][j];
  76. }
  77. }
  78. int byteCount = twoBytes.length / 8;
  79. if (twoBytes.length % 8 > 0) {
  80. byteCount = byteCount + 1;
  81. }
  82. ByteBuffer byteBuffer = ByteBuffer.allocate(byteCount);
  83. int byteValue = 0;
  84. int bitCount = 0;
  85. for (int i = 0; i < twoBytes.length; i++) {
  86. int bit = twoBytes[i];
  87. byteValue = (byteValue << 1) | bit;
  88. bitCount++;
  89. if (bitCount == 8) {
  90. byteBuffer.put((byte) byteValue);
  91. byteValue = 0;
  92. bitCount = 0;
  93. }
  94. }
  95. if (bitCount > 0) {
  96. byteBuffer.put((byte) byteValue);
  97. }
  98. byteBuffer.flip();
  99. byte[] byteArray = new byte[byteBuffer.remaining()];
  100. byteBuffer.get(byteArray);
  101. return byteArray;
  102. }
  103. }