Tech Articles


Generating a QR code using QRCoder


QRCoder is an open source .Net assembly for creating QR Codes.  What we're going to do is wrap that with an assembly in SnapDevelop we can use from PowerBuilder.  First thing we need to do is create a .Net standard Class Library project in SnapDevelop.


Once we've done that we go into the Nuget Package Manager and install the QRCoder library.

In our wrapper assembly, we just need to add a few lines of code 

 public string GenerateQRCode(string qrText)   
     {  
       QRCodeGenerator qRCodeGenerator = new QRCodeGenerator();  
       QRCodeData qRCodeData = qRCodeGenerator.CreateQrCode(qrText, QRCodeGenerator.ECCLevel.Q);  
       PngByteQRCode pngByteQRCode = new PngByteQRCode(qRCodeData);  
       byte[] qrCodeBytes = pngByteQRCode.GetGraphic(20);  
       string qrCodeString = Convert.ToBase64String(qrCodeBytes);  
       return qrCodeString;  
     }  

A couple of things to note here.  first, if you look at the QRCoder documentation the sample they give involves the base QRCode class and converts the result to a bitmap.  Since I want to pass this back to PowerBuilder as a byte array or, as I've done here, as a Base64 encoded string, converting the result is a bitmap first is an intermediate step I don't want or need.  So I used the PngByteQRCode class instead to get a byte array   Second, I am converting the byte array to a Base64 string.  You can return a byte array directly from the assembly, and the .Net Importer will convert it to a blob automatically.  The issue is that for debugging purposes I can't see inside of a blob variable in the PowerBuilder debugger.  So if something goes wrong I'm not sure what I got back.  By passing back a Base64 encoded string to PowerBuilder and then converting it to a blob in PowerScript I have better insight when debugging.

After you've compiled that, lets switch over to PowerBuilder 2019 R3 and use it.  I created an target that contains a window that has a single line edit (sle_1) to enter text, a picture control (p_1_ to display the bitmap and a command button to run the script needed to generate the QR code image using the assembly.  Now we need to run the .Net Assembly importer to import our wrapper assembly.

As usual with a window this is going to call a .net assembly using the importer I do the following in order to facilitate error handling.

1.  Declare the .net assembly wrapper object as an instance variable on the window 

 nvo_qrgenerator inv_generator  

 2.  Instantiate the wrapper object in the open event and register the error handler to an event on the window 

 inv_generator = Create nvo_qrgenerator  
 inv_generator.of_seterrorhandler( this, 'assemblyerror')  

 3.  Destroy the instance variable in the close event of the window 

 Destroy inv_generator   

 4.  Create a custom event on the window to use as the error handler, and display any error messages relayed by the .net assembly wrapper 

 MessageBox ( "Error", inv_generator.is_errortext )  

 With that done we're ready to add the code to call the wrapper object and put the generated QR code in the picture control: 

 string          ls_text  
 string          ls_base64  
 blob          lblb_qrcode  
 CoderObject          co  
 p_1.visible = FALSE  
 ls_text = sle_1.Text  
 ls_base64 = inv_generator.of_generateqrcode( ls_text )  
 co = create CoderObject  
 lblb_qrcode = co.base64decode(ls_base64)  
 destroy co  
 p_1.setpicture( lblb_qrcode )  
 p_1.visible = TRUE  

 That's it.  Enter some text, click the command button and you've got a QR code

There's a bunch of options that I haven't touched on such as generating the QR code in color or adding a custom image to the center of the QR code.  I'll let you explore those on your own.   If you want to skip performing all these steps and just get started with the code, I've uploaded it to CodeXchange.

 

Comments (14)
Tuesday, Apr 20 2021

Hi Bruce,
Thank you very much for the code.

I'm trying to use the "qrcodedemo" api with PB 2019 R3. I changed the path of the dll file to my personal folder. Clicking to generate the QRCode that would run the DLL gives the error:

"Call GerenateQRCode failed.
Error Text: Error calling external object function generatedqrcode. Do not possible load the file or assembly 'QRCoder, Version = 1.4.1.0 ... "

I'm using Windows 10.
dotnet:
.netcore.app 5.0.5
.aspnetcore.app 5.0.5
.widnwosdesktop.app 5.0.5

dotnet sdk 5.0.202

I would like a help to resolve the error please.

0

Thursday, Apr 22 2021

I'm not sure what you mean by "I changed the path of the dll file to my personal folder". Do you mean you changed the location that was included in the nvo_qrgenerator object?

Also, the QRCodeGenerator.dll file is just a wrapper around the QRCoder.dll. Do you have both in the location where you are attempting to load the assembly?

0

Thursday, Sep 02 2021

Hai Bruce,
What is the reason for the below error message?
I completed this route as save you explained at the top...
but, getting error...

"Could not load file or assembly 'netstandard Version=2.1.0.0, Culture-neutral,PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified"

0
Thursday, Sep 02 2021

Generally this means your project is targeting one version of the .net framework and you've reference a library that targets a different version.

0
Thursday, Sep 02 2021

Let me check and thanks for your reply...

0

Thursday, Feb 17 2022

Great Bruce, thanks for your contributions to the community.

I have recently created a .Net library based on Zxing for the generation of barcodes of different types, including Qr.

The difference is that in addition to generating codes, it has the interesting option of reading them.

You can see my example here:

https://community.appeon.com/index.php/codeexchange/powerbuilder/309-qrcode-reader-and-qrcode-generator-with-zxing

0

Friday, Mar 25 2022

Hello
This is my first attempt to use SnapDevelop and here's my experience with this example. Information here may help beginners to avoid some try and fail attempts.
I use PB 2021
1. Create a new project based on Class Library .NET Standard as it's mentioned above.
2. In menu "Tools - Nuget Package Manager" -> "Manage Nuget Package for Solution" look for package "QRCoder". On the right side click the checkbox left to your project. Then click Install.
3. In menu "Project" -> "<your project> Properites" switch "Target framework" to ".NET Standard 2.0"
3. In your already generated .cs file insert row "using QRCoder;" at the end of the using block.
4. Put the code mentioned in the article into the pre-generated class.
5. Click menu Build -> Build Solution.
6. Create an export folder (somewhere, doesn't matter).
7. In Solution Explorer rclick the project, choose "Publish". Select a proper "target location", "configuration", "target framework" (again ".NET Standard 2.0"). Then follow the wizard. This step helps you solve dependencies.
8. Take all the files newly generated in the export folder from step 6. and copy them to the folder with your PB project.
9. Start PB. In menu Tools select ".NET DLL importer". Choose your DLL. Check whole DLL, click Import.
10. Move your newly created nvo from the app (project) library to a different library.
11. Remove the absolute path from the nvo instance variable named "is_assemblypath". Only filename must stay here for correct deployment.
11. Comment //inv_generator.of_seterrorhandler( this, 'assemblyerror') from the code in the article because there's no such method.
12. Open your PowerClient project. Go to 'External files' tab. Add all exported files here.

Notes:
Don't use p_1.OriginalSize attribute because picture object gets crazy after loading from blob through p_1.setpicture(lblb_blb).
Once you touch the .NET DLL in PB the DLL and the folder containing this DLL is being locked until you close PB. Never start SnapDevelop directly from PB because if you close PB it closes SnapDevelop.

0

Monday, Jul 03 2023

Hello,

when i use this example within PB2022 and SnapDevelop2022, i only get a nugetpackage in the publish folder.
No "ORCodeGenerator.dll" and "ORCoder.dll" in the publish folder for the solution.
How can i use the PB .net DLL importer with a NuGet Package?

BR

Uwe

0

Tuesday, Jul 04 2023

Works fine for me. I downloaded the example, extracted it and deleted the the existing QRCodeGenerator.dll, both from the root directory and from the QRCodeGenerator\bin\Debug\netstandard2.0 directory. I then recompiled the sample using SnapDevelop 2022. I then copied the newly created QRCodeGenerator.dll in the QRCodeGenerator\bin\Debug\netstandard2.0 folder back into the root directory for the PowerBuilder application to find.

I then opened the PowerBuilder application, migrated it to 2022 and re-ran the .Net Importer. The only code change I had to make was to remove the code at line 2 of the open event of the w_main object. Apparently that line of code was a function that existed when I first wrote the demo, but is not in more recent versions of PowerBuilder. Once I did that, the sample ran fine.

P.S. I've uploaded a 2022 version of the sample to the original CodeXchange article.

Comment was last edited 1 year ago by Bruce Armstrong
0

Tuesday, Aug 29 2023

i got the error (could not load file or assembly 'QRcoder,version=1.4.3.0, Culture=neutral,PublicKeyToken=c4ed5b9ae8358a28'.A strongly-named assembly is required.(0x80131044)) at line 18 in function of_generateqrcode() of object nvo_myclass.... please anyone help me for this error to get the solution .

0
Wednesday, Aug 30 2023

Can you give any more details? I just grabbed the 2022 version of the sample code from CodeXchange, opened it in 2020 R2 GA and ran it and it ran without error.

0

Friday, Oct 13 2023

I wrote in Visual Studio 2019 C# and SnapDevelop a library that generates a QR code, then a 64-bit application in PB 2022 R2 and it works fine on my computer where I have installed PowerBuilder 2022 R2 and Visual Studio 2019 (attached image), but when I install on a client computer it doesn't work (attached image). This is the list of PB2022 libraries that I deploy on the client, in fact I copied the whole folder: C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.281919\x64 to the client and it doesn't work, I can't find what could be missing, I can't find what I need to install on the client.

[Files]
Source: "C:\_DESARROLLO\EjemPB\GeneraQR\generaqr.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\_DESARROLLO\EjemPB\GeneraQR\GeneraQR.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\_DESARROLLO\EjemPB\GeneraQR\QRCoder.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\AtlAuxiliary.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\libcurl.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\msvcp140.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbacc.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\PBAccessibility.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbase.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbcompression.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbcrypt.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbdotnet.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbdotnetcoreinvoker.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbdotnetframeworkinvoker.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbdotnetinvoker.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbdpl.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbdwe.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbDWExcel12Interop.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbhttpclient.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbjson.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbmsoledbsql.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\PBMSTEXT.DLL"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbo10.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbo90.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pboauth.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbodb.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbora.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbPDF.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbpdfbuilder.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbrestclient.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbresource.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbribbonbar.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbshr.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbsysfunc.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbtheme.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbtra.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbUIS.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbvm.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbwebbrowser.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbXerces.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pdflib.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\vcruntime140.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\vcruntime140_1.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\xerces-c.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Program Files (x86)\Appeon\Common\PowerBuilder\Runtime 22.1.0.2819\x64\pbodb.ini"; DestDir: "{app}"; Flags: ignoreversion

Attachments

Your account does not have privileges to view attachments in the comment

0

Monday, Oct 16 2023

Hi,
Try to use LoadWithDotNetFramework function. LoadWithDotNet doesn't work for me in 2819.

0
Monday, Oct 16 2023

I solved it but it was not easy, I had to do it in Visual C# to generate QRCoder.dll, because SnapDevelop does not generate the QRCoder.dll library.

0

Find Articles by Tag

WinAPI UI Themes TortoiseGit SnapObjects OrcaScript IDE Branch & Merge Event PowerServer Mobile Jenkins PBDOM SOAP Service DataWindow JSON Database Connection TreeView Design Repository DataWindow Outlook REST ODBC MessageBox Migration Web Service Proxy Automated Testing Variable External Functions .NET DataStore Testing Export SVN Oracle Database Table Schema SqlModelMapper Windows 10 Icons Import Resize Script .NET Assembly Syntax Database Table Text Azure Validation DragDrop Debugger PowerBuilder Compiler Linux OS .NET Std Framework 32-bit Performance UI Modernization Debug Deployment SnapDevelop License PostgreSQL ODBC driver RibbonBar Platform RichTextEdit Control UI Database Profile SqlExecutor Messagging Installation Graph JSONParser Event Handling PDFlib Charts COM DataType 64-bit JSONGenerator Windows OS Database Painter TLS/SSL PowerBuilder (Appeon) CI/CD Data Model Elevate Conference SQL Event Handler RESTClient DLL Configuration PostgreSQL NativePDF Authentication Trial Open Source Source Control Sort Git Source Code iOS Application InfoMaker Mobile Excel TFS Android HTTPClient ActiveX Web API PowerBuilder Menu GhostScript RibbonBar Builder Debugging PDF C# DevOps Filter Bug SDK WebBrowser PowerServer Web OAuth 2.0 Authorization XML PowerScript (PS) JSON File OAuth Stored Procedure Export JSON BLOB Transaction Interface Class Encryption Database OLE Expression Database Object CrypterObject Array SQL Server Encoding API Import JSON CoderObject Icon Error PFC Window Database Table Data