Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement