PureBasic Requires Chilkat v11.7.0+
PureBasic
Split Data across Several QR Codes (Structured Append)
See more QR Code Examples
Demonstrates structured append, the QR feature for data too large for one symbol of the size wanted. The data is divided over up to 16 symbols, and each carries its position, the total, and a parity byte that ties the set together. With encoding.structuredAppend.enabled, QrCode uses as few symbols as it can. output.pathTemplate writes one file per symbol, with {index} replaced by 1, 2, 3 and so on. The reader must support structured append to put the parts back together. Many phone camera apps do not, and show each part separately.Chilkat PureBasic Downloads
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.
; Some text that is too long for one symbol of the size allowed below.
sbText.i = CkStringBuilder::ckCreate()
If sbText.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
i.i
For i = 1 To 20
CkStringBuilder::ckAppend(sbText,"Line ")
CkStringBuilder::ckAppendInt(sbText,i)
CkStringBuilder::ckAppendLine(sbText," of a message that is too long for one small QR code.",1)
Next
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateSb(json,"payload.text",sbText)
; Keep each symbol small (version 10 is 57 modules wide) and allow a set of symbols.
CkJsonObject::ckUpdateInt(json,"encoding.qr.maxVersion",10)
CkJsonObject::ckUpdateBool(json,"encoding.structuredAppend.enabled",1)
; One file for each symbol: qr_part_1.png, qr_part_2.png, ...
CkJsonObject::ckUpdateString(json,"output.pathTemplate","qa_output/qr_part_{index}.png")
CkJsonObject::ckUpdateBool(json,"output.overwrite",1)
CkJsonObject::ckUpdateString(json,"output.return","none")
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)
CkStringBuilder::ckDispose(sbText)
CkJsonObject::ckDispose(json)
CkFormats::ckDispose(fmt)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "text length: " + Str(CkStringBuilder::ckLength(sbText))
numSymbols.i = CkJsonObject::ckSizeOfArray(jsonOut,"symbols")
Debug "symbols: " + Str(numSymbols)
For i = 0 To numSymbols - 1
CkJsonObject::setCkI(jsonOut, i)
; structuredAppend.index counts from 0.
Debug CkJsonObject::ckStringOf(jsonOut,"outputs[i].path") + ": symbol " + Str((CkJsonObject::ckIntOf(jsonOut,"symbols[i].structuredAppend.index") + 1)) + " of " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[i].structuredAppend.total")) + ", version " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[i].selectedEncoding.version")) + ", " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[i].payloadBytes")) + " bytes"
Next
; Every symbol of a set carries the same parity byte, which is how a reader knows they belong together.
Debug "parity: " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].structuredAppend.parity"))
; The six symbols:
;
; Sample output:
; text length: 1231
; symbols: 6
; qa_output/qr_part_1.png: symbol 1 of 6, version 10, 206 bytes
; qa_output/qr_part_2.png: symbol 2 of 6, version 10, 205 bytes
; qa_output/qr_part_3.png: symbol 3 of 6, version 10, 205 bytes
; qa_output/qr_part_4.png: symbol 4 of 6, version 10, 205 bytes
; qa_output/qr_part_5.png: symbol 5 of 6, version 10, 205 bytes
; qa_output/qr_part_6.png: symbol 6 of 6, version 10, 205 bytes
; parity: 50
CkStringBuilder::ckDispose(sbText)
CkJsonObject::ckDispose(json)
CkFormats::ckDispose(fmt)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndProcedure