Advertisement
sam1509

Untitled

Oct 31st, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.26 KB | None | 0 0
  1. package qrcode;
  2.  
  3. import java.nio.charset.StandardCharsets;
  4.  
  5. import reedsolomon.ErrorCorrectionEncoding;
  6.  
  7. public final class DataEncoding {
  8.  
  9. /**
  10. * @param input
  11. * @param version
  12. * @return
  13. */
  14. public static boolean[] byteModeEncoding(String input, int version) {
  15. // TODO Implementer
  16. return bytesToBinaryArray(addErrorCorrection(fillSequence(addInformations(encodeString(input,QRCodeInfos.getMaxInputLength(version))), QRCodeInfos.getCodeWordsLength(version)), QRCodeInfos.getECCLength(version)));
  17. }
  18.  
  19. /**
  20. * @param input
  21. * The string to convert to ISO-8859-1
  22. * @param maxLength
  23. * The maximal number of bytes to encode (will depend on the version of the QR code)
  24. * @return A array that represents the input in ISO-8859-1. The output is
  25. * truncated to fit the version capacity
  26. */
  27. public static int[] encodeString(String input, int maxLength) {
  28. // TODO Implementer
  29. if (maxLength>input.length()) {
  30. maxLength= input.length();
  31. }
  32. if (maxLength<input.length()) {
  33.  
  34. }
  35.  
  36. byte[] tabByte = new byte[maxLength];
  37. int[] tabInt = new int[maxLength];
  38. tabByte = input.getBytes(StandardCharsets.ISO_8859_1);
  39.  
  40. for (int i=0; i<maxLength;++i) {
  41. tabInt[i] = tabByte[i] & 0xFF;
  42. }
  43. return tabInt;
  44. }
  45.  
  46. /**
  47. * Add the 12 bits information data and concatenate the bytes to it
  48. *
  49. * @param inputBytes
  50. * the data byte sequence
  51. * @return The input bytes with an header giving the type and size of the data
  52. */
  53. public static int[] addInformations(int[] inputBytes) {
  54. byte indicateur = 0100;
  55. int taille = inputBytes.length;
  56.  
  57. byte postFix = 0000;
  58. int[] tabInt = new int[inputBytes.length + 2];
  59. tabInt[0]= indicateur | (taille >> 4);
  60. tabInt[1] = (taille << 4) & 0xFF | (inputBytes[0] >> 4);
  61. for (int i= 2; i < tabInt.length; ++i) {
  62. if (i!= tabInt.length -1) {
  63. tabInt[i] = (inputBytes[i-2] << 4) & 0xFF | (inputBytes[i-1] >>4);
  64. } else {
  65. tabInt[i] = (inputBytes[i-2] << 4) & 0xFF | (postFix << 4);
  66. }
  67.  
  68. }
  69. return tabInt;
  70. }
  71.  
  72. /**
  73. * Add padding bytes to the data until the size of the given array matches the
  74. * finalLength
  75. *
  76. * @param encodedData
  77. * the initial sequence of bytes
  78. * @param finalLength
  79. * the minimum length of the returned array
  80. * @return an array of length max(finalLength,encodedData.length) padded with
  81. * bytes 236,17
  82. */
  83. public static int[] fillSequence(int[] encodedData, int finalLength) {
  84. // return encodedData if finalLength < encodedData.length
  85. if(finalLength<=encodedData.length) {
  86. return encodedData;
  87. }
  88.  
  89.  
  90. int[] finalInt = new int[finalLength];
  91.  
  92. int bit1 = 17;
  93. int bit2 = 236;
  94.  
  95. //fill finalInt
  96. for(int i = 0; i < finalLength;++i) {
  97. if (i < encodedData.length) {
  98. finalInt[i]= encodedData[i];
  99. }else {
  100. //fill 17 and 236
  101. if(i%2 == 0) {
  102. finalInt[i]= bit1;
  103. ;
  104. }else {
  105. finalInt[i]=bit2;
  106. ;
  107. }
  108. }
  109. }
  110. return finalInt;
  111. // TODO Implementer
  112. }
  113.  
  114. /**
  115. * Add the error correction to the encodedData
  116. *
  117. * @param encodedData
  118. * The byte array representing the data encoded
  119. * @param eccLength
  120. * the version of the QR code
  121. * @return the original data concatenated with the error correction
  122. */
  123. public static int[] addErrorCorrection(int[] encodedData, int eccLength) {
  124. int[] error = ErrorCorrectionEncoding.encode(encodedData, eccLength);
  125. int [] finalEncodedData = new int[encodedData.length + error.length];
  126. for (int i = 0; i < finalEncodedData.length; i++) {
  127. if (i < encodedData.length) {
  128. finalEncodedData[i] = encodedData[i];
  129. } else {
  130. finalEncodedData[i] = error[i - encodedData.length];
  131. }
  132. }
  133. // TODO Implementer
  134. return finalEncodedData;
  135. }
  136.  
  137. /**
  138. * Encode the byte array into a binary array represented with boolean using the
  139. * most significant bit first.
  140. *
  141. * @param data
  142. * an array of bytes
  143. * @return a boolean array representing the data in binary
  144. */
  145. public static boolean[] bytesToBinaryArray(int[] data) {
  146. boolean [] boolTab= new boolean[8*data.length];
  147. int currentData = 0;
  148. for (int i = 0; i < data.length; i++) {
  149. currentData = data[i];
  150. for (int j = 0; j < 8; j++) {
  151. if(currentData % 2 == 0) {
  152. boolTab [(7-j) + 8*i] = false;
  153. currentData /= 2;
  154. } else {
  155. if (currentData % 2 == 1) {
  156. boolTab [(7-j) + 8*i] = true;
  157. currentData = (currentData -1)/2;
  158. }
  159. }
  160. }
  161. }
  162.  
  163. // TODO Implementer
  164. return boolTab;
  165. }
  166.  
  167. }
  168. package qrcode;
  169.  
  170. public class MatrixConstruction {
  171.  
  172. /*
  173. * Constants defining the color in ARGB format
  174. *
  175. * W = White integer for ARGB
  176. *
  177. * B = Black integer for ARGB
  178. *
  179. * both needs to have their alpha component to 255
  180. */
  181. // TODO add constant for White pixel
  182. // TODO add constant for Black pixel
  183.  
  184. //FF = 255 & 00 = 0
  185. //meilleur notation pour les couleurs en java est en hexadécimal
  186.  
  187.  
  188. public static int W = 0xFF_00_00_00; // blanc
  189. public static int B = 0xFF_FF_FF_FF; // noir
  190.  
  191.  
  192. // ... MYDEBUGCOLOR = ...;
  193. // feel free to add your own colors for debugging purposes
  194. public static int R = 0xFF_FF_00_00; // rouge
  195. public static int V = 0xFF_00_FF_00; // vert
  196.  
  197. /**
  198. * Create the matrix of a QR code with the given data.
  199. *
  200. * @param version
  201. * The version of the QR code
  202. * @param data
  203. * The data to be written on the QR code
  204. * @param mask
  205. * The mask used on the data. If not valid (e.g: -1), then no mask is
  206. * used.
  207. * @return The matrix of the QR code
  208. */
  209. public static int[][] renderQRCodeMatrix(int version, boolean[] data, int mask) {
  210.  
  211. /*
  212. * PART 2
  213. */
  214. int[][] matrix = constructMatrix(version, mask);
  215. /*
  216. * PART 3
  217. */
  218. addDataInformation(matrix, data, mask);
  219.  
  220. return matrix;
  221. }
  222.  
  223. /*
  224. * =======================================================================
  225. *
  226. * ****************************** PART 2 *********************************
  227. *
  228. * =======================================================================
  229. */
  230.  
  231. /**
  232. * Create a matrix (2D array) ready to accept data for a given version and mask
  233. *
  234. * @param version
  235. * the version number of QR code (has to be between 1 and 4 included)
  236. * @param mask
  237. * the mask id to use to mask the data modules. Has to be between 0
  238. * and 7 included to have a valid matrix. If the mask id is not
  239. * valid, the modules would not be not masked later on, hence the
  240. * QRcode would not be valid
  241. * @return the qrcode with the patterns and format information modules
  242. * initialized. The modules where the data should be remain empty.
  243. */
  244. public static int[][] constructMatrix(int version, int mask) {
  245. // TODO Implementer
  246. int[][] Matrice = initializeMatrix(version);
  247. addFinderPatterns(Matrice);
  248. addAlignmentPatterns(Matrice,version);
  249.  
  250. return Matrice;
  251.  
  252. }
  253.  
  254. /**
  255. * Create an empty 2d array of integers of the size needed for a QR code of the
  256. * given version
  257. *
  258. * @param version
  259. * the version number of the qr code (has to be between 1 and 4
  260. * included
  261. * @return an empty matrix
  262. */
  263. public static int[][] initializeMatrix(int version) {
  264. // TODO Implementer
  265. int MatTaille = QRCodeInfos.getMatrixSize(version);
  266. int[][] Matrice = new int[MatTaille][MatTaille];
  267.  
  268. //initialise la matrice avec les composants ARGB tous à 0
  269. for(int i = 0; i < Matrice.length;++i) {
  270. for(int j = 0; j < Matrice.length;++j) {
  271. Matrice[i][j]= 0x00_00_00_00;
  272. }
  273. }
  274. return Matrice;
  275. }
  276.  
  277. /**
  278. * Add all finder patterns to the given matrix with a border of White modules.
  279. *
  280. * @param matrix
  281. * the 2D array to modify: where to add the patterns
  282. * @return
  283. */
  284. public static int[][] Pattern(int[][] matrix, int posx, int posy, int taille) {
  285.  
  286. for(int i = 0; i < taille;++i) {
  287. for(int j = 0; j < taille;++j) {
  288. if((i==1 && ( j<taille-1 && j>0 ))||(i==taille-2 && ( j<taille-1 && j>0 ))|| (j==1 && ( i<taille-1 && i>0 ))||(j==taille-2 && ( i<taille-1 && i>0 ))) {
  289. matrix[posx+i][posy+j] = B;
  290. }else {
  291. matrix[posx+i][posy+j] = W;
  292. }
  293. }
  294. }
  295. return matrix;
  296. }
  297.  
  298.  
  299. public static void addFinderPatterns(int[][] matrix) {
  300. // TODO Implementer
  301. // premier pattern
  302. matrix = Pattern(matrix,0,0,7);
  303. //deuxième pattern
  304. matrix = Pattern(matrix,matrix.length-7,0,7);
  305. // troisième pattern
  306. matrix = Pattern(matrix,0,matrix.length-7,7);
  307.  
  308.  
  309. }
  310.  
  311. /**
  312. * Add the alignment pattern if needed, does nothing for version 1
  313. *
  314. * @param matrix
  315. * The 2D array to modify
  316. * @param version
  317. * the version number of the QR code needs to be between 1 and 4
  318. * included
  319. */
  320. public static void addAlignmentPatterns(int[][] matrix, int version) {
  321. // TODO Implementer
  322. if(version != 1) {
  323. int DebutPattern = matrix.length-9;
  324.  
  325. matrix = Pattern(matrix,DebutPattern,DebutPattern,5);
  326. }
  327.  
  328.  
  329.  
  330. }
  331.  
  332. /**
  333. * Add the timings patterns
  334. *
  335. * @param matrix
  336. * The 2D array to modify
  337. */
  338. public static void addTimingPatterns(int[][] matrix) {
  339. // TODO Implementer
  340. }
  341.  
  342. /**
  343. * Add the dark module to the matrix
  344. *
  345. * @param matrix
  346. * the 2-dimensional array representing the QR code
  347. */
  348. public static void addDarkModule(int[][] matrix) {
  349. // TODO Implementer
  350. }
  351.  
  352. /**
  353. * Add the format information to the matrix
  354. *
  355. * @param matrix
  356. * the 2-dimensional array representing the QR code to modify
  357. * @param mask
  358. * the mask id
  359. */
  360. public static void addFormatInformation(int[][] matrix, int mask) {
  361. // TODO Implementer
  362. }
  363.  
  364. /*
  365. * =======================================================================
  366. * ****************************** PART 3 *********************************
  367. * =======================================================================
  368. */
  369.  
  370. /**
  371. * Choose the color to use with the given coordinate using the masking 0
  372. *
  373. * @param col
  374. * x-coordinate
  375. * @param row
  376. * y-coordinate
  377. * @param color
  378. * : initial color without masking
  379. * @return the color with the masking
  380. */
  381. public static int maskColor(int col, int row, boolean dataBit, int masking) {
  382. // TODO Implementer
  383. return 0;
  384. }
  385.  
  386. /**
  387. * Add the data bits into the QR code matrix
  388. *
  389. * @param matrix
  390. * a 2-dimensionnal array where the bits needs to be added
  391. * @param data
  392. * the data to add
  393. */
  394. public static void addDataInformation(int[][] matrix, boolean[] data, int mask) {
  395. // TODO Implementer
  396.  
  397. }
  398.  
  399. /*
  400. * =======================================================================
  401. *
  402. * ****************************** BONUS **********************************
  403. *
  404. * =======================================================================
  405. */
  406.  
  407. /**
  408. * Create the matrix of a QR code with the given data.
  409. *
  410. * The mask is computed automatically so that it provides the least penalty
  411. *
  412. * @param version
  413. * The version of the QR code
  414. * @param data
  415. * The data to be written on the QR code
  416. * @return The matrix of the QR code
  417. */
  418. public static int[][] renderQRCodeMatrix(int version, boolean[] data) {
  419.  
  420. int mask = findBestMasking(version, data);
  421.  
  422. return renderQRCodeMatrix(version, data, mask);
  423. }
  424.  
  425. /**
  426. * Find the best mask to apply to a QRcode so that the penalty score is
  427. * minimized. Compute the penalty score with evaluate
  428. *
  429. * @param data
  430. * @return the mask number that minimize the penalty
  431. */
  432. public static int findBestMasking(int version, boolean[] data) {
  433. // TODO BONUS
  434. return 0;
  435. }
  436.  
  437. /**
  438. * Compute the penalty score of a matrix
  439. *
  440. * @param matrix:
  441. * the QR code in matrix form
  442. * @return the penalty score obtained by the QR code, lower the better
  443. */
  444. public static int evaluate(int[][] matrix) {
  445. //TODO BONUS
  446.  
  447. return 0;
  448. }
  449.  
  450. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement