Hello I have a function which split a string and translate each caracter into its ASCII code and put it in a Matrix. Here is the function
const encodeWithRedondance = (userInput) => {
const realLength = userInput.length;
/* if the userInput is less than 6 we add white caracter untill the length reach 6 */
if(realLength < 6){
const residual = 6-realLength;
for(let i = 0; i < residual; i++){
userInput += ' ';
}
}
const userInputLengthAdd = userInput.length % 2 == 0 ? userInput.length : userInput.length +1;
console.log("length added "+userInputLengthAdd);
const rows = (userInputLengthAdd % 4 === 0) ? userInputLengthAdd / 4 : userInputLengthAdd % 4;
const cols = userInputLengthAdd / 2;
console.log("rows = "+rows+" cols = "+ cols);
//Create a bidimentionnal array <=> Matrix
let encoded = [rows];
let userInputSplited = new Array(rows);
for(let i = 0; i < rows; i++){
userInputSplited[i] = new Array(cols);
encoded[i] = new Array(cols);
}
// Phase d'affectation de la matrice
/* Put each userInput each within a Matrix */
let elt = 0;
for(let i = 0; i < rows; i++){
for(let j = 0; j < cols ; j++,elt++){
userInputSplited[i][j] = userInput[elt];
/* if the userInput length is not dividable by 2 we add the '!' caracter at the end */
/* so it will fill the last element of Matrix */
if((realLength % 2 !== 0) && ((i === rows -1) && (j === cols -1))){
userInputSplited[i][j] = '!';
}
}
}
/* encodage du message contenu dans la matrice */
/* encoding message content within the matrix */
let encoding = [];
for(let i = 0; i < rows; i++){
encoding = userInputSplited[i].join('');
//console.log("here encoding = "+encoding);
for(let j = 0; j < encoding.length; j++){
let ascii = encoding.charCodeAt(j);
let s;
if(ascii < 100){
s = ascii.toString();
ascii = '0'+s;
}else{ascii = ascii.toString();}
//console.log("asciiString = "+ascii);
//for(let t = 0; t < cols; t++){encoded[i][t] = ascii;}
//console.log("j donne: ==> "+j);
encoded[i][j] = ascii;
//console.log(" ==> "+encoded[i][j]);
}
}
//console.log(userInputSplited);
console.log(encoded);
}
encodeWithRedondance('hel');
module.exports = {encodage : encode , encodeWithRedondance : encodeWithRedondance}
and now I want to put display my matrix as an image. So can someone help me with image and matrix in NodeJS.
The final goal is to have a QR-Code.
Here is how I call it in another file
const encode = require('./encodeDecode');
const readline = require('readline');
//create interface
const rl = readline.createInterface({input: process.stdin,
output: process.stdout});
rl.question("Write here the message which you want to encode ?\n",
(userInput) => {
if(userInput){
encode.encodeWithRedondance(userInput);
rl.close();
}
//encode.encodage(userInput);
});