Create barcode - QR-code wit AHK

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Create barcode - QR-code wit AHK

Post by Albireo » 01 Dec 2021, 17:28

My wish is to create an AHK program (GUI?) That can generate 1D / 2D codes.
Depending on how easy it will be to handle, I will try to use the solution in different situations e.g. to be able to scan directly on the screen. (but preferably I would like to print the code - but I feel it difficult)
Found some old examples .:
- BARCODER - Create 1D and 2D Barcodes (QRCode , C39,etc) (2014)
- Barcode Gen - Create 1D and 2D Barcodes (QR Code , C39,etc) (2011)
- Command line (2021)
I do not know, but I can not find the right files that fit the AHK-programs or ...

About 1D code, Code 128 would probably work. (today I usually use EAN128 - which is similar)
About 2D code,
It is harder to decide which type of 2D would work for me.
  • PDF417
  • QR-Code
  • Data Matrix
or ...

One wish is that the 2D code can contain Swedish characters (eg ÅÄÖ åäö) - if possible.


Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Create barcode - QR-code wit AHK

Post by Albireo » 02 Dec 2021, 15:08

I don't find right libraries...

Is BARCODER.ahk the same as BARCODE_GEN.ahk (not found)
Is Gdip.ahk (not found) the same as Gdip_all.ahk

or is there another library I need?

Found a good summary of 1D / 2D codes on .: Barcode Types and Guide about QR Code (in many different languages)

Found a page with the ability to test the limitation of different 1D/2D codes online barcode generator (in many different languages)
This page shows that the 2D codes below can handle Swedish characters.
  • PDF417
  • Datamatrix

Another good barcode generator, Generate Free Barcodes Online
That page also provides an HTML code to generate codes But do not know if it is only 1D codes or if it can be used with AHK

Code: Select all

<div style='text-align: center;'>
  <!-- insert your custom barcode setting your data in the GET parameter "data" -->
  <img alt='Barcode Generator TEC-IT'
       src='https://barcode.tec-it.com/barcode.ashx?data=12345&code=&multiplebarcodes=true&translate-esc=false&unit=Fit&dpi=96&imagetype=Gif&rotation=0&color=%23000000&bgcolor=%23ffffff&codepage=Default&qunit=Mm&quiet=0&hidehrt=False'/>
</div>
<div style='padding-top:8px; text-align:center; font-size:15px; font-family: Source Sans Pro, Arial, sans-serif;'>
  <!-- back-linking to www.tec-it.com is required -->
  <a href='https://www.tec-it.com' title='Barcode Software by TEC-IT' target='_blank'>
    TEC-IT Barcode Generator<br/>
    <!-- logos are optional -->
    <img alt='TEC-IT Barcode Software' border='0'
         src='http://www.tec-it.com/pics/banner/web/TEC-IT_Logo_75x75.gif'>
  </a>
</div>

User avatar
JoeWinograd
Posts: 2179
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Create barcode - QR-code wit AHK

Post by JoeWinograd » 02 Dec 2021, 17:33

Albireo wrote:or is there another library I need?
Gio's BARCODER works perfectly here in W10 with AHK v1.1.33.10. The link you posted for it is correct:
viewtopic.php?f=6&t=5538#p32028

The Dropbox link there for v1.02 is still alive:
https://www.dropbox.com/s/gwryp5d3iarkuiv/BARCODER.ahk?dl=1

The latest Gdip standard library (v1.45, dated 07/09/11) by tic (Tariq Porter) is discussed here:
viewtopic.php?t=6517

The Dropbox link there for v1.45 is still alive:
https://www.dropbox.com/s/0e9gdfetbfa8v0o/Gdip_All.ahk

Here's a QR Code that I just generated with it:

Gio BARCODER works!.png
Gio BARCODER works!.png (8.1 KiB) Viewed 2577 times

What is not working for you? Regards, Joe

Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Create barcode - QR-code wit AHK

Post by Albireo » 03 Dec 2021, 06:22

Thank You!

- Downloaded both files from DropBox.
- Found a test script, which I modified.

Code: Select all

#SingleInstance, Force
SetBatchLines, -1

#Include %A_ScriptDir%/BARCODER.ahk
#Include %A_ScriptDir%/GDIP_All.ahk

START:
inputbox, Test,, Type in a message and a corresponding QR-CODE image will be generated and saved in the scripts directory.

MATRIX_TO_PRINT := GENERATE_QR_CODE(test)

if (MATRIX_TO_PRINT = 1)
{
   Goto START
}

; Start gdi+
If !pToken := Gdip_Startup()
{
   MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
   ExitApp
}



pBitmap := Gdip_CreateBitmap(MATRIX_TO_PRINT.1.MaxIndex(), MATRIX_TO_PRINT.MaxIndex())
G := Gdip_GraphicsFromImage(pBitmap)
Gdip_SetSmoothingMode(pBitmap, 3)
pBrush := Gdip_BrushCreateSolid(0xFFFFFFFF)
Gdip_FillRectangle(G, pBrush, 0, 0, MATRIX_TO_PRINT.1.MaxIndex(), MATRIX_TO_PRINT.MaxIndex())
Gdip_DeleteBrush(pBrush)

   
Loop % MATRIX_TO_PRINT.MaxIndex()
{
   CURRENT_ROW := A_Index-1
   CURRENT_ROW_DATA := MATRIX_TO_PRINT[A_Index]
   Loop % CURRENT_ROW_DATA.MaxIndex()
      If (CURRENT_ROW_DATA[A_Index] = 1)   
         Gdip_SetPixel(pBitmap, A_Index-1, CURRENT_ROW, 0xFF000000)
}
   
CURRENT_ROW := "", CURRENT_COLUMN := ""
   
StringReplace, FILE_NAME_TO_USE, test, `" ; We can't use all the characters that byte mode can encode in the name of the file. So we are replacing them here (if they exist).
FILE_PATH_AND_NAME := A_ScriptDir . "\" . SubStr(RegExReplace(FILE_NAME_TO_USE, "/w+"), 1, 20) . ".png" ; Same as above.
Gdip_SaveBitmapToFile(pBitmap, FILE_PATH_AND_NAME)
Gdip_DisposeImage(pBitmap)
Gdip_DeleteGraphics(G)
Gdip_Shutdown(pToken)

msgbox, 0, Success, QR-CODE image succesfully created!
Goto START

Return
I received the following message
(The question of what text to convert to QR code never came up)
Error: Call to nonexistent function.

Specifically: GENERATE_QR_CODE(test)

Line#
2707: VarSetCapacity(String, char_count * 2)
2708: char_count := DllCall("MultiByteToWideChar", "uint", Encoding, "uint", 0, "uint", Address, "int", Length, "uint", &String, "int", char_count * 2)
2709: String := StrGetB(&String, char_count, 1200)
2710: }
2712: Return,String
2713: }
008: InputBox,Test,,Type in a message and a corresponding QR-CODE image will be generated and saved in the scripts directory.
---> 010: MATRIX_TO_PRINT := GENERATE_QR_CODE(test)
012: if (MATRIX_TO_PRINT = 1)
013: {
014: Goto,START
015: }
018: if !pToken := Gdip_Startup()
019: {
020: MsgBox,48,gdiplus error!,Gdiplus failed to start. Please ensure you have gdiplus on your system

The program will exit.
(Your QR code works to scan.)

User avatar
JoeWinograd
Posts: 2179
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Create barcode - QR-code wit AHK

Post by JoeWinograd » 03 Dec 2021, 09:58

Albireo wrote:Error: Call to nonexistent function.
Specifically: GENERATE_QR_CODE(test)
The correct name of the function is BARCODER_GENERATE_QR_CODE. Regards, Joe

Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Create barcode - QR-code wit AHK

Post by Albireo » 03 Dec 2021, 19:10

Thank you!

Changed the instruction to .:

Code: Select all

MATRIX_TO_PRINT := BARCODER_GENERATE_QR_CODE(Test)
Must also move the libraries (at the end of the script)

Code: Select all

#Include %A_ScriptDir%/BARCODER.ahk
#Include %A_ScriptDir%/GDIP_All.ahk
Now it works - I get a png file with a barcode that can be read with a scanner.
Even the Swedish characters ÅÄÖ can be generated. :dance:

But the barcode becomes very small. The image must be enlarged (to at least 150 - 200%) before it can be read with my scanner on the laptop.
When the QR code is enlarged, the sharpness of the image deteriorates (the file, with the QR code, will be about 300kB)

Is it possible to decide how big the image with the QR code should be??

____________________________________________--

BARCODER.ahk Can probably generate several different codes - like .:
  • BARCODER_GENERATE_QR_CODE()
  • BARCODER_GENERATE_CODE_39()
  • BARCODER_GENERATE_CODE_ITF()
  • BARCODER_GENERATE_CODE_128B()


I tried to use BARCODER_GENERATE_CODE_128B() Nothing happens! (no image is created)
Some suggestions, why it does not work?

User avatar
JoeWinograd
Posts: 2179
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Create barcode - QR-code wit AHK

Post by JoeWinograd » 03 Dec 2021, 20:36

Albireo wrote:Now it works - I get a png file with a barcode that can be read with a scanner. Even the Swedish characters ÅÄÖ can be generated.
Great news!
Is it possible to decide how big the image with the QR code should be??
Try the attached modification of the example script. In the first prompt, enter an integer between 1 and 50, which affects the size of the image (higher number results in bigger image). Experiment with values between 1 and 50 until you find a size that you like.
Nothing happens! (no image is created)
Same here.
Some suggestions, why it does not work?
I don't know. According to Gio's 23-Jun-2016 comment, AlphaBravo wrote the code. I suggest contacting him/her via a PM. Regards, Joe

Edit#1: In the two #Include statements, replaced the location of each library on my computer with a generic placeholder (made no other changes to the attached script).

Edit#2: Deleted the attached script, as the new function-based script (with a sample calling script) in a later post is better.
Last edited by JoeWinograd on 04 Dec 2021, 20:00, edited 2 times in total.

Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: Create barcode - QR-code wit AHK

Post by Albireo » 04 Dec 2021, 03:21

Yes! :bravo: it works!
Thank you!

As info .: The QR code starts to get damaged around "size 48" (but still works to read)

The next step is to create a function() that contains
  • 1) The text to be converted
  • 2) Name of the result file
  • 3) Catalog of results
  • 4) QR size
The function should return OK or an error message.
The last three variables can have a default value and do not always need to be specified
The call could be something similar .:

Code: Select all

QRresult: = QRcode("Test name"; "QR-test"; "c: \ temp \"; "10")
or

Code: Select all

QRresult: = QRcode("Test default")
When it comes to 1D codes, I intend to first try to use fonts

User avatar
JoeWinograd
Posts: 2179
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Create barcode - QR-code wit AHK

Post by JoeWinograd » 04 Dec 2021, 06:01

Albireo wrote:Thank you!
You're welcome. I just edited the script in my last post to replace the location of the two libraries on my computer with generic placeholders (made no other changes).

Good idea to write that function. Please post it when you're done. Regards, Joe

User avatar
JoeWinograd
Posts: 2179
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Create barcode - QR-code wit AHK

Post by JoeWinograd » 04 Dec 2021, 14:53

Albireo wrote:The next step is to create a function
I created a function that suits my purposes. It is not everything that you asked for, but it may be helpful for you. Attached are the function (GenerateQRcode.ahk) and a sample call to it (TestQRcodeFunction.ahk). Regards, Joe
Attachments
TestQRcodeFunction.ahk
(385 Bytes) Downloaded 128 times
GenerateQRcode.ahk
(2.09 KiB) Downloaded 157 times

Post Reply

Return to “Ask for Help (v1)”