PureBasic Requires Chilkat v11.7.0+
PureBasic
Create a vCard (Contact) QR Code
See more QR Code Examples
Demonstrates a QR code that adds a contact to a phone when scanned. The text is a vCard, the standard format for contact details: lines between BEGIN:VCARD and END:VCARD, each ending with CRLF. A vCard is fairly long, so the symbol is larger than one holding a web address. Leave out the fields that are not needed to keep it small.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.
; The second argument of AppendLine is cktrue to end each line with CRLF.
sbCard.i = CkStringBuilder::ckCreate()
If sbCard.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppendLine(sbCard,"BEGIN:VCARD",1)
CkStringBuilder::ckAppendLine(sbCard,"VERSION:3.0",1)
CkStringBuilder::ckAppendLine(sbCard,"N:Smith;Alex;;;",1)
CkStringBuilder::ckAppendLine(sbCard,"FN:Alex Smith",1)
CkStringBuilder::ckAppendLine(sbCard,"ORG:Example Company",1)
CkStringBuilder::ckAppendLine(sbCard,"TITLE:Sales Manager",1)
CkStringBuilder::ckAppendLine(sbCard,"TEL;TYPE=CELL:+1-555-0100",1)
CkStringBuilder::ckAppendLine(sbCard,"EMAIL:alex.smith@example.com",1)
CkStringBuilder::ckAppendLine(sbCard,"URL:https://www.example.com",1)
CkStringBuilder::ckAppendLine(sbCard,"END:VCARD",1)
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateSb(json,"payload.text",sbCard)
CkJsonObject::ckUpdateInt(json,"render.moduleSize",6)
CkJsonObject::ckUpdateString(json,"output.path","qa_output/qr_vcard.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(sbCard)
CkJsonObject::ckDispose(json)
CkFormats::ckDispose(fmt)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "vCard length: " + Str(CkStringBuilder::ckLength(sbCard))
Debug "QR version: " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].selectedEncoding.version"))
Debug "modules: " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].dimensions.modulesWide")) + " x " + Str(CkJsonObject::ckIntOf(jsonOut,"symbols[0].dimensions.modulesHigh"))
Debug "image: " + Str(CkJsonObject::ckIntOf(jsonOut,"outputs[0].width")) + " x " + Str(CkJsonObject::ckIntOf(jsonOut,"outputs[0].height")) + " pixels"
; The image written to qa_output/qr_vcard.png:
;
; Sample output:
; vCard length: 197
; QR version: 10
; modules: 57 x 57
; image: 390 x 390 pixels
CkStringBuilder::ckDispose(sbCard)
CkJsonObject::ckDispose(json)
CkFormats::ckDispose(fmt)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndProcedure