Recent

Author Topic: [Solved] Looking for QRCODE matrix generator  (Read 4069 times)

Fibonacci

  • Hero Member
  • *****
  • Posts: 647
  • Internal Error Hunter
[Solved] Looking for QRCODE matrix generator
« on: September 17, 2023, 05:21:36 am »
It must just make 2D array with 0 and 1. It must have no dependencies (0 units in uses section, maybe just SysUtils allowed).

It doesnt need to have all features implemented. Just standard text to QR code, one version, one error correction level.

Is there something like that?

If not I will port PHP code, it looks simple
https://github.com/psyon/php-qrcode/blob/master/qrcode.php#L532

Another simple one-file code, in C
https://github.com/nayuki/QR-Code-generator/blob/master/c/qrcodegen.c

Compiled binary just 38 kB and works well, I think I will use it as static linked library in my Pascal app.

My phone recognized valid QR code printed in console :D
« Last Edit: September 17, 2023, 07:20:04 pm by Fibonacci »

Mongkey

  • Sr. Member
  • ****
  • Posts: 435
Re: Looking for QRCODE matrix generator
« Reply #1 on: September 17, 2023, 05:35:44 am »
This would help you a lot, you could do it vice versa with pascal app -> bringing all zxing capabilities:
https://forum.lazarus.freepascal.org/index.php/topic,64616.0.html
« Last Edit: September 17, 2023, 05:41:26 am by Mongkey »

Fibonacci

  • Hero Member
  • *****
  • Posts: 647
  • Internal Error Hunter
Re: Looking for QRCODE matrix generator
« Reply #2 on: September 17, 2023, 05:49:26 am »
This would help you a lot:
https://forum.lazarus.freepascal.org/index.php/topic,64616.0.html

How? Its Pascal code calling Java bridge which imports many modules including QR generator. I need pure Pascal code that does all the job. No libraries, no imports. Exception would be statically linked library very small in size and without any other dependencies.

To be even more clear: cant use classes, nothing in uses section except for SysUtils.

« Last Edit: Today at 05:41:26 am by Mongkey »
bringing all zxing capabilities

Thats a problem. No libraries.
« Last Edit: September 17, 2023, 05:52:39 am by Fibonacci »

Mongkey

  • Sr. Member
  • ****
  • Posts: 435
Re: Looking for QRCODE matrix generator
« Reply #3 on: September 17, 2023, 06:49:14 am »
Seems you are a hero programmer  :D, i live on a real world that needs fast and effective solutions.
Today got many cheap hardwares. Just like my friends said why are banking sector use jvm for its core? We had no any problem with hardware, even it needs 10 times pascal programming cost. But i apreciate your idealisms. And those need real work and many efforts.

Thank you 
« Last Edit: September 17, 2023, 08:01:32 am by Mongkey »

wp

  • Hero Member
  • *****
  • Posts: 12530
Re: Looking for QRCODE matrix generator
« Reply #4 on: September 17, 2023, 10:50:25 am »
0 units in uses section, maybe just SysUtils allowed
Joking?

OK, let me take up the challenge...

The first thing which comes to my mind is LazBarcodes, available via Online-Package-Manager in Lazarus. It can generate QRCodes in pure Pascal code: https://wiki.lazarus.freepascal.org/LazBarcodes. Although based on the zint library it does not require any external libraries. It is built around Lazarus LCL though, and thus far away from "0 units in uses section".

But there is also a more puristic qrcode generator in the fpc sources: packages/fcl-image; a sample project is in packages/fcl-image/examples. Here is another sample project:

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. uses
  4.   fpqrcodegen, fpimgqrcode, fpWritePng;
  5.  
  6. const
  7.   TEXT_STRING = 'This is a qrcode generated by fpc.';
  8.   FILE_NAME = 'qrcode.png';
  9. var
  10.   qrcode: TImageQRCodeGenerator;
  11. begin
  12.   qrcode := TImageQRCodeGenerator.Create;
  13.   try
  14.     qrcode.PixelSize := 4;
  15.     qrcode.Border := 8;
  16.     qrcode.ErrorCorrectionLevel := EccHigh;
  17.     qrcode.Generate(TEXT_STRING);
  18.     qrcode.SaveToFile(FILE_NAME);
  19.   finally
  20.     qrcode.Free;
  21.   end;
  22. end.

Or, even more puristic, just 0 and 1, the same code, but skipping the generation of the image. Like this:
Code: Pascal  [Select][+][-]
  1. program project2;
  2.  
  3. uses
  4.   fpqrcodegen;
  5.  
  6. const
  7.   TEXT_STRING = 'This is a qrcode generated by fpc.';
  8. var
  9.   qrcode: TQRCodeGenerator;
  10.   x,y: Integer;
  11. begin
  12.   qrcode := TQRCodeGenerator.Create;
  13.   try
  14.     qrcode.ErrorCorrectionLevel := EccHigh;
  15.     qrcode.Generate(TEXT_STRING);
  16.     for y := 0 to qrcode.Size-1 do
  17.     begin
  18.       for x := 0 to qrcode.Size-1 do
  19.         if qrcode.Bits[x,y] then
  20.           Write('x')
  21.         else
  22.          Write(' ');
  23.       WriteLn;
  24.     end;
  25.   finally
  26.     qrcode.Free;
  27.   end;
  28.  
  29.   ReadLn;
  30. end.

The used unit fpqrcodegen depends only on SysUtils.

cant use classes, nothing in uses section except for SysUtils.
Looking at fpqrcodegen in more detail I see that the class TQCodeGenerator is just a container to have everything at its own place. You could remove the class from the unit and use the other public functions to do the work. This leads to
Code: Pascal  [Select][+][-]
  1. program project3;
  2.  
  3. uses
  4.   simpleqrcodegen;
  5.  
  6. const
  7.   TEXT_STRING = 'This is a qrcode generated by fpc.';
  8. var
  9.   x,y: Integer;
  10.   qrbuffer: TQRBuffer = nil;
  11.   tmpbuffer: TQRBuffer = nil;
  12.   size: Integer;
  13. begin
  14.   SetLength(qrBuffer, QRBUFFER_LEN_MAX);
  15.   SetLength(tmpBuffer, QRBUFFER_LEN_MAX);
  16.  
  17.   if QREncodeText(TEXT_STRING, tmpBuffer, qrBuffer, EccHIGH, QRVERSIONMIN, QRVERSIONMAX, mp0, false) then
  18.   begin
  19.     size := QRGetSize(qrBuffer);
  20.     WriteLn('success');
  21.     WriteLn('Size: ', size);
  22.     for y := 0 to size-1 do
  23.     begin
  24.       for x := 0 to size-1 do
  25.         if QRGetModule(qrBuffer, x, y) then
  26.           Write('x')
  27.         else
  28.           Write(' ');
  29.       WriteLn;
  30.     end;
  31.   end;
  32.  
  33.   ReadLn;
  34. end.
Unit "simpleqrcodegen" is the stripped-down version of fpqrcodegen which you can find in the attachment.
« Last Edit: September 17, 2023, 12:00:16 pm by wp »

Mongkey

  • Sr. Member
  • ****
  • Posts: 435
Re: Looking for QRCODE matrix generator
« Reply #5 on: September 17, 2023, 11:14:28 am »
Quote
LazBarcodes, available via Online-Package-Manager in Lazarus, can generate QRCodes in pure Pascal code: https://wiki.lazarus.freepascal.org/LazBarcodes. Although based on the zint library it does not require any external libraries. It is built around Lazarus LCL though, and thus far away from "0 units in uses section".

if it were FCL -> easy integration with LAMW, for anyone who alergic to other languange in android world.

wp

  • Hero Member
  • *****
  • Posts: 12530
Re: Looking for QRCODE matrix generator
« Reply #6 on: September 17, 2023, 12:02:38 pm »
I guess you stopped reading after the first few lines...

Fibonacci

  • Hero Member
  • *****
  • Posts: 647
  • Internal Error Hunter
Re: Looking for QRCODE matrix generator
« Reply #7 on: September 17, 2023, 07:16:19 pm »
0 units in uses section, maybe just SysUtils allowed
Joking?

Dead serious.

Looks like I underestimated FPC codebase. The code is already done. FPQRCodeGen uses just SysUtils, and if you remove it then just need to add type for TBytes and remove EQRCode class (and its usage).

Same with your simpleqrcodegen - remove SysUtils and define TBytes as array of byte.

This way the goal is achieved. 0 units used, not even SysUtils. Final executable binary is exactly 51200 bytes in size.

(I have my own exception handling unit with some basic functions, thats why I dont use SysUtils)

Thank you @wp for your examples and research. BTW for the generated output to be recognizable by QR scanners (text in console) write 1 as double # and 0 as double space, otherwise its too high in relation to width.

Code: Pascal  [Select][+][-]
  1. uses
  2.   simpleqrcodegen;
  3.  
  4. const
  5.   TEXT_STRING = 'This is a qrcode generated by fpc.';
  6. var
  7.   x,y: Integer;
  8.   qrbuffer: TQRBuffer = nil;
  9.   tmpbuffer: TQRBuffer = nil;
  10.   size: Integer;
  11. begin
  12.   SetLength(qrBuffer, QRBUFFER_LEN_MAX);
  13.   SetLength(tmpBuffer, QRBUFFER_LEN_MAX);
  14.  
  15.   if QREncodeText(TEXT_STRING, tmpBuffer, qrBuffer, EccHIGH, QRVERSIONMIN, QRVERSIONMAX, mp0, false) then
  16.   begin
  17.     size := QRGetSize(qrBuffer);
  18.     WriteLn('success');
  19.     WriteLn('Size: ', size);
  20.  
  21.     for y := 0 to size-1 do
  22.     begin
  23.       for x := 0 to size-1 do
  24.         if QRGetModule(qrBuffer, x, y) then
  25.           Write('##')
  26.         else
  27.           Write('  ');
  28.       WriteLn;
  29.     end;
  30.   end;
  31.  
  32.   ReadLn;
  33. end.

Again thanks, I would start rewriting this C code without you :D
« Last Edit: September 17, 2023, 07:46:14 pm by Fibonacci »

wp

  • Hero Member
  • *****
  • Posts: 12530
Re: Looking for QRCODE matrix generator
« Reply #8 on: September 18, 2023, 01:15:18 am »
BTW for the generated output to be recognizable by QR scanners (text in console) write 1 as double # and 0 as double space, otherwise its too high in relation to width.
Nice idea with duplication of the ON/OFF characters. Trying to read the QRCode from the console with my smartphone, however, fails. Switching Windows to the ancient IBM codepage (437) and using the block character #219 for the ON state fixes this:
Code: Pascal  [Select][+][-]
  1. program qrcode_bits_only_no_class;
  2. uses
  3.   windows,
  4.   simpleqrcodegen;    
  5. const
  6.   BIT_ON = #219#219;   // Block character on CP437
  7.   BIT_OFF = '  ';
  8. ...
  9. begin
  10.   SetConsoleCP(437);
  11.   ...
  12.     for y := 0 to size-1 do
  13.     begin
  14.       for x := 0 to size-1 do
  15.         if QRGetModule(qrBuffer, x, y) then
  16.           Write(BIT_ON)
  17.         else
  18.           Write(BIT_OFF);
  19.       WriteLn;
  20.     end;
  21.   ...
 
I know - not your use-case, did this just for the fun of it.
« Last Edit: September 18, 2023, 01:16:59 am by wp »

Mongkey

  • Sr. Member
  • ****
  • Posts: 435
Re: [Solved] Looking for QRCODE matrix generator
« Reply #9 on: September 18, 2023, 01:32:20 am »
Quote
I guess you stopped reading after the first few lines...

 :D, need a cup of coffee. I saw under fcl-report also got qrcode generator for report -> going to be huge improvement on LAMW android pdf reporting.
« Last Edit: September 18, 2023, 02:05:01 am by Mongkey »

Fibonacci

  • Hero Member
  • *****
  • Posts: 647
  • Internal Error Hunter
Re: Looking for QRCODE matrix generator
« Reply #10 on: September 18, 2023, 06:18:25 am »
Switching Windows to the ancient IBM codepage (437) and using the block character #219 for the ON state fixes this

No need to, check this out:

Code: Pascal  [Select][+][-]
  1.         if QRGetModule(qrBuffer, x, y) then
  2.           Write(#9608#9608)
  3.         else
  4.           Write('  ');

Looks the same as in your screenshot

EDIT: #219#219 works too, well no need to SetConsoleCP()

@Mongkey: I wonder why you keep talking about LAMW and Android :D I never had intention to use this code on Android. I guess its becasue of the screenshot (with Android) I attached, but it was just to show that QR generated by ~1k lines of C code (which compiled to just 38 KB binary) is valid. In your mind QR = something with smartphones, and smartphones = Android and LAMW :D
« Last Edit: September 18, 2023, 06:43:46 am by Fibonacci »

Mongkey

  • Sr. Member
  • ****
  • Posts: 435
Re: [Solved] Looking for QRCODE matrix generator
« Reply #11 on: September 18, 2023, 08:42:31 am »
 :D, i do a lot pascal in android world, in android world pascal has no sound, take a look at my work at android showcase using LAMW, i already did dancing in latest android platform using this LAMW FRAMEWORK,  :D, my intention just simple -> proofing this lang is not going dead, i did a lot automation, ERP (web+dekstop), i did a lot barcoding also using this language. If you dont mind, show me your work  for this language exsistence?

Thank you
https://forum.lazarus.freepascal.org/index.php/topic,54678.0.html
« Last Edit: September 18, 2023, 08:49:36 am by Mongkey »

Fibonacci

  • Hero Member
  • *****
  • Posts: 647
  • Internal Error Hunter
Re: [Solved] Looking for QRCODE matrix generator
« Reply #12 on: September 18, 2023, 08:51:26 am »
May you could show me your work  for this language exsistent?

You mean something I coded for Android? All I did was just check if and how it (LAMW) works, and it did, but for some low API version, and some errors/notifications showed up before app open.

I guess if I were to code something for Android I would use Apache Cordova.

Mongkey

  • Sr. Member
  • ****
  • Posts: 435
Re: [Solved] Looking for QRCODE matrix generator
« Reply #13 on: September 18, 2023, 09:00:22 am »
No, i know some people has their own specialty, like your console barcode  :D, it is great!
I ever saw, someone using fpc console for doing car race game directly on it, it was amazing, it was hard to do for me  :o

Android studio improvement = pascal LAMW improvement, we had a lot advantage on android with LAMW.
« Last Edit: September 18, 2023, 09:07:58 am by Mongkey »

Fibonacci

  • Hero Member
  • *****
  • Posts: 647
  • Internal Error Hunter
Re: [Solved] Looking for QRCODE matrix generator
« Reply #14 on: September 18, 2023, 09:25:28 am »
For now I would chose Cordova. JS is my "even more" main language. Node.js, NPM, Cordova - thats how I would make Android apps, more libs available, easier, faster.

Of course Im rooting for LAMW, let it grow. I dont code for Android so not my game anyway.

No, i know some people has their own specialty, like your console barcode  :D, it is great!
I ever saw, someone using fpc console for doing car race game directly on it, it was amazing, it was hard to do for me  :o

Its not about making things for console. The goal was to have raw QR code data. From this point I can go further and visualise it as I want. Either on console as above examples, or I can create image from it, or use this raw data in Owner Draw and draw rectangles with WinApi, or draw it with Canvas in LCL, sky is the limit. The thing is I cant accept if such task will add few MB to my binary, thus no external library accepted (also TGraphics, TStreams etc. not at this point).

 

TinyPortal © 2005-2018