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

Embed a QR Code in HTML as a Data URI

See more QR Code Examples

Demonstrates putting a QR code directly into an HTML page or HTML email with no separate image file. The base64 PNG returned by QrCode becomes the src of an img tag as a data URI, which is the MIME type from outputs[0].mimeType followed by the base64 from outputs[0].base64.

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 @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/'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'render.moduleSize', 5

    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

    --  A data URI has the form  data:<MIME type>;base64,<base64 data>
    DECLARE @sbHtml int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbHtml OUT

    EXEC sp_OAMethod @sbHtml, 'Append', @success OUT, '<html><body><p>Scan to visit chilkatsoft.com</p><img alt="QR code" width="'
    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'outputs[0].width'
    EXEC sp_OAMethod @sbHtml, 'AppendInt', @success OUT, @iTmp0
    EXEC sp_OAMethod @sbHtml, 'Append', @success OUT, '" height="'
    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'outputs[0].height'
    EXEC sp_OAMethod @sbHtml, 'AppendInt', @success OUT, @iTmp0
    EXEC sp_OAMethod @sbHtml, 'Append', @success OUT, '" src="data:'
    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'outputs[0].mimeType'
    EXEC sp_OAMethod @sbHtml, 'Append', @success OUT, @sTmp0
    EXEC sp_OAMethod @sbHtml, 'Append', @success OUT, ';base64,'
    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'outputs[0].base64'
    EXEC sp_OAMethod @sbHtml, 'Append', @success OUT, @sTmp0
    EXEC sp_OAMethod @sbHtml, 'Append', @success OUT, '"></body></html>'

    EXEC sp_OAMethod @sbHtml, 'WriteFile', @success OUT, 'qa_output/qr_page.html', 'utf-8', 0
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sbHtml, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @fmt
        EXEC @hr = sp_OADestroy @jsonOut
        EXEC @hr = sp_OADestroy @sbHtml
        RETURN
      END


    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'outputs[0].width'

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp1 OUT, 'outputs[0].height'

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

    EXEC sp_OAGetProperty @sbHtml, 'Length', @iTmp0 OUT
    PRINT 'HTML length: ' + @iTmp0

    --  The page, as a browser shows it:

    --  image

    --  Sample output:

    --  image: 185 x 185 pixels
    --  HTML length: 1211

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


END
GO