PureBasic Requires Chilkat v11.7.0+
PureBasic
Embed a QR Code in HTML as a Data URI
See more QR Code Examples
Demonstrates putting a QR code directly into an HTML page or HTML email with no separate image file. The base64 PNG returned by QrCode becomes the src of an img tag as a data URI, which is the MIME type from outputs[0].mimeType followed by the base64 from outputs[0].base64.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.
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",5)
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
; A data URI has the form data:<MIME type>;base64,<base64 data>
sbHtml.i = CkStringBuilder::ckCreate()
If sbHtml.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(sbHtml,"<html><body><p>Scan to visit chilkatsoft.com</p><img alt=" + Chr(34) + "QR code" + Chr(34) + " width=" + Chr(34))
CkStringBuilder::ckAppendInt(sbHtml,CkJsonObject::ckIntOf(jsonOut,"outputs[0].width"))
CkStringBuilder::ckAppend(sbHtml,Chr(34) + " height=" + Chr(34))
CkStringBuilder::ckAppendInt(sbHtml,CkJsonObject::ckIntOf(jsonOut,"outputs[0].height"))
CkStringBuilder::ckAppend(sbHtml,Chr(34) + " src=" + Chr(34) + "data:")
CkStringBuilder::ckAppend(sbHtml,CkJsonObject::ckStringOf(jsonOut,"outputs[0].mimeType"))
CkStringBuilder::ckAppend(sbHtml,";base64,")
CkStringBuilder::ckAppend(sbHtml,CkJsonObject::ckStringOf(jsonOut,"outputs[0].base64"))
CkStringBuilder::ckAppend(sbHtml,Chr(34) + "></body></html>")
success = CkStringBuilder::ckWriteFile(sbHtml,"qa_output/qr_page.html","utf-8",0)
If success = 0
Debug CkStringBuilder::ckLastErrorText(sbHtml)
CkJsonObject::ckDispose(json)
CkFormats::ckDispose(fmt)
CkJsonObject::ckDispose(jsonOut)
CkStringBuilder::ckDispose(sbHtml)
ProcedureReturn
EndIf
Debug "image: " + Str(CkJsonObject::ckIntOf(jsonOut,"outputs[0].width")) + " x " + Str(CkJsonObject::ckIntOf(jsonOut,"outputs[0].height")) + " pixels"
Debug "HTML length: " + Str(CkStringBuilder::ckLength(sbHtml))
; The page, as a browser shows it:
;
; Sample output:
; image: 185 x 185 pixels
; HTML length: 1211
CkJsonObject::ckDispose(json)
CkFormats::ckDispose(fmt)
CkJsonObject::ckDispose(jsonOut)
CkStringBuilder::ckDispose(sbHtml)
ProcedureReturn
EndProcedure