Advertisement
sam1509

Untitled

Oct 28th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. public final class DataEncoding {
  2.  
  3. /**
  4. * @param input
  5. * @param version
  6. * @return
  7. */
  8. public static boolean[] byteModeEncoding(String input, int version) {
  9. // TODO Implementer
  10. return bytesToBinaryArray(addErrorCorrection(fillSequence(addInformations(encodeString(input,QRCodeInfos.getMaxInputLength(version))), QRCodeInfos.getCodeWordsLength(version)), QRCodeInfos.getECCLength(version)));
  11. }
  12.  
  13. /**
  14. * @param input
  15. * The string to convert to ISO-8859-1
  16. * @param maxLength
  17. * The maximal number of bytes to encode (will depend on the version of the QR code)
  18. * @return A array that represents the input in ISO-8859-1. The output is
  19. * truncated to fit the version capacity
  20. */
  21. public static int[] encodeString(String input, int maxLength) {
  22. // TODO Implementer
  23. if (maxLength>input.length()) {
  24. maxLength= input.length();
  25. }
  26. if (maxLength<input.length()) {
  27.  
  28. }
  29.  
  30. byte[] tabByte = new byte[maxLength];
  31. int[] tabInt = new int[maxLength];
  32. tabByte = input.getBytes(StandardCharsets.ISO_8859_1);
  33.  
  34. for (int i=0; i<maxLength;++i) {
  35. tabInt[i] = tabByte[i] & 0xFF;
  36. }
  37. return tabInt;
  38. }
  39.  
  40. /**
  41. * Add the 12 bits information data and concatenate the bytes to it
  42. *
  43. * @param inputBytes
  44. * the data byte sequence
  45. * @return The input bytes with an header giving the type and size of the data
  46. */
  47. public static int[] addInformations(int[] inputBytes) {
  48. byte indicateur = 0100;
  49. int taille = inputBytes.length;
  50.  
  51. byte postFix = 0000;
  52. int[] tabInt = new int[inputBytes.length + 2];
  53. tabInt[0]= indicateur | (taille >> 4);
  54. tabInt[1] = (taille << 4) & 0xFF | (inputBytes[0] >> 4);
  55. for (int i= 2; i < tabInt.length; ++i) {
  56. if (i!= tabInt.length -1) {
  57. tabInt[i] = (inputBytes[i-2] << 4) & 0xFF | (inputBytes[i-1] >>4);
  58. } else {
  59. tabInt[i] = (inputBytes[i-2] << 4) & 0xFF | (postFix << 4);
  60. }
  61.  
  62. }
  63. return tabInt;
  64. }
  65.  
  66. /**
  67. * Add padding bytes to the data until the size of the given array matches the
  68. * finalLength
  69. *
  70. * @param encodedData
  71. * the initial sequence of bytes
  72. * @param finalLength
  73. * the minimum length of the returned array
  74. * @return an array of length max(finalLength,encodedData.length) padded with
  75. * bytes 236,17
  76. */
  77. public static int[] fillSequence(int[] encodedData, int finalLength) {
  78. // return encodedData if finalLength < encodedData.length
  79. if(finalLength<=encodedData.length) {
  80. return encodedData;
  81. }
  82.  
  83.  
  84. int[] finalInt = new int[finalLength];
  85.  
  86. int bit1 = 17;
  87. int bit2 = 236;
  88.  
  89. //fill finalInt
  90. for(int i = 0; i < finalLength;++i) {
  91. if (i < encodedData.length) {
  92. finalInt[i]= encodedData[i];
  93. }else {
  94. //fill 17 and 236
  95. if(i%2 == 0) {
  96. finalInt[i]= bit1;
  97. ;
  98. }else {
  99. finalInt[i]=bit2;
  100. ;
  101. }
  102. }
  103. }
  104. return finalInt;
  105. // TODO Implementer
  106. }
  107.  
  108. /**
  109. * Add the error correction to the encodedData
  110. *
  111. * @param encodedData
  112. * The byte array representing the data encoded
  113. * @param eccLength
  114. * the version of the QR code
  115. * @return the original data concatenated with the error correction
  116. */
  117. public static int[] addErrorCorrection(int[] encodedData, int eccLength) {
  118. int[] error = ErrorCorrectionEncoding.encode(encodedData, eccLength);
  119. int [] finalEncodedData = new int[encodedData.length + error.length];
  120. for (int i = 0; i < finalEncodedData.length; i++) {
  121. if (i < encodedData.length) {
  122. finalEncodedData[i] = encodedData[i];
  123. } else {
  124. finalEncodedData[i] = error[i - encodedData.length];
  125. }
  126. }
  127. // TODO Implementer
  128. return finalEncodedData;
  129. }
  130.  
  131. /**
  132. * Encode the byte array into a binary array represented with boolean using the
  133. * most significant bit first.
  134. *
  135. * @param data
  136. * an array of bytes
  137. * @return a boolean array representing the data in binary
  138. */
  139. public static boolean[] bytesToBinaryArray(int[] data) {
  140. boolean [] boolTab= new boolean[8*data.length];
  141. int currentData = 0;
  142. for (int i = 0; i < data.length; i++) {
  143. currentData = data[i];
  144. for (int j = 0; j < 8; j++) {
  145. if(currentData % 2 == 0) {
  146. boolTab [(7-j) + 8*i] = false;
  147. currentData /= 2;
  148. } else {
  149. if (currentData % 2 == 1) {
  150. boolTab [(7-j) + 8*i] = true;
  151. currentData = (currentData -1)/2;
  152. }
  153. }
  154. }
  155. }
  156.  
  157. // TODO Implementer
  158. return boolTab;
  159. }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement