Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package qrcode;
- import java.nio.charset.StandardCharsets;
- import reedsolomon.ErrorCorrectionEncoding;
- public final class DataEncoding {
- /**
- * @param input
- * @param version
- * @return
- */
- public static boolean[] byteModeEncoding(String input, int version) {
- // TODO Implementer
- return bytesToBinaryArray(addErrorCorrection(fillSequence(addInformations(encodeString(input,QRCodeInfos.getMaxInputLength(version))), QRCodeInfos.getCodeWordsLength(version)), QRCodeInfos.getECCLength(version)));
- }
- /**
- * @param input
- * The string to convert to ISO-8859-1
- * @param maxLength
- * The maximal number of bytes to encode (will depend on the version of the QR code)
- * @return A array that represents the input in ISO-8859-1. The output is
- * truncated to fit the version capacity
- */
- public static int[] encodeString(String input, int maxLength) {
- // TODO Implementer
- if (maxLength>input.length()) {
- maxLength= input.length();
- }
- if (maxLength<input.length()) {
- }
- byte[] tabByte = new byte[maxLength];
- int[] tabInt = new int[maxLength];
- tabByte = input.getBytes(StandardCharsets.ISO_8859_1);
- for (int i=0; i<maxLength;++i) {
- tabInt[i] = tabByte[i] & 0xFF;
- }
- return tabInt;
- }
- /**
- * Add the 12 bits information data and concatenate the bytes to it
- *
- * @param inputBytes
- * the data byte sequence
- * @return The input bytes with an header giving the type and size of the data
- */
- public static int[] addInformations(int[] inputBytes) {
- byte indicateur = 0100;
- int taille = inputBytes.length;
- byte postFix = 0000;
- int[] tabInt = new int[inputBytes.length + 2];
- tabInt[0]= indicateur | (taille >> 4);
- tabInt[1] = (taille << 4) & 0xFF | (inputBytes[0] >> 4);
- for (int i= 2; i < tabInt.length; ++i) {
- if (i!= tabInt.length -1) {
- tabInt[i] = (inputBytes[i-2] << 4) & 0xFF | (inputBytes[i-1] >>4);
- } else {
- tabInt[i] = (inputBytes[i-2] << 4) & 0xFF | (postFix << 4);
- }
- }
- return tabInt;
- }
- /**
- * Add padding bytes to the data until the size of the given array matches the
- * finalLength
- *
- * @param encodedData
- * the initial sequence of bytes
- * @param finalLength
- * the minimum length of the returned array
- * @return an array of length max(finalLength,encodedData.length) padded with
- * bytes 236,17
- */
- public static int[] fillSequence(int[] encodedData, int finalLength) {
- // return encodedData if finalLength < encodedData.length
- if(finalLength<=encodedData.length) {
- return encodedData;
- }
- int[] finalInt = new int[finalLength];
- int bit1 = 17;
- int bit2 = 236;
- //fill finalInt
- for(int i = 0; i < finalLength;++i) {
- if (i < encodedData.length) {
- finalInt[i]= encodedData[i];
- }else {
- //fill 17 and 236
- if(i%2 == 0) {
- finalInt[i]= bit1;
- ;
- }else {
- finalInt[i]=bit2;
- ;
- }
- }
- }
- return finalInt;
- // TODO Implementer
- }
- /**
- * Add the error correction to the encodedData
- *
- * @param encodedData
- * The byte array representing the data encoded
- * @param eccLength
- * the version of the QR code
- * @return the original data concatenated with the error correction
- */
- public static int[] addErrorCorrection(int[] encodedData, int eccLength) {
- int[] error = ErrorCorrectionEncoding.encode(encodedData, eccLength);
- int [] finalEncodedData = new int[encodedData.length + error.length];
- for (int i = 0; i < finalEncodedData.length; i++) {
- if (i < encodedData.length) {
- finalEncodedData[i] = encodedData[i];
- } else {
- finalEncodedData[i] = error[i - encodedData.length];
- }
- }
- // TODO Implementer
- return finalEncodedData;
- }
- /**
- * Encode the byte array into a binary array represented with boolean using the
- * most significant bit first.
- *
- * @param data
- * an array of bytes
- * @return a boolean array representing the data in binary
- */
- public static boolean[] bytesToBinaryArray(int[] data) {
- boolean [] boolTab= new boolean[8*data.length];
- int currentData = 0;
- for (int i = 0; i < data.length; i++) {
- currentData = data[i];
- for (int j = 0; j < 8; j++) {
- if(currentData % 2 == 0) {
- boolTab [(7-j) + 8*i] = false;
- currentData /= 2;
- } else {
- if (currentData % 2 == 1) {
- boolTab [(7-j) + 8*i] = true;
- currentData = (currentData -1)/2;
- }
- }
- }
- }
- // TODO Implementer
- return boolTab;
- }
- }
- package qrcode;
- public class MatrixConstruction {
- /*
- * Constants defining the color in ARGB format
- *
- * W = White integer for ARGB
- *
- * B = Black integer for ARGB
- *
- * both needs to have their alpha component to 255
- */
- // TODO add constant for White pixel
- // TODO add constant for Black pixel
- //FF = 255 & 00 = 0
- //meilleur notation pour les couleurs en java est en hexadécimal
- public static int W = 0xFF_00_00_00; // blanc
- public static int B = 0xFF_FF_FF_FF; // noir
- // ... MYDEBUGCOLOR = ...;
- // feel free to add your own colors for debugging purposes
- public static int R = 0xFF_FF_00_00; // rouge
- public static int V = 0xFF_00_FF_00; // vert
- /**
- * Create the matrix of a QR code with the given data.
- *
- * @param version
- * The version of the QR code
- * @param data
- * The data to be written on the QR code
- * @param mask
- * The mask used on the data. If not valid (e.g: -1), then no mask is
- * used.
- * @return The matrix of the QR code
- */
- public static int[][] renderQRCodeMatrix(int version, boolean[] data, int mask) {
- /*
- * PART 2
- */
- int[][] matrix = constructMatrix(version, mask);
- /*
- * PART 3
- */
- addDataInformation(matrix, data, mask);
- return matrix;
- }
- /*
- * =======================================================================
- *
- * ****************************** PART 2 *********************************
- *
- * =======================================================================
- */
- /**
- * Create a matrix (2D array) ready to accept data for a given version and mask
- *
- * @param version
- * the version number of QR code (has to be between 1 and 4 included)
- * @param mask
- * the mask id to use to mask the data modules. Has to be between 0
- * and 7 included to have a valid matrix. If the mask id is not
- * valid, the modules would not be not masked later on, hence the
- * QRcode would not be valid
- * @return the qrcode with the patterns and format information modules
- * initialized. The modules where the data should be remain empty.
- */
- public static int[][] constructMatrix(int version, int mask) {
- // TODO Implementer
- int[][] Matrice = initializeMatrix(version);
- addFinderPatterns(Matrice);
- addAlignmentPatterns(Matrice,version);
- return Matrice;
- }
- /**
- * Create an empty 2d array of integers of the size needed for a QR code of the
- * given version
- *
- * @param version
- * the version number of the qr code (has to be between 1 and 4
- * included
- * @return an empty matrix
- */
- public static int[][] initializeMatrix(int version) {
- // TODO Implementer
- int MatTaille = QRCodeInfos.getMatrixSize(version);
- int[][] Matrice = new int[MatTaille][MatTaille];
- //initialise la matrice avec les composants ARGB tous à 0
- for(int i = 0; i < Matrice.length;++i) {
- for(int j = 0; j < Matrice.length;++j) {
- Matrice[i][j]= 0x00_00_00_00;
- }
- }
- return Matrice;
- }
- /**
- * Add all finder patterns to the given matrix with a border of White modules.
- *
- * @param matrix
- * the 2D array to modify: where to add the patterns
- * @return
- */
- public static int[][] Pattern(int[][] matrix, int posx, int posy, int taille) {
- for(int i = 0; i < taille;++i) {
- for(int j = 0; j < taille;++j) {
- 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 ))) {
- matrix[posx+i][posy+j] = B;
- }else {
- matrix[posx+i][posy+j] = W;
- }
- }
- }
- return matrix;
- }
- public static void addFinderPatterns(int[][] matrix) {
- // TODO Implementer
- // premier pattern
- matrix = Pattern(matrix,0,0,7);
- //deuxième pattern
- matrix = Pattern(matrix,matrix.length-7,0,7);
- // troisième pattern
- matrix = Pattern(matrix,0,matrix.length-7,7);
- }
- /**
- * Add the alignment pattern if needed, does nothing for version 1
- *
- * @param matrix
- * The 2D array to modify
- * @param version
- * the version number of the QR code needs to be between 1 and 4
- * included
- */
- public static void addAlignmentPatterns(int[][] matrix, int version) {
- // TODO Implementer
- if(version != 1) {
- int DebutPattern = matrix.length-9;
- matrix = Pattern(matrix,DebutPattern,DebutPattern,5);
- }
- }
- /**
- * Add the timings patterns
- *
- * @param matrix
- * The 2D array to modify
- */
- public static void addTimingPatterns(int[][] matrix) {
- // TODO Implementer
- }
- /**
- * Add the dark module to the matrix
- *
- * @param matrix
- * the 2-dimensional array representing the QR code
- */
- public static void addDarkModule(int[][] matrix) {
- // TODO Implementer
- }
- /**
- * Add the format information to the matrix
- *
- * @param matrix
- * the 2-dimensional array representing the QR code to modify
- * @param mask
- * the mask id
- */
- public static void addFormatInformation(int[][] matrix, int mask) {
- // TODO Implementer
- }
- /*
- * =======================================================================
- * ****************************** PART 3 *********************************
- * =======================================================================
- */
- /**
- * Choose the color to use with the given coordinate using the masking 0
- *
- * @param col
- * x-coordinate
- * @param row
- * y-coordinate
- * @param color
- * : initial color without masking
- * @return the color with the masking
- */
- public static int maskColor(int col, int row, boolean dataBit, int masking) {
- // TODO Implementer
- return 0;
- }
- /**
- * Add the data bits into the QR code matrix
- *
- * @param matrix
- * a 2-dimensionnal array where the bits needs to be added
- * @param data
- * the data to add
- */
- public static void addDataInformation(int[][] matrix, boolean[] data, int mask) {
- // TODO Implementer
- }
- /*
- * =======================================================================
- *
- * ****************************** BONUS **********************************
- *
- * =======================================================================
- */
- /**
- * Create the matrix of a QR code with the given data.
- *
- * The mask is computed automatically so that it provides the least penalty
- *
- * @param version
- * The version of the QR code
- * @param data
- * The data to be written on the QR code
- * @return The matrix of the QR code
- */
- public static int[][] renderQRCodeMatrix(int version, boolean[] data) {
- int mask = findBestMasking(version, data);
- return renderQRCodeMatrix(version, data, mask);
- }
- /**
- * Find the best mask to apply to a QRcode so that the penalty score is
- * minimized. Compute the penalty score with evaluate
- *
- * @param data
- * @return the mask number that minimize the penalty
- */
- public static int findBestMasking(int version, boolean[] data) {
- // TODO BONUS
- return 0;
- }
- /**
- * Compute the penalty score of a matrix
- *
- * @param matrix:
- * the QR code in matrix form
- * @return the penalty score obtained by the QR code, lower the better
- */
- public static int evaluate(int[][] matrix) {
- //TODO BONUS
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement