PureBasic Requires Chilkat v11.7.0+
PureBasic
Get the Modules of a QR Code (Bit Matrix)
See more QR Code Examples
Demonstrates getting the QR code itself, as a grid of dark and light modules, to draw with something other than the built-in PNG and SVG output: on a canvas, in a PDF, on a display, or with a label printer language. With output.includeModuleData the response has symbols[0].moduleData, a bit matrix with one bit per module, each row starting on a new byte, most significant bit first, 1 for dark. The quiet zone is not part of the matrix. The example prints the symbol using two characters per module.Chilkat PureBasic Downloads
IncludeFile "CkBinData.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkFormats.pb"
Procedure ChilkatExample()
success.i = 0
; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json,"payload.text","CHILKAT")
CkJsonObject::ckUpdateBool(json,"output.includeModuleData",1)
fmt.i = CkFormats::ckCreate()
If fmt.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
jsonOut.i = CkJsonObject::ckCreate()
If jsonOut.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkFormats::ckQrCode(fmt,"generate",json,jsonOut)
If success = 0
Debug CkFormats::ckLastErrorText(fmt)
CkJsonObject::ckDispose(json)
CkFormats::ckDispose(fmt)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
width.i = CkJsonObject::ckIntOf(jsonOut,"symbols[0].moduleData.width")
height.i = CkJsonObject::ckIntOf(jsonOut,"symbols[0].moduleData.height")
rowBytes.i = CkJsonObject::ckIntOf(jsonOut,"symbols[0].moduleData.rowBytes")
Debug "kind: " + CkJsonObject::ckStringOf(jsonOut,"symbols[0].moduleData.kind")
Debug "modules: " + Str(width) + " x " + Str(height) + ", " + Str(rowBytes) + " bytes per row"
; The matrix is height rows of rowBytes bytes.
bd.i = CkBinData::ckCreate()
If bd.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkBinData::ckAppendEncoded(bd,CkJsonObject::ckStringOf(jsonOut,"symbols[0].moduleData.bytesBase64"),"base64")
If success = 0
Debug CkBinData::ckLastErrorText(bd)
CkJsonObject::ckDispose(json)
CkFormats::ckDispose(fmt)
CkJsonObject::ckDispose(jsonOut)
CkBinData::ckDispose(bd)
ProcedureReturn
EndIf
sbRow.i = CkStringBuilder::ckCreate()
If sbRow.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
row.i
col.i
b.i
mask.i = 128
For row = 0 To height - 1
CkStringBuilder::ckClear(sbRow)
For col = 0 To width - 1
; The module at (col,row) is a bit of the byte at (row * rowBytes) + (col / 8).
; The leftmost module of each byte is its most significant bit.
If (col % 8) = 0
mask = 128
EndIf
b = CkBinData::ckGetByte(bd,(row * rowBytes) + (col / 8))
If ((b / mask) % 2) = 1
CkStringBuilder::ckAppend(sbRow,"##")
Else
CkStringBuilder::ckAppend(sbRow," ")
EndIf
mask = mask / 2
Next
Debug CkStringBuilder::ckGetAsString(sbRow)
Next
; The same symbol as the PNG that the call also returned:
;
; Sample output:
; kind: bitMatrix
; modules: 21 x 21, 3 bytes per row
; ############## ##############
; ## ## #### ## ##
; ## ###### ## ## #### ## ###### ##
; ## ###### ## ## ## ###### ##
; ## ###### ## ## ## ## ## ###### ##
; ## ## #### ## ## ##
; ############## ## ## ## ##############
; ####
; ## ########## #### ##########
; #### ## ######## ######
; #### ######## #### ## #### ####
; ## ## ############ #### ##
; ###### #### ###### ## ## ##
; ## ## ## ## ## ##
; ############## ## ## ## ##
; ## ## ## ## ###### ####
; ## ###### ## #### ## ## ##
; ## ###### ## ## ############ ##
; ## ###### ## ## ## #### ##
; ## ## ## ######## ##
; ############## ## ## ######
CkJsonObject::ckDispose(json)
CkFormats::ckDispose(fmt)
CkJsonObject::ckDispose(jsonOut)
CkBinData::ckDispose(bd)
CkStringBuilder::ckDispose(sbRow)
ProcedureReturn
EndProcedure