QR Code Generation and Save to Blob Field

slmaluwaslmaluwa Member Posts: 366
Business Central On Prem - Latest

Could some one help to create a QR code and save it to a blob or image field? Later to display it on a page as an image?
In reports, they say it is easy use a 2D font. but, here want to create and save internally.
"A bove maiore discit arare minor"-"From the old ox, the young one learns to plow."

Best Answer

  • ftorneroftornero Member Posts: 524
    Answer ✓
    Hello @slmaluwa,

    In BC OnPrem you can use dotNet, so theres is a solution:
        procedure GetQRCode(QRCodeInput: Text) QRCodeImage: text
        var
            QRCoder: DotNet QRCodeGenerator;
            Base64QR: DotNet Base64QRCode;
            QRData: DotNet QRCodeData;
            QRCodeGenECCLevel: DotNet "QRCodeGenerator+ECCLevel";
            QRCodeGenEciMode: DotNet "QRCodeGenerator+EciMode";
    
        begin
            QRCoder := QRCoder.QRCodeGenerator();
            QRData := QRCoder.CreateQrCode(QRCodeInput, QRCodeGenECCLevel.M, FALSE, FALSE, QRCodeGenEciMode.Default, -1);
            Base64QR := Base64QR.Base64QRCode(QRData);
            QRCodeImage := Base64QR.GetGraphic(3);
        end;
    

    You need to use the QRCoder DLL and this is the DotNet declaration:
        assembly("QRCoder")
        {
            type("QRCoder.QRCodeData"; QRCodeData) { }
            type("QRCoder.QRCodeGenerator"; "QRCodeGenerator") { }
            type("QRCoder.QRCodeGenerator+ECCLevel"; "QRCodeGenerator+ECCLevel") { }
            type("QRCoder.QRCodeGenerator+EciMode"; "QRCodeGenerator+EciMode") { }
            type("QRCoder.Base64QRCode"; "Base64QRCode") { }
        }	
    

    Here is the link to get the DLL
    https://github.com/codebude/QRCoder/packages/1072246

    And to use the procedure and save in a media field (QRCode) inside the table "My Table" :
    	var
    	ResTxt: Text;
            b64: Codeunit "Base64 Convert";
            InStr: InStream;
            OutStr: OutStream;
            tmpBlob: Codeunit "Temp Blob";
            Text2QrCode: Text;
             MyTable: Record "My Table";
             
            begin	
    	    Text2QrCode := 'Your data to convert in QR Code';
                ResTxt := GetQRCode(Text2QrCode);
                tmpBlob.CreateOutStream(OutStr);
                b64.FromBase64(ResTxt, OutStr);
                tmpBlob.CreateInStream(InStr);
                MyTable.QRCode.ImportStream(InStr, 'QR code', 'image/png');	
            end;	
    

    Regards.

Answers

  • slmaluwaslmaluwa Member Posts: 366
    To clarify, I am trying to learn
    How can I create a QR code image and Save as an Image to a field. Any command or codeunit available for the purpose of creating a QR code image in memory?
    "A bove maiore discit arare minor"-"From the old ox, the young one learns to plow."
  • ftorneroftornero Member Posts: 524
    Answer ✓
    Hello @slmaluwa,

    In BC OnPrem you can use dotNet, so theres is a solution:
        procedure GetQRCode(QRCodeInput: Text) QRCodeImage: text
        var
            QRCoder: DotNet QRCodeGenerator;
            Base64QR: DotNet Base64QRCode;
            QRData: DotNet QRCodeData;
            QRCodeGenECCLevel: DotNet "QRCodeGenerator+ECCLevel";
            QRCodeGenEciMode: DotNet "QRCodeGenerator+EciMode";
    
        begin
            QRCoder := QRCoder.QRCodeGenerator();
            QRData := QRCoder.CreateQrCode(QRCodeInput, QRCodeGenECCLevel.M, FALSE, FALSE, QRCodeGenEciMode.Default, -1);
            Base64QR := Base64QR.Base64QRCode(QRData);
            QRCodeImage := Base64QR.GetGraphic(3);
        end;
    

    You need to use the QRCoder DLL and this is the DotNet declaration:
        assembly("QRCoder")
        {
            type("QRCoder.QRCodeData"; QRCodeData) { }
            type("QRCoder.QRCodeGenerator"; "QRCodeGenerator") { }
            type("QRCoder.QRCodeGenerator+ECCLevel"; "QRCodeGenerator+ECCLevel") { }
            type("QRCoder.QRCodeGenerator+EciMode"; "QRCodeGenerator+EciMode") { }
            type("QRCoder.Base64QRCode"; "Base64QRCode") { }
        }	
    

    Here is the link to get the DLL
    https://github.com/codebude/QRCoder/packages/1072246

    And to use the procedure and save in a media field (QRCode) inside the table "My Table" :
    	var
    	ResTxt: Text;
            b64: Codeunit "Base64 Convert";
            InStr: InStream;
            OutStr: OutStream;
            tmpBlob: Codeunit "Temp Blob";
            Text2QrCode: Text;
             MyTable: Record "My Table";
             
            begin	
    	    Text2QrCode := 'Your data to convert in QR Code';
                ResTxt := GetQRCode(Text2QrCode);
                tmpBlob.CreateOutStream(OutStr);
                b64.FromBase64(ResTxt, OutStr);
                tmpBlob.CreateInStream(InStr);
                MyTable.QRCode.ImportStream(InStr, 'QR code', 'image/png');	
            end;	
    

    Regards.
  • slmaluwaslmaluwa Member Posts: 366
    ftornero wrote: »
    Hello @slmaluwa,

    In BC OnPrem you can use dotNet, so theres is a solution:
        procedure GetQRCode(QRCodeInput: Text) QRCodeImage: text
        var
            QRCoder: DotNet QRCodeGenerator;
            Base64QR: DotNet Base64QRCode;
            QRData: DotNet QRCodeData;
            QRCodeGenECCLevel: DotNet "QRCodeGenerator+ECCLevel";
            QRCodeGenEciMode: DotNet "QRCodeGenerator+EciMode";
    
        begin
            QRCoder := QRCoder.QRCodeGenerator();
            QRData := QRCoder.CreateQrCode(QRCodeInput, QRCodeGenECCLevel.M, FALSE, FALSE, QRCodeGenEciMode.Default, -1);
            Base64QR := Base64QR.Base64QRCode(QRData);
            QRCodeImage := Base64QR.GetGraphic(3);
        end;
    

    You need to use the QRCoder DLL and this is the DotNet declaration:
        assembly("QRCoder")
        {
            type("QRCoder.QRCodeData"; QRCodeData) { }
            type("QRCoder.QRCodeGenerator"; "QRCodeGenerator") { }
            type("QRCoder.QRCodeGenerator+ECCLevel"; "QRCodeGenerator+ECCLevel") { }
            type("QRCoder.QRCodeGenerator+EciMode"; "QRCodeGenerator+EciMode") { }
            type("QRCoder.Base64QRCode"; "Base64QRCode") { }
        }	
    

    Here is the link to get the DLL
    https://github.com/codebude/QRCoder/packages/1072246

    And to use the procedure and save in a media field (QRCode) inside the table "My Table" :
    	var
    	ResTxt: Text;
            b64: Codeunit "Base64 Convert";
            InStr: InStream;
            OutStr: OutStream;
            tmpBlob: Codeunit "Temp Blob";
            Text2QrCode: Text;
             MyTable: Record "My Table";
             
            begin	
    	    Text2QrCode := 'Your data to convert in QR Code';
                ResTxt := GetQRCode(Text2QrCode);
                tmpBlob.CreateOutStream(OutStr);
                b64.FromBase64(ResTxt, OutStr);
                tmpBlob.CreateInStream(InStr);
                MyTable.QRCode.ImportStream(InStr, 'QR code', 'image/png');	
            end;	
    

    Regards.

    Thank you very much. Really appreciate your help.

    I didn't just copy paste. Your post helped me to learn

    1. How to build a .net dll (project from GIT forced me to download Visual Studio 2022)
    2. Where to put that DLL in the server (ad-ins folded)
    3. Where to place the dll in the VS Code folder (.netpackages)
    4. How to declare a dotnet assembly and how to refer it from other pages.
    5. create QR code, store it as TEXT and convert to image and save to a image field.
    and more.

    Thank you once again

    "A bove maiore discit arare minor"-"From the old ox, the young one learns to plow."
Sign In or Register to comment.