Sample code for 30+ languages & platforms
SQL Server 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 SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @iTmp1 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @sTmp1 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, '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.
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'render.moduleSize', 10
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'render.sizing', 'shrinkToFit'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'render.minModuleSize', 3
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'render.maxWidth', 300
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'render.maxHeight', 300

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'output.path', 'qa_output/qr_fit_300.png'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'output.overwrite', 1
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'output.return', 'none'

    DECLARE @fmt int
    EXEC @hr = sp_OACreate 'Chilkat.Formats', @fmt OUT

    DECLARE @jsonOut int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonOut OUT

    EXEC sp_OAMethod @fmt, 'QrCode', @success OUT, 'generate', @json, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @fmt, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @fmt
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END


    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'measurements[0].requestedPitch'

    PRINT 'module asked for: ' + @sTmp0 + ' pixels'

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'measurements[0].pitch'

    PRINT 'module used: ' + @iTmp0 + ' pixels'

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'measurements[0].canvasWidth'

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp1 OUT, 'measurements[0].canvasHeight'

    PRINT 'image: ' + @iTmp0 + ' x ' + @iTmp1 + ' pixels'

    --  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.
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'render.maxWidth', 100
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'render.maxHeight', 100
    EXEC sp_OAMethod @fmt, 'QrCode', @success OUT, 'generate', @json, @jsonOut
    IF @success = 0
      BEGIN

        EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'errors[0].code'

        EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp1 OUT, 'errors[0].message'

        PRINT '100 x 100: ' + @sTmp0 + ' (' + @sTmp1 + ')'
      END

    --  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)

    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @fmt
    EXEC @hr = sp_OADestroy @jsonOut


END
GO