October 27, 2021, 07:00:10

Author Topic: QR codes  (Read 334 times)

Offline chrisws

  • Full Member
  • ***
  • Posts: 117
    • SmallBASIC
QR codes
« on: May 13, 2021, 08:24:03 »
I'm porting the c-based QR Code library from Project Nayuki to SmallBASIC

https://www.nayuki.io/page/qr-code-generator-library
https://github.com/smallbasic/smallbasic.plugins/blob/master/units/qrcode.bas

It's more or less working, I think there are still some issues.

The code is packaged as a unit to make it easy to include in another project.

One funny thing I found was the C xor operator character "^" is a power operator in SmallBASIC.

I couldn't claim to know exactly how this all works, apart from areas I really had to dig into.

There's a pretty good overview on the Project Nayuki  page.

« Last Edit: May 13, 2021, 08:25:58 by chrisws »

Offline chrisws

  • Full Member
  • ***
  • Posts: 117
    • SmallBASIC
Re: QR codes
« Reply #1 on: May 19, 2021, 10:50:33 »
Here's a short example using the library:

Code: [Select]
import qrcode

sub main(text)
  local bufLen = qrcode.qrcodegen_BUFFER_LEN_FOR_VERSION(qrcode.qrcodegen_VERSION_MAX)
  dim qrcode(bufLen)
  local errCorLvl = qrcode.qrcodegen_Ecc_LOW
  if (!qrcode.qrcodegen_encodeText(text, qrcode, errCorLvl, qrcode.qrcodegen_VERSION_MIN, qrcode.qrcodegen_VERSION_MAX, qrcode.qrcodegen_Mask_AUTO, true)) then
    print "Failed to create QR code"
  else
    local size = qrcode.qrcodegen_getSize(qrcode)
    local border = 0
    local x, y
    local xs = 5
    local ys = 5
    local xo = (xmax - (size * xs)) / 2
    local yo = (ymax - (size * xs)) / 2
   
    for y = 0 to size - 1
      for x = 0 to  size -1
        if (qrcode.qrcodegen_getModule(qrcode, x, y)) then
          rect xo + (xs * x), yo + (ys * y), xo + (xs * (x + 1)), yo + (ys * (y + 1)), 0 filled
        endif
      next x
    next y
  endif
end

main("hello")





 

SimplePortal 2.3.6 © 2008-2014, SimplePortal