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

Fit a QR Code within a Maximum Size

See more QR Code Examples

Demonstrates limiting the size of the image. render.maxWidth and render.maxHeight are the largest image allowed. With render.sizing set to shrinkToFit, QrCode starts from the module size asked for and reduces it, a whole pixel at a time, until the image fits, but never below render.minModuleSize. If it cannot fit, the call fails with noFit rather than returning a code too small to scan.

Chilkat C Downloads

C
#include <C_CkJsonObject.h>
#include <C_CkFormats.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObject json;
    HCkFormats fmt;
    HCkJsonObject jsonOut;

    success = FALSE;

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

    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"payload.text","https://www.chilkatsoft.com/barcode_overview.asp");

    //  Ask for 10-pixel modules, but accept anything down to 3 pixels to stay within 300 x 300.
    CkJsonObject_UpdateInt(json,"render.moduleSize",10);
    CkJsonObject_UpdateString(json,"render.sizing","shrinkToFit");
    CkJsonObject_UpdateInt(json,"render.minModuleSize",3);
    CkJsonObject_UpdateInt(json,"render.maxWidth",300);
    CkJsonObject_UpdateInt(json,"render.maxHeight",300);

    CkJsonObject_UpdateString(json,"output.path","qa_output/qr_fit_300.png");
    CkJsonObject_UpdateBool(json,"output.overwrite",TRUE);
    CkJsonObject_UpdateString(json,"output.return","none");

    fmt = CkFormats_Create();
    jsonOut = CkJsonObject_Create();
    success = CkFormats_QrCode(fmt,"generate",json,jsonOut);
    if (success == FALSE) {
        printf("%s\n",CkFormats_lastErrorText(fmt));
        CkJsonObject_Dispose(json);
        CkFormats_Dispose(fmt);
        CkJsonObject_Dispose(jsonOut);
        return;
    }

    printf("module asked for: %s pixels\n",CkJsonObject_stringOf(jsonOut,"measurements[0].requestedPitch"));
    printf("module used: %d pixels\n",CkJsonObject_IntOf(jsonOut,"measurements[0].pitch"));
    printf("image: %d x %d pixels\n",CkJsonObject_IntOf(jsonOut,"measurements[0].canvasWidth"),CkJsonObject_IntOf(jsonOut,"measurements[0].canvasHeight"));

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

    //  image

    //  The same code cannot fit in 100 x 100 with modules of at least 3 pixels.
    CkJsonObject_UpdateInt(json,"render.maxWidth",100);
    CkJsonObject_UpdateInt(json,"render.maxHeight",100);
    success = CkFormats_QrCode(fmt,"generate",json,jsonOut);
    if (success == FALSE) {
        printf("100 x 100: %s (%s)\n",CkJsonObject_stringOf(jsonOut,"errors[0].code"),CkJsonObject_stringOf(jsonOut,"errors[0].message"));
    }

    //  Sample output:

    //  module asked for: 10 pixels
    //  module used: 7 pixels
    //  image: 287 x 287 pixels
    //  100 x 100: noFit (the symbol does not fit the render constraints: minModuleSize)


    CkJsonObject_Dispose(json);
    CkFormats_Dispose(fmt);
    CkJsonObject_Dispose(jsonOut);

    }