# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  [VB6/VBA] QR Code generator library

## wqweto

*mdQRCodegen.bas* is a single file no dependency pure VB6 implementation of a QR Code Generator based on https://www.nayuki.io/page/qr-code-generator-library

 

Just add *mdQRCodegen.bas* to your project and call *QRCodegenBarcode* function to produce QR Code barcodes as vector based StdPicture objects.

cheers,
</wqw>

----------


## Elroy

Very nice.  I tried it with a few ASCII phrases, and my phone seemed to correctly translate them.

----------


## yokesee

good job work perfect
 :Smilie:

----------


## bgerasimoski

Thanks for sharing. Nice work!  :Smilie:

----------


## saturnian

Very nice code !
I made a small modification: I transformed the LNG_STEP constant into a variable to be able to modify the size of the QR code and copy it to the clipboard. 
This then makes it possible to insert it into a Word document, for example, or any other application...

VbQRCodegen.zip

Many thanks wqweto !

François

----------


## wqweto

> Very nice code !
> I made a small modification: I transformed the LNG_STEP constant into a variable to be able to modify the size of the QR code and copy it to the clipboard. 
> This then makes it possible to insert it into a Word document, for example, or any other application...
> 
> VbQRCodegen.zip
> 
> Many thanks wqweto !
> 
> François


Fixed in Allow using custom ModuleSize and reduce default to 10 (from 500) commit.

Thanks!

cheers,
</wqw>

----------


## georgekar

Thanks wqweto, for the code
I used it for M2000 Interpreter
https://georgekarras.blogspot.com/20...11-qrcode.html
Attachment 185995

----------


## Tizio

thanks for sharing your projects. Great work

----------


## Episcopal

As the slang says here .... really crazy .... or great tool. WQWETO: How do I convert the image to text?

----------


## georgekar

Use your phone scanner

----------


## wqweto

> As the slang says here .... really crazy .... or great tool. WQWETO: How do I convert the image to text?


Why would you want to do that?

The idea of any barcode is to be read by a barcode scanner device. These are wands with laser beam used at cash registers and warehouses to scan barcodes on goods, packages, crates or whatever.

Incidentally most modern smartphone's camera software allows the same to be used as a barcode scanner device too. This is how I tested the output of mdQRCodegen.bas -- by pointing my phone at the screen to test read the contents of it.

cheers,
</wqw>

----------


## Elroy

> As the slang says here .... really crazy .... or great tool. WQWETO: How do I convert the image to text?


Maybe he wants the opposite of what you did, Wqweto.  To take a scanned picture and convert it to text with VB6 code.  That'd actually be quite the project.  Find the orientation corners, rotate the picture to "square it up".  Resize it to some standardized size.  Then start going through the QR "pixels", seeing if they lean more toward white or black.  And then do the reverse of what Wqweto's posted code does.  Argh.

----------


## jpbro

Here are a couple of projects by Olaf that can decode QR codes using RC6 (and libQuirc under the hood):

https://www.vbforums.com/showthread....Code-Decoding)

https://www.vbforums.com/showthread....ow-Positioning

----------


## Episcopal

> Maybe he wants the opposite of what you did, Wqweto.  To take a scanned picture and convert it to text with VB6 code.


Exactly.... Just like Wqweto did with pure code, I thought the opposite too. On the PSC (+-2015) before going offline there was an example of this in VB6... but in the GitHub repository it didn't reach it.

I must have a backup, because I formatted the pc from that time to now .... if I find it, I'll come back here .....

I have a source in C++..... by Phil Karn

----------


## wqweto

Just curious where do you plan on using such QR Code recognition? I cannot find a use-case for any practical purposes.

Where do you have to process pictures with QR Codes, recognize there are QR Codes in view, extract the QR Codes and act on what is encoded?

You must be involved in some really interesting projects that are beyond my imagination :-))

cheers,
</wqw>

----------


## Episcopal

I'm not involved in projects, as you mentioned. As you were curious, I was also curious to know and your module did the reverse .... then I came back here and asked ...

I downloaded a qrcode reader and saw that its module works perfectly......

----------


## wqweto

I've never worked on or even seen a LOB application which needs OCR (Optical Character Recognition) of some kind for instance but probably QR Code recognition of scanned documents is more common though I highly doubt it.

Another option is for the app to somehow handle video input from cameras but this would be rarely the case for a LOB application to fiddle with my laptop camera. I would be pretty scared if it did frankly.

In any case why is QR Code recognition a thing outside specialized camera or scanner software is beyond me but yes, there are some libraries which generate QR Codes *and* can recognize QR Codes from images (which a completely different algorithm) however surprising such a feature might be.

Alas QR Code recognition is never going to be supported by this QR Code generator library in a single .bas module as posted in the current thread.

cheers,
</wqw>

----------


## Schmidt

> ...probably QR Code recognition of scanned documents is more common...


Yep, we use the RC5/RC6 QRDecode-stuff in a LOB-App like that.

The scenario is: "personalized questionary-printouts" ...
(for in-house participants at Seminars, who all receive "a Page" with a QR-encoded "participant-ID" in the TopLeft-corner):
 - which are later on (after the seminar was finished) fed "unordered" into a document-scanner
- where the bunch of pages are scanned and converted to Tiffs directly, and placed on a Network-Share
- from there we then read-out the TopLeft-Corner-Bitmap-part, QR-decode it ...
- to be able to associate the "right file with the right participant" via a DB-Insert finally

Olaf

----------


## wqweto

Yes, seems a possible use-case.



Nowadays I bet you'd want the whole questionnaire to be online in first place.

Edit: Btw, is RC5/RC6 QR Code gen/decode stuff a VB6 implementation?

cheers,
</wqw>

----------


## jpbro

Another use would be at a construction site - you generate and post a unique QR code at each room/floor/whatever unit you like, and when it's time to do a site/progress review the reviewer can walk around with their Surface and snap QR codes as they go. As they type their report and take pictures of any deficiencies, everything will automatically be linked to specific locations, with no chance to type/pick the wrong room #.

----------


## Schmidt

> Yes, seems a possible use-case.
> Nowadays I bet you'd want the whole questionnaire to be online in first place.


We suggested this at the time, using a "BYOD-scheme" (where Users fill out the form on their own SmartPhones),
but our customer said "No" (because they have always a percentage of "older participants", which might not be able to configure their Phones for that - or didn't even own one).

Then we suggested to "just buy 30 cheap ARM-Tablets, which ca be pre-configured" - 
but this was denied as well ("too expensive").

So, "Paper it was"...  :Wink: 




> Btw, is RC5/RC6 QR Code gen/decode stuff a VB6 implementation?


Here's an excerpt from _Library-Licenses.txt (as included in the RC6-BaseDlls-package):


```
   - libqrencode (the "quasi-standard" by Kentaro Fukuchi)
     http://fukuchi.org/works/qrencode/
     LGPL 2.1 license (https://www.gnu.org/licenses/lgpl-2.1.html)
     
   - quirc-library (small and efficient QR-Decoding by Daniel Beer)
     https://github.com/dlbeer/quirc
     It's a "personalized license" which comes below:
     Copyright (C) 2010-2012 Daniel Beer <dlbeer@gmail.com>
        Permission to use, copy, modify, and/or distribute this software for
        any purpose with or without fee is hereby granted, provided that the
        above copyright notice and this permission notice appear in all
        copies.

        THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
        WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
        WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
        AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
        DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
        PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
        TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
        PERFORMANCE OF THIS SOFTWARE.
```

HTH

Olaf

----------


## Elroy

Personally, I think the work that Wqweto has provided herein is fantastic.

And he further stated...




> Alas QR Code recognition is never going to be supported by this QR Code generator library in a single .bas module as posted in the current thread.


So Episcopal, we'll be watching for your CodeBank entry to interpret scanned QR codes.   :Stick Out Tongue: 

And, truth be told, this all sounds like it should be a separate thread in the regular VB6 Q&A section.

----------


## wqweto

In latest update QR codes generated are with fancy rounded modules by default (inspired by Telegram activation codes)



Surprisingly such eye-candy does not render barcodes unreadable :-))

MS Access users might want to use new *QRCodegenConvertToData* function with the native Image control like this



```
Image0.PictureData = QRCodegenConvertToData(QRCodegenBarcode("https://www.vbforums.com"), 500, 500)
```

. . . to feed *PictureData* property with a raw byte-array of BMP rescaled with anti-aliasing to 500x500 pixels.

cheers,
</wqw>

----------


## DaveDavis

so advance code, please educate me how it works? I couldn't find any dll file of the nayuki.io reference but it works like magic.

----------


## georgekar

How we get the picture from byte array later?

----------


## wqweto

> How we get the picture from byte array later?


Take a look at *Form1.frm* source code, there is no need to use byte-arrays in VB6 because you can just use *Set Image1.Picture = QRCodegenBarcode("Sample Text")* to directly display the vector image.

In MS Access it's not possible to use StdPicture like this so they have to use *Image0.PictureData = QRCodegenConvertToData(QRCodegenBarcode("Sample Text"))* instead.

If you want to rasterize the vector image returned from *QRCodegenBarcode* for one reason or another you can use *QRCodegenResizePicture* on the returned StdPicture.

If you want to rasterize the vector and anti-alias the result (by super sampling w/ StretchBlt in HALFTONE mode) then you have to use *QRCodegenResizePicture* twice like this:



```
    '--- from QRCodegenConvertToData source code 

    If NewWidth > 0 And NewHeight > 0 Then
        '--- super sample to 4x4 for cheap anti-aliasing
        Set pPicture = QRCodegenResizePicture(pPicture, NewWidth * 4, NewHeight * 4)
        Set pPicture = QRCodegenResizePicture(pPicture, NewWidth, NewHeight)
    End If
```

cheers,
</wqw>

----------


## DaveDavis

> so advance code, please educate me how it works? I couldn't find any dll file of the nayuki.io reference but it works like magic.


so curious how it works, I can't figure out where is the library.

----------


## wqweto

> so curious how it works, I can't figure out where is the library.


It's a single standard module library. There is no external dependency as the original C code is completely translated to VB6.

With standard BAS modules you can only use whichever procedures you need and the ones you don't call will be removed by the linker from the final binary (which is not the case with class modules, forms and user-controls).

cheers,
</wqw>

----------


## Alan Lamb

I've been exploring the new QRCodegenConvertToData (wow - and thank you) to push the data into the native Access image control



```
Image1.PictureData = QRCodegenConvertToData(mdQRCodegen.QRCodegenBarcode("https://www.vbforums.com"))
```

The above code works as expected, I plan to experiment and see if I can get the native controls to also bind to a control source.



```
Image1.PictureData = QRCodegenConvertToData(mdQRCodegen.QRCodegenBarcode("https://www.vbforums.com", SquareModules:=True), 500, 500)
```

The code above with square modules works (mostly)
It (curiously) rotates the QR code, and, if width and height are omitted, itfails to draw the QR code

----------


## georgekar

Something going wrong with the curved one. Just check the image (using clipboard to pass it), to Word 365, and check the print preview. Lines between small squares are not white (so we can see it). Only M2000 environment can render to bitmap, first by exporting to a memory buffer as emf file and then using GDI plus for rendering.

----------


## wqweto

10x, Ill research this compatibility issue.

----------


## wqweto

Just fixed the obvious issues with WinWord and MS Paint in commit 640be96 but there is still some "halo" effect in office applications due to the anti-aliasing these perform with (probably) a custom EMF playback implementation.

To remove this "halo" effect I'll have to come up with completely different renderer which solves for a minimum set of non-overlapping black polygons which when composited will produce the verbatim image of the QR code.

cheers,
</wqw>

----------


## wqweto

JFYI, the all new polygons based renderer in commit 138cea2 produces vector EMFs which are displayed very well both in WinWord and MS Paint (in both square and rounded modules modes).



cheers,
</wqw>

----------


## georgekar

Bravo, its ok, with Word 365, Paint, Inkspace and always in M2000 Interpreter (which included)

----------


## couttsj

Great work wqweto. Attached is a version modified to use as a Class module.

J.A. Coutts

----------


## wqweto

Btw, there is one clear benefit to using standard modules for shared "libraries" -- the procedures which are not called from anywhere in the client project a .bas file is included into are not compiled in final executable due to a simple link-time optimization.

With a class module you get *every* procedure compiled into final executable *always* because public (and private) methods are referenced from its VTable as emitted from the VB compiler -- bummer!

cheers,
</wqw>

----------


## Elroy

> Btw, there is one clear benefit to using standard modules for shared "libraries" -- the procedures which are not called from anywhere in the client project a .bas file is included into are not compiled in final executable due to a simple link-time optimization.


I did not know that, and it's good to know.  And another advantage of a BAS over a one-time-CLS is that you don't have to jump through the hoops of adding the ObjPtr to the argument list and you don't have to deal with rearranging the HRESULT return and the actual return of functions.

For one-time-CLS modules, the only advantage I see is that they have Initialize and Terminate events, which are admittedly handy.

----------


## wqweto

For "library" code i.e. code which will be reused in  multiple projects an optimal strategy is to implement everything as a BAS file with a lot of public procedures which hopefully get trimmed (together with their dependent private procedures which are used only by any trimmed ones) if unused in a particular project.

So instead of implementing Compress procedure with an EncryptStrength parameter and conditionally call pvEncrypt procedure do separate public Compress and Encrypt procedures so that encryption procedure (and all its dependant private ones) can be trimmed by the linker if compression is used without encryption i.e. the design of "library" code is important too.

cheers,
</wqw>

----------


## Episcopal

> Btw, there is one clear benefit to using standard modules for shared "libraries" -- the procedures which are not called from anywhere in the client project a .bas file is included into are not compiled in final executable due to a simple link-time optimization.


Wqweto ... This includes all public methods?

----------

