QR = {} -- i = row -- j = column -- Functions to use later local function replaceChar(pos, str, r) return str:sub(1, pos-1) .. r .. str:sub(pos+1) end local function boolBin(condition) if condition == true then return '1' else return '0' end end local function binXOR(bin1,bin2) local bin3 = bin1 print('BinXOR:') print('bin1',bin1) print('bin2',bin2) for i = 1,#bin1,1 do if bin1:sub(i,i) == '1' and bin2:sub(i,i) == '1' then bin3 = replaceChar(i,bin3,'0') else bin3 = replaceChar(i,bin3,bin2:sub(i,i)) end end return bin3 end local function bin(num,fillerZeroes,fillerIsMax) local binTable = {} local rest = 0 while num > 0 do rest = math.fmod(num,2) binTable[#binTable+1] = math.floor(rest) num = (num-rest)/2 end while #binTable < (fillerZeroes or 0) do binTable[#binTable+1] = 0 end if (fillerZeroes and fillerIsMax) and #binTable > fillerZeroes then return bin(tonumber(string.rep('1',fillerZeroes),2),fillerZeroes) end local binString = string.reverse(table.concat(binTable)) return binString end QR.EncodingMode={ Numeric=1, Alphanumeric=2, Bit8Byte=4 } QR.ErrorCorrectionLevel={ Low=1, Medium=0, Quadrant=3, High=2 } QR.Mask = { [0] = { MaskBinary = '000', MaskPattern = '101', Logic = function() return (i+j)%2==0 end}, [1] = { MaskBinary = '001', Logic = function() return i%2==0 end}, [2] = { MaskBinary = '010', Logic = function() return j%3==0 end}, [3] = { MaskBinary = '011', Logic = function() return (i+j)%3==0 end}, [4] = { MaskBinary = '100', Logic = function() return (i/2+j/3)%2==0 end}, [5] = { MaskBinary = '101', Logic = function() return (i*j)%2+(i*j)%3==0 end}, [6] = { MaskBinary = '110', Logic = function() return ((i*j)%3+i*j)%2==0 end}, [7] = { MaskBinary = '111', Logic = function() return ((i*j)%3+i+j)%2==0 end} } function QR.Mask:FromMaskBinary(MaskBinary) return self[tonumber(MaskBinary,2)] end QR.Version={ v1={Value=1,Name='Version 1',Resolution=21}, v2={Value=2,Name='Version 2',Resolution=25}, v3={Value=3,Name='Version 3',Resolution=29}, v4={Value=4,Name='Version 4',Resolution=33}, v10={Value=10,Name='Version 10',Resolution=57}, v25={Value=25,Name='Version 25',Resolution=117}, v40={Value=40,Name='Version 40',Resolution=177} } QR._BinaryArrays={ PositionMarker={ NW = { '11111110', '10000010', '10111010', '10111010', '10111010', '10000010', '11111110', '00000000' }, NE = { '01111111', '01000001', '01011101', '01011101', '01011101', '01000001', '01111111', '00000000' }, SW = { '00000000', '11111110', '10000010', '10111010', '10111010', '10111010', '10000010', '11111110' } }, AlignmentMarker = { '11111', '10001', '10101', '10001', '11111' } } QR._BinarySpecifications={ FormatInformationString = { [1] = { [0]='111011111000100', [1]='111001011110011', [2]='111110110101010', [3]='111100010011101', [4]='110011000101111', [5]='110001100011000', [6]='110110001000001', [7]='110100101110110' }, [0] = { [0]='101010000010010', [1]='101000100100101', [2]='101111001111100', [3]='101101101001011', [4]='100010111111001', [5]='100000011001110', [6]='100111110010111', [7]='100101010100000' }, [3] = { [0]='011010101011111', [1]='011000001101000', [2]='011111100110001', [3]='011101000000110', [4]='010010010110100', [5]='010000110000011', [6]='010111011011010', [7]='010101111101101' }, [2] = { [0]='001011010001001', [1]='001001110111110', [2]='001110011100111', [3]='001100111010000', [4]='000011101100010', [5]='000001001010101', [6]='000110100001100', [7]='000100000111011' }, } } function QR.maskBit(mask,bitValue,j,i) if QR.Mask.Logic[mask]() == true then if bitValue == 0 then bitValue = 1 else bitValue = 0 end end return bitValue end function QR.OneZeroBlock(str) str,_ = str:gsub('1','█') str,_ = str:gsub('0',' ') return str end function QR.new(str,version,mask,encodingMode,errorCorrectionLevel) str = str or '' version = version or QR.Version.v2 mask = mask or QR.Mask[0] encodingMode = encodingMode or QR.EncodingMode.Alphanumeric errorCorrectionLevel = errorCorrectionLevel or QR.ErrorCorrectionLevel.Low local QRStruct = { Text=str, Version=version, Mask=mask, EncodingMode=encodingMode, ErrorCorrectionLevel=errorCorrectionLevel } function QRStruct:PrintInfo() print( string.format('QR Version: %s\nContent: %s\nMask: %s\nEncoding: %s\nECL: %s', self.Version.Name..' ('..tostring(self.Version.Resolution)..'x'..tostring(self.Version.Resolution)..')', '"'..self.Text..'"', self.Mask.MaskBinary, bin(self.EncodingMode,4)..' ('..tostring(self.EncodingMode)..')', self.ErrorCorrectionLevel )) end function QRStruct:GetBinaryArray() local binaryArray = {} local function writeToArrayPos(array,posX,posY,allowOverwrite) if posY then posY = posY - 1 end if posX then posX = posX - 1 end if #array+posY > self.Version.Resolution then posY = self.Version.Resolution-#array end if #array[1]+posX > self.Version.Resolution then posX = self.Version.Resolution-#array[1] end for rowInx = 1+posY,#array+posY,1 do for charInx = 1,#array[rowInx-posY],1 do if binaryArray[rowInx]:sub(charInx+posX,charInx+posX) ~= 'x' and not allowOverwrite then else binaryArray[rowInx] = replaceChar( charInx+posX, binaryArray[rowInx], array[rowInx-posY]:sub(charInx,charInx) ) end end end end for binRow = 1,self.Version.Resolution,1 do -- 'x' represents an empty space, can be written over binaryArray[binRow] = string.rep('x',self.Version.Resolution) end -- Post-masking: local Format5Bits = bin(self.ErrorCorrectionLevel,2)..self.Mask.MaskBinary print(Format5Bits) -- Calculation of the Error Correction bits local GeneratorPolynomial = '10100110111' -- > Constant value -- Pad with zeroes to the right, and then remove leading zeroes local ErrorCorrectionBits = (Format5Bits..string.rep('0',15-#Format5Bits)):sub(string.find(Format5Bits,'1'),15) while #ErrorCorrectionBits >= 11 do local GeneratorPolyPadded = GeneratorPolynomial..string.rep('0',#ErrorCorrectionBits-#GeneratorPolynomial) -- > Generator polynomial padded so its length is equal to the ErrorCorrectionBits string local XOR_ECB = binXOR(ErrorCorrectionBits,GeneratorPolyPadded) -- > XOR_ECB = XOR Error Correction Bits print('XOR Result:',XOR_ECB) -- Removing leading zeroes from XOR_ECB and assign it to ErrorCorrectionBits for next loop ErrorCorrectionBits = XOR_ECB:sub(string.find(XOR_ECB,'1'),15) print(ErrorCorrectionBits,'('..tostring(#ErrorCorrectionBits)..')') end if #ErrorCorrectionBits < 10 then ErrorCorrectionBits = string.rep('0',10-#ErrorCorrectionBits)..ErrorCorrectionBits end local Format15String = Format5Bits..ErrorCorrectionBits local MaskXORString = QR._BinarySpecifications.FormatInformationString[self.ErrorCorrectionLevel][tonumber(self.Mask.MaskBinary,2)] print('Final ErrCorrection:',ErrorCorrectionBits) print('15-Bit Format String:',Format15String) print('Mask XOR String:',MaskXORString) local MaskedFormat15String = binXOR(Format15String,MaskXORString) print('Masked 15-Bit Format String:',MaskedFormat15String) -- Placement: NW Marker local R = self.Version.Resolution for inx,val in ipairs({ -- # X Y {1,1,9},{2,2,9},{3,3,9},{4,4,9}, {5,5,9},{6,6,9},{7,8,9},{8,9,9}, {9,9,8},{10,9,6},{11,9,5},{12,9,4}, {13,9,3},{14,9,2},{15,9,1}, {1,9,R-1},{2,9,R-2},{3,9,R-3},{4,9,R-4}, {5,9,R-5},{6,9,R-6},{7,9,R-7},{8,R-7,9}, {9,R-6,9},{10,R-5,9},{11,R-4,9},{12,R-3,9}, {13,R-2,9},{14,R-1,9},{15,R,9} }) do writeToArrayPos( {MaskedFormat15String:sub(val[1],val[1])}, val[2], val[3] ) end -- Timing Markers (NW -> SW) for i = 8,self.Version.Resolution-8,1 do writeToArrayPos( {boolBin((i-1)%2==0)}, 7, i, true ) end -- Timing Markers (NW -> NE) for i = 8,self.Version.Resolution-8,1 do writeToArrayPos( {boolBin((i-1)%2==0)}, i, 7, true ) end -- NW Position Marker's 5 Formatting Bits writeToArrayPos( {Format5Bits}, 1, 9, true ) -- SW Position Marker's 5 Formatting Bits (Vertical Copy) writeToArrayPos( { Format5Bits:sub(5,5), Format5Bits:sub(4,4), Format5Bits:sub(3,3), Format5Bits:sub(2,2), Format5Bits:sub(1,1) }, 9, self.Version.Resolution, true ) -- Constants: do not change -- Dark Module writeToArrayPos({'1'},9,self.Version.Resolution-7,true) writeToArrayPos(QR._BinaryArrays.PositionMarker.NW,1,1,true) writeToArrayPos(QR._BinaryArrays.PositionMarker.SW,1,self.Version.Resolution,true) writeToArrayPos(QR._BinaryArrays.PositionMarker.NE,self.Version.Resolution,1,true) if self.Version.Value >= 3 then -- Alignment marker writeToArrayPos(QR._BinaryArrays.AlignmentMarker,self.Version.Resolution-8,self.Version.Resolution-8,true) end -- Cleanup the x for cleanupInx,cleanupVal in ipairs(binaryArray) do local cleanedString,_ = cleanupVal:gsub('x','0') --binaryArray[cleanupInx] = cleanedString end return binaryArray end return QRStruct end local newQRCode = QR.new('test',QR.Version.v1) newQRCode.ErrorCorrectionLevel = QR.ErrorCorrectionLevel.Low newQRCode.Mask = QR.Mask[4] newQRCode:PrintInfo() local qrcodeBinaryArray = newQRCode:GetBinaryArray() for i,v in ipairs(qrcodeBinaryArray) do print(i,QR.OneZeroBlock(v)) end
Write, Run & Share Lua code online using OneCompiler's Lua online compiler for free. It's one of the robust, feature-rich online compilers for Lua language, running the latest Lua version 5.4. Getting started with the OneCompiler's Lua editor is easy and fast. The editor shows sample boilerplate code when you choose language as Lua and start coding.
OneCompiler's Lua online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Lua program which takes name as input and prints hello message with your name.
name = io.read("*a")
print ("Hello ", name)
Lua is a light weight embeddable scripting language which is built on top of C. It is used in almost all kind of applications like games, web applications, mobile applications, image processing etc. It's a very powerful, fast, easy to learn, open-source scripting language.
-- global variables
a = 10
-- local variables
local x = 30
Value Type | Description |
---|---|
number | Represents numbers |
string | Represents text |
nil | Differentiates values whether it has data or not |
boolean | Value can be either true or false |
function | Represents a sub-routine |
userdata | Represents arbitary C data |
thread | Represents independent threads of execution. |
table | Can hold any value except nil |
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition)
do
--code
end
Repeat-Until is also used to iterate a set of statements based on a condition. It is very similar to Do-While, it is mostly used when you need to execute the statements atleast once.
repeat
--code
until( condition )
For loop is used to iterate a set of statements based on a condition.
for init,max/min value, increment
do
--code
end
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increase re-usuability and modularity.
optional_function_scope function function_name( argument1, argument2, argument3........, argumentn)
--code
return params with comma seperated
end