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

Get the Modules of a QR Code (Bit Matrix)

See more QR Code Examples

Demonstrates getting the QR code itself, as a grid of dark and light modules, to draw with something other than the built-in PNG and SVG output: on a canvas, in a PDF, on a display, or with a label printer language. With output.includeModuleData the response has symbols[0].moduleData, a bit matrix with one bit per module, each row starting on a new byte, most significant bit first, 1 for dark. The quiet zone is not part of the matrix. The example prints the symbol using two characters per module.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 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', 'CHILKAT'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'output.includeModuleData', 1

    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

    DECLARE @width int
    EXEC sp_OAMethod @jsonOut, 'IntOf', @width OUT, 'symbols[0].moduleData.width'
    DECLARE @height int
    EXEC sp_OAMethod @jsonOut, 'IntOf', @height OUT, 'symbols[0].moduleData.height'
    DECLARE @rowBytes int
    EXEC sp_OAMethod @jsonOut, 'IntOf', @rowBytes OUT, 'symbols[0].moduleData.rowBytes'

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'symbols[0].moduleData.kind'
    PRINT 'kind: ' + @sTmp0




    PRINT 'modules: ' + @width + ' x ' + @height + ', ' + @rowBytes + ' bytes per row'

    --  The matrix is height rows of rowBytes bytes.
    DECLARE @bd int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'symbols[0].moduleData.bytesBase64'
    EXEC sp_OAMethod @bd, 'AppendEncoded', @success OUT, @sTmp0, 'base64'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @bd, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @fmt
        EXEC @hr = sp_OADestroy @jsonOut
        EXEC @hr = sp_OADestroy @bd
        RETURN
      END

    DECLARE @sbRow int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbRow OUT

    DECLARE @row int

    DECLARE @col int

    DECLARE @b int

    DECLARE @mask int
    SELECT @mask = 128
    SELECT @row = 0
    WHILE @row <= @height - 1
      BEGIN
        EXEC sp_OAMethod @sbRow, 'Clear', NULL
        SELECT @col = 0
        WHILE @col <= @width - 1
          BEGIN
            --  The module at (col,row) is a bit of the byte at (row * rowBytes) + (col / 8).
            --  The leftmost module of each byte is its most significant bit.
            IF (@col % 8) = 0
              BEGIN
                SELECT @mask = 128
              END
            EXEC sp_OAMethod @bd, 'GetByte', @b OUT, (@row * @rowBytes) + (@col / 8)
            IF ((@b / @mask) % 2) = 1
              BEGIN
                EXEC sp_OAMethod @sbRow, 'Append', @success OUT, '##'
              END
            ELSE
              BEGIN
                EXEC sp_OAMethod @sbRow, 'Append', @success OUT, '  '
              END
            SELECT @mask = @mask / 2
            SELECT @col = @col + 1
          END
        EXEC sp_OAMethod @sbRow, 'GetAsString', @sTmp0 OUT
        PRINT @sTmp0
        SELECT @row = @row + 1
      END

    --  The same symbol as the PNG that the call also returned:

    --  image

    --  Sample output:

    --  kind: bitMatrix
    --  modules: 21 x 21, 3 bytes per row
    --  ##############              ##############
    --  ##          ##      ####    ##          ##
    --  ##  ######  ##  ##  ####    ##  ######  ##
    --  ##  ######  ##  ##          ##  ######  ##
    --  ##  ######  ##  ##  ##  ##  ##  ######  ##
    --  ##          ##  ####  ##    ##          ##
    --  ##############  ##  ##  ##  ##############
    --                  ####
    --  ##  ##########      ####    ##########
    --      ####          ##  ########    ######
    --  ####  ########    ####  ##  ####      ####
    --        ##  ##      ############    ####  ##
    --    ######  ####  ######  ##    ##  ##
    --                  ##  ##  ##    ##  ##    ##
    --  ##############        ##  ##    ##      ##
    --  ##          ##  ##  ##        ######  ####
    --  ##  ######  ##  ####  ##  ##    ##
    --  ##  ######  ##  ##  ############    ##
    --  ##  ######  ##  ##      ##  ####  ##
    --  ##          ##    ##  ########          ##
    --  ##############  ##      ##      ######

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


END
GO