Android Question How print QRCode on ESC/POS printer

dws

Member
Licensed User
Longtime User
Hello

I would like some help on printing QRCode to ESC / POS (Bluetooth Printer)

1. I use QRGenerator to create QRCode in a Bitmap object
2. I use EscPosPrinter.bas for printing.
3. If I use (WriteQRCode) from EscPosPrinter the QRCode is printed but you do not scan at all.
4. If I use (PrintImage) the QRCode is printed diagonally

4.1. Here is a small example of my code for printing QRCode as an image

B4X:
' QrGen is QRGenerator class
' Prn   is EscPosPrinter class
' sQRString is my string with 114 length for creating QRCode

' 1. Create an image with QRCode
dim QrBmp as Bitmap = QrGen.Create(sQRString)

' 1.1 If assing QrBmp on view is currect '

' 2. Print QRCode Image with EscPosPrinter object
dim img as AnImage = Prn.ImageToBWIMage(QrGen)
img = Prn.ThresholdImage(img, 128)
img = Prn.PackImage(img)

' 3. Final Print QRCode Image
Prn.PrintImage(img)

the result is a diagonal print of QRCode image.

Any Help or Is there an example or way to print the QRCode either as an image or directly as a QRCode?

Thanks ...
 
Last edited:

Kevin Hartin

Active Member
Licensed User
Hello

I would like some help on printing QRCode to ESC / POS (Bluetooth Printer)

1. I use QRGenerator to create QRCode in a Bitmap object
2. I use EscPosPrinter.bas for printing.
3. If I use (WriteQRCode) from EscPosPrinter the QRCode is printed but you do not scan at all.
4. If I use (PrintImage) the QRCode is printed diagonally

4.1. Here is a small example of my code for printing QRCode as an image

B4X:
' QrGen is QRGenerator class
' Prn   is EscPosPrinter class
' sQRString is my string with 114 length for creating QRCode

' 1. Create an image with QRCode
dim QrBmp as Bitmap = QrGen.Create(sQRString)

' 1.1 If assing QrBmp on view is currect '

' 2. Print QRCode Image with EscPosPrinter object
dim img as AnImage = Prn.ImageToBWIMage(QrGen)
img = Prn.ThresholdImage(img, 128)
img = Prn.PackImage(img)

' 3. Final Print QRCode Image
Prn.PrintImage(img)

the result is a diagonal print of QRCode image.

Any Help or Is there an example or way to print the QRCode either as an image or directly as a QRCode?

Thanks ...
I have been doing the same using QRgenerator and ESCPOSprinter and found that the diagonal tearing is because the source QRcode image is not a multiple of 8 or 24. I found that the best results come from generating the QRcode at 512 px if I know it is going to an 80mm printer or 360 if it is going to a 58mm printer.

for printing, I do the following and it is reliable.

QR generate and print:
Dim qr As QRGenerator
Dim Printer1 As EscPosPrinter
Dim iwidth as Int = 512

Dim vl As String = "AlhSU1lTWldMMzczWlFYRjQBAAAAAQAAAEwLAwAAAAAAAAABcQ5zvzUAAAAqZAw1njZ%2F8bGFxY2L6p3ncqt3yS71%2BktS7R0f2wcnLFyVqQita0mP9idV5gq7D6AzKqXHnd2vkAyJ%2BdeBGkbRXt7ltGUQb%2Fs3g3DEZYAY%2BL2p9t3c5rAyeTp1BJckyV6KCggot5FHJ3iA3r6Lmtbfje%2FxnWnCmrKzRgsTdVgexzE9ZZhgAd0B7uw5zG8QyzN1STiAfs2igQwn2Bw%2BmdmOeBqq2Cy9bULw3mCeyS2Czz1ab4129Q%2BOU1HuLlb7AH9CYBIHA5GL7ZHJbpr7EEEGjQf7iLTK69b6LSSo4jBAOh9EdmvzwgnQz4oJht68ac%2FYzCpAYwQTIFDlCdMoxFbSYNxmErLoJUHbUvza6iLfC8kRgcWPVpaQ7zo%2FKM4bZniP%2FY9ypfBK%2BEcjpmDpD4xbKIipXGQQ%2FlE2tUxT7ad8BPmaCUlUBAAtHu6VfIGZXOxayCRf3o1vj6ORD9eQxMLX47%2Fs2edogY5W6ThVakmJrzMd%2BdQWDwe3x6ya4kGEl2qUoHBqUmNBBE9tlvmP8oI6TaKZtDQn4wBfcwjfStN9U2X%2BBOwNCfBCjwKsZLF49dex5wfNFF%2FfDIxLONWEmP3dtPezpkkcinK6QkU91uiu2HEgcQVFj9VYgX3H%2B8fEAwLYRS6T%2B%2FUhqn0T3Iaqomt9SAcgARSbDbPPqwg%2FdGj7aw%3D%3D"
Dim QRtext As String = "https://frontendui.test.taxcore.dti.rs/v/?vl="&vl

qr.Initialize(iwidth)
img = qr.Create(QRtext)
Dim out As OutputStream
out = File.OpenOutput(B4XPages.MainPage.dbPath, "QRcode.jpg", False)
img.WriteToStream(out, 100, "JPEG")
out.Close

Dim QRimage As AnImage = Printer1.ImageToBWIMage(bmp)
QRimage = Printer1.ThresholdImage(QRimage, 128)
QRimage = Printer1.PackImage(QRimage)
Printer1.PrintImage(QRimage)

Regards,
Kev
247078295_415560089921580_756382186623138616_n.jpg
 
Upvote 0

dws

Member
Licensed User
Longtime User
I have been doing the same using QRgenerator and ESCPOSprinter and found that the diagonal tearing is because the source QRcode image is not a multiple of 8 or 24. I found that the best results come from generating the QRcode at 512 px if I know it is going to an 80mm printer or 360 if it is going to a 58mm printer.

for printing, I do the following and it is reliable.

QR generate and print:
Dim qr As QRGenerator
Dim Printer1 As EscPosPrinter
Dim iwidth as Int = 512

Dim vl As String = "AlhSU1lTWldMMzczWlFYRjQBAAAAAQAAAEwLAwAAAAAAAAABcQ5zvzUAAAAqZAw1njZ%2F8bGFxY2L6p3ncqt3yS71%2BktS7R0f2wcnLFyVqQita0mP9idV5gq7D6AzKqXHnd2vkAyJ%2BdeBGkbRXt7ltGUQb%2Fs3g3DEZYAY%2BL2p9t3c5rAyeTp1BJckyV6KCggot5FHJ3iA3r6Lmtbfje%2FxnWnCmrKzRgsTdVgexzE9ZZhgAd0B7uw5zG8QyzN1STiAfs2igQwn2Bw%2BmdmOeBqq2Cy9bULw3mCeyS2Czz1ab4129Q%2BOU1HuLlb7AH9CYBIHA5GL7ZHJbpr7EEEGjQf7iLTK69b6LSSo4jBAOh9EdmvzwgnQz4oJht68ac%2FYzCpAYwQTIFDlCdMoxFbSYNxmErLoJUHbUvza6iLfC8kRgcWPVpaQ7zo%2FKM4bZniP%2FY9ypfBK%2BEcjpmDpD4xbKIipXGQQ%2FlE2tUxT7ad8BPmaCUlUBAAtHu6VfIGZXOxayCRf3o1vj6ORD9eQxMLX47%2Fs2edogY5W6ThVakmJrzMd%2BdQWDwe3x6ya4kGEl2qUoHBqUmNBBE9tlvmP8oI6TaKZtDQn4wBfcwjfStN9U2X%2BBOwNCfBCjwKsZLF49dex5wfNFF%2FfDIxLONWEmP3dtPezpkkcinK6QkU91uiu2HEgcQVFj9VYgX3H%2B8fEAwLYRS6T%2B%2FUhqn0T3Iaqomt9SAcgARSbDbPPqwg%2FdGj7aw%3D%3D"
Dim QRtext As String = "https://frontendui.test.taxcore.dti.rs/v/?vl="&vl

qr.Initialize(iwidth)
img = qr.Create(QRtext)
Dim out As OutputStream
out = File.OpenOutput(B4XPages.MainPage.dbPath, "QRcode.jpg", False)
img.WriteToStream(out, 100, "JPEG")
out.Close

Dim QRimage As AnImage = Printer1.ImageToBWIMage(bmp)
QRimage = Printer1.ThresholdImage(QRimage, 128)
QRimage = Printer1.PackImage(QRimage)
Printer1.PrintImage(QRimage)

Regards,
Kev
View attachment 120535
Thanks Kevin
What is bmp in this line (Dim QRimage As AnImage = Printer1.ImageToBWIMage(bmp))
 
Upvote 0

Kevin Hartin

Active Member
Licensed User
Thanks Kevin
What is bmp in this line (Dim QRimage As AnImage = Printer1.ImageToBWIMage(bmp))
bmp is the image file generated by QRgenerate

B4X:
Dim qr As QRGenerator
Dim Printer1 As EscPosPrinter
Dim iwidth as Int = 512

Dim vl As String = "AlhSU1lTWldMMzczWlFYRjQBAAAAAQAAAEwLAwAAAAAAAAABcQ5zvzUAAAAqZAw1njZ%2F8bGFxY2L6p3ncqt3yS71%2BktS7R0f2wcnLFyVqQita0mP9idV5gq7D6AzKqXHnd2vkAyJ%2BdeBGkbRXt7ltGUQb%2Fs3g3DEZYAY%2BL2p9t3c5rAyeTp1BJckyV6KCggot5FHJ3iA3r6Lmtbfje%2FxnWnCmrKzRgsTdVgexzE9ZZhgAd0B7uw5zG8QyzN1STiAfs2igQwn2Bw%2BmdmOeBqq2Cy9bULw3mCeyS2Czz1ab4129Q%2BOU1HuLlb7AH9CYBIHA5GL7ZHJbpr7EEEGjQf7iLTK69b6LSSo4jBAOh9EdmvzwgnQz4oJht68ac%2FYzCpAYwQTIFDlCdMoxFbSYNxmErLoJUHbUvza6iLfC8kRgcWPVpaQ7zo%2FKM4bZniP%2FY9ypfBK%2BEcjpmDpD4xbKIipXGQQ%2FlE2tUxT7ad8BPmaCUlUBAAtHu6VfIGZXOxayCRf3o1vj6ORD9eQxMLX47%2Fs2edogY5W6ThVakmJrzMd%2BdQWDwe3x6ya4kGEl2qUoHBqUmNBBE9tlvmP8oI6TaKZtDQn4wBfcwjfStN9U2X%2BBOwNCfBCjwKsZLF49dex5wfNFF%2FfDIxLONWEmP3dtPezpkkcinK6QkU91uiu2HEgcQVFj9VYgX3H%2B8fEAwLYRS6T%2B%2FUhqn0T3Iaqomt9SAcgARSbDbPPqwg%2FdGj7aw%3D%3D"
Dim QRtext As String = "https://frontendui.test.taxcore.dti.rs/v/?vl="&vl

qr.Initialize(iwidth)
img = qr.Create(QRtext)
Dim out As OutputStream
out = File.OpenOutput(File.DirInternal, "QRcode.jpg", False)
img.WriteToStream(out, 100, "JPEG")
out.Close

Dim bmp As Bitmap
bmp.Initialize(File.DirInternal, "QRcode.jpg")  
bmp = bmp.Resize(imgWidth, imgWidth, True)   ' not really needed as we QRgenerated at iwidth 512
Dim QRimage As AnImage = Printer1.ImageToBWIMage(bmp)
QRimage = Printer1.ThresholdImage(QRimage, 128)
QRimage = Printer1.PackImage(QRimage)
Printer1.PrintImage(QRimage)
 
Upvote 0

dws

Member
Licensed User
Longtime User
bmp is the image file generated by QRgenerate

B4X:
Dim qr As QRGenerator
Dim Printer1 As EscPosPrinter
Dim iwidth as Int = 512

Dim vl As String = "AlhSU1lTWldMMzczWlFYRjQBAAAAAQAAAEwLAwAAAAAAAAABcQ5zvzUAAAAqZAw1njZ%2F8bGFxY2L6p3ncqt3yS71%2BktS7R0f2wcnLFyVqQita0mP9idV5gq7D6AzKqXHnd2vkAyJ%2BdeBGkbRXt7ltGUQb%2Fs3g3DEZYAY%2BL2p9t3c5rAyeTp1BJckyV6KCggot5FHJ3iA3r6Lmtbfje%2FxnWnCmrKzRgsTdVgexzE9ZZhgAd0B7uw5zG8QyzN1STiAfs2igQwn2Bw%2BmdmOeBqq2Cy9bULw3mCeyS2Czz1ab4129Q%2BOU1HuLlb7AH9CYBIHA5GL7ZHJbpr7EEEGjQf7iLTK69b6LSSo4jBAOh9EdmvzwgnQz4oJht68ac%2FYzCpAYwQTIFDlCdMoxFbSYNxmErLoJUHbUvza6iLfC8kRgcWPVpaQ7zo%2FKM4bZniP%2FY9ypfBK%2BEcjpmDpD4xbKIipXGQQ%2FlE2tUxT7ad8BPmaCUlUBAAtHu6VfIGZXOxayCRf3o1vj6ORD9eQxMLX47%2Fs2edogY5W6ThVakmJrzMd%2BdQWDwe3x6ya4kGEl2qUoHBqUmNBBE9tlvmP8oI6TaKZtDQn4wBfcwjfStN9U2X%2BBOwNCfBCjwKsZLF49dex5wfNFF%2FfDIxLONWEmP3dtPezpkkcinK6QkU91uiu2HEgcQVFj9VYgX3H%2B8fEAwLYRS6T%2B%2FUhqn0T3Iaqomt9SAcgARSbDbPPqwg%2FdGj7aw%3D%3D"
Dim QRtext As String = "https://frontendui.test.taxcore.dti.rs/v/?vl="&vl

qr.Initialize(iwidth)
img = qr.Create(QRtext)
Dim out As OutputStream
out = File.OpenOutput(File.DirInternal, "QRcode.jpg", False)
img.WriteToStream(out, 100, "JPEG")
out.Close

Dim bmp As Bitmap
bmp.Initialize(File.DirInternal, "QRcode.jpg")
bmp = bmp.Resize(imgWidth, imgWidth, True)   ' not really needed as we QRgenerated at iwidth 512
Dim QRimage As AnImage = Printer1.ImageToBWIMage(bmp)
QRimage = Printer1.ThresholdImage(QRimage, 128)
QRimage = Printer1.PackImage(QRimage)
Printer1.PrintImage(QRimage)
Thanks Kevin ...

I understood it, I just wanted to confirm it ....
Now I face another problem. I want the QRCode to print a maximum of 256x256 ον Bluetooth pos printer with 58mm paper
 
Upvote 0

Kevin Hartin

Active Member
Licensed User
Thanks Kevin ...

I understood it, I just wanted to confirm it ....
Now I face another problem. I want the QRCode to print a maximum of 256x256 ον Bluetooth pos printer with 58mm paper
set the size to 256 when you generate the image. how many characters do you need to encode in the QR? our tax system here requires almost 800 characters to be encoded, so we end up with a very big QR, with fine details that can have issues at smaller sizes
 
Upvote 0

dws

Member
Licensed User
Longtime User
set the size to 256 when you generate the image. how many characters do you need to encode in the QR? our tax system here requires almost 800 characters to be encoded, so we end up with a very big QR, with fine details that can have issues at smaller sizes
144 Characters i need to QRCode

I have already declared 256 but again the printing is not done correctly
I need to make QRCode for string like this ( https://www1.gsis.gr/tameiakes/mywe...69D0FF6CBDB1E46A8B15BDE3EE0E25FABD28210723.20 )
 
Upvote 0
Top