Sample code for 30+ languages & platforms
DataFlex Requires Chilkat v11.7.0+

Generate a QR Code PNG File

See more QR Code Examples

Demonstrates the simplest way to create a QR code: the text to encode goes in payload.text, and output.path names the PNG file to write. QrCode chooses the smallest QR version that holds the text at error-correction level M, and the response reports what it chose and the size of the image. The request and the response are both JSON objects, and they must be two different objects.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vJson
    Handle hoJson
    Handle hoFmt
    Variant vJsonOut
    Handle hoJsonOut
    String sTemp1
    Integer iTemp1
    Integer iTemp2
    Integer iTemp3

    Move False To iSuccess

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "payload.text" "https://www.chilkatsoft.com/" To iSuccess

    //  Each module (one of the small squares) is drawn 8 pixels wide.  The default is 4.
    Get ComUpdateInt Of hoJson "render.moduleSize" 8 To iSuccess

    //  Write the image to a file.  An existing file is replaced only when output.overwrite is true.
    //  Otherwise the call fails and the existing file is left as it was.
    Get ComUpdateString Of hoJson "output.path" "qa_output/qr_chilkat.png" To iSuccess
    Get ComUpdateBool Of hoJson "output.overwrite" True To iSuccess

    //  The image is also returned in the response as base64 unless output.return is "none".
    Get ComUpdateString Of hoJson "output.return" "none" To iSuccess

    Get Create (RefClass(cComChilkatFormats)) To hoFmt
    If (Not(IsComObjectCreated(hoFmt))) Begin
        Send CreateComObject of hoFmt
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComQrCode Of hoFmt "generate" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFmt To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoJsonOut "outputs[0].path" To sTemp1
    Showln "file: " sTemp1
    Get ComIntOf Of hoJsonOut "outputs[0].width" To iTemp1
    Get ComIntOf Of hoJsonOut "outputs[0].height" To iTemp2
    Get ComIntOf Of hoJsonOut "outputs[0].byteCount" To iTemp3
    Showln "image: " iTemp1 " x " iTemp2 " pixels, " iTemp3 " bytes"
    Get ComIntOf Of hoJsonOut "symbols[0].selectedEncoding.version" To iTemp1
    Showln "QR version: " iTemp1
    Get ComStringOf Of hoJsonOut "symbols[0].selectedEncoding.errorCorrection" To sTemp1
    Showln "error correction: " sTemp1
    Get ComIntOf Of hoJsonOut "symbols[0].dimensions.modulesWide" To iTemp1
    Get ComIntOf Of hoJsonOut "symbols[0].dimensions.modulesHigh" To iTemp2
    Showln "modules: " iTemp1 " x " iTemp2

    //  The image written to qa_output/qr_chilkat.png:

    //  image

    //  Sample output:

    //  file: qa_output/qr_chilkat.png
    //  image: 296 x 296 pixels, 1251 bytes
    //  QR version: 3
    //  error correction: M
    //  modules: 29 x 29


End_Procedure