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

QR Code with a Physical Size in Millimeters at a Printer DPI

See more QR Code Examples

Demonstrates sizing a QR code for print. With render.units set to mm (or in, or pt) the module size is a physical length, and render.dpi says how many pixels the printer or the image has per inch. A module is always a whole number of pixels, because modules of uneven width scan badly, so the size asked for is rounded to the nearest pixel and the response reports the size actually used. The PNG records the dpi, so software that honors it prints the image at that physical size.

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/'

    --  Modules of 0.5 mm, for a 300 dpi printer.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'render.units', 'mm'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'render.moduleSize', '0.5'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'render.dpi', 300

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'output.path', 'qa_output/qr_300dpi.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

    --  0.5 mm at 300 dpi is 5.9 pixels, so each module is drawn 6 pixels wide.

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

    PRINT 'requested module: ' + @sTmp0 + ' pixels'

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

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

    PRINT 'module drawn: ' + @iTmp0 + ' pixels = ' + @sTmp0 + ' mm'

    EXEC sp_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'measurements[0].pitchAdjusted'
    PRINT 'size was rounded: ' + @iTmp0

    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'

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

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp1 OUT, 'measurements[0].physicalHeight'

    PRINT 'printed size: ' + @sTmp0 + ' x ' + @sTmp1 + ' mm'

    --  The image.  A web page shows it pixel for pixel.  Printed at 300 dpi it is 18.8 mm wide:

    --  image

    --  When the exact size matters more than getting an image, requireExactPitch makes a size
    --  that is not a whole number of pixels a failure (noFit) instead of rounding it.
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'render.requireExactPitch', 1
    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 'requireExactPitch: ' + @sTmp0 + ' (' + @sTmp1 + ')'
      END

    --  Sample output:

    --  requested module: 5.905512 pixels
    --  module drawn: 6 pixels = 0.508 mm
    --  size was rounded: True
    --  image: 222 x 222 pixels
    --  printed size: 18.796 x 18.796 mm
    --  requireExactPitch: noFit (the symbol does not fit the render constraints: pitchAdjusted)

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


END
GO