PureBasic Requires Chilkat v11.7.0+
PureBasic
Generate a QR Code PNG in Memory
See more QR Code Examples
Demonstrates getting a QR code image without writing a file. With no output options the result is a PNG returned as base64 in outputs[0].base64 of the response. The example decodes it into a BinData, which holds the bytes of the PNG to send in an HTTP response, store in a database, attach to an email, or save.Chilkat PureBasic Downloads
IncludeFile "CkBinData.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","https://www.chilkatsoft.com/")
CkJsonObject::ckUpdateInt(json,"render.moduleSize",6)
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
Debug "format: " + CkJsonObject::ckStringOf(jsonOut,"outputs[0].format")
Debug "MIME type: " + CkJsonObject::ckStringOf(jsonOut,"outputs[0].mimeType")
Debug "image: " + Str(CkJsonObject::ckIntOf(jsonOut,"outputs[0].width")) + " x " + Str(CkJsonObject::ckIntOf(jsonOut,"outputs[0].height")) + " pixels"
; outputs[0].base64 is the PNG file, base64 encoded.
bdPng.i = CkBinData::ckCreate()
If bdPng.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkBinData::ckAppendEncoded(bdPng,CkJsonObject::ckStringOf(jsonOut,"outputs[0].base64"),"base64")
If success = 0
Debug CkBinData::ckLastErrorText(bdPng)
CkJsonObject::ckDispose(json)
CkFormats::ckDispose(fmt)
CkJsonObject::ckDispose(jsonOut)
CkBinData::ckDispose(bdPng)
ProcedureReturn
EndIf
Debug "PNG bytes: " + Str(CkBinData::ckNumBytes(bdPng))
; The BinData holds the image. Here it is simply saved.
success = CkBinData::ckWriteFile(bdPng,"qa_output/qr_in_memory.png")
If success = 0
Debug CkBinData::ckLastErrorText(bdPng)
CkJsonObject::ckDispose(json)
CkFormats::ckDispose(fmt)
CkJsonObject::ckDispose(jsonOut)
CkBinData::ckDispose(bdPng)
ProcedureReturn
EndIf
; The PNG held in bdPng:
;
; Sample output:
; format: png
; MIME type: image/png
; image: 222 x 222 pixels
; PNG bytes: 877
CkJsonObject::ckDispose(json)
CkFormats::ckDispose(fmt)
CkJsonObject::ckDispose(jsonOut)
CkBinData::ckDispose(bdPng)
ProcedureReturn
EndProcedure