aruZeta / QRgen

A QR code generation library.
https://aruzeta.github.io/QRgen/
MIT License
103 stars 8 forks source link

Colab #50

Closed qo4on closed 1 year ago

qo4on commented 1 year ago

Can I run it in Colab?

aruZeta commented 1 year ago

I'm sorry I have never used Colab nor I know what it's used for, so I don't know.

qo4on commented 1 year ago

Unfortunately, I never used Nim. Probably Colab should work, at least it shows the version of the installed Nim:

!pip install git+https://github.com/demotomohiro/nim4colab.git
%load_ext nim4colab
%%nimc
echo "Nim version is ", NimVersion

Installing Nim stable
done
Hint: used config file '/root/nim-stable/nim/config/nim.cfg' [Conf]
Hint: used config file '/root/nim-stable/nim/config/config.nims' [Conf]
.........................................................
CC: nim-stable/nim/lib/std/private/digitsutils.nim
CC: nim-stable/nim/lib/system/dollars.nim
CC: nim-stable/nim/lib/system/io.nim
CC: nim-stable/nim/lib/system.nim
CC: code.nim
Hint:  [Link]
Hint: gc: refc; opt: none (DEBUG BUILD, `-d:release` generates faster code)
26647 lines; 1.341s; 31.695MiB peakmem; proj: /root/code.nim; out: /root/code [SuccessX]
Hint: /root/code  [Exec]
Nim version is 1.6.11

But I don't know how to run nimble install, it returns the error undeclared identifier: 'nimble':

%%nimc
nimble install qrgen

Hint: used config file '/root/nim-stable/nim/config/nim.cfg' [Conf]
Hint: used config file '/root/nim-stable/nim/config/config.nims' [Conf]
.........................................................
/root/code.nim(1, 1) Error: undeclared identifier: 'nimble'
candidates (edit distance, scope distance); see '--spellSuggest': 
 (3, 2): 'File' [type declared in /root/nim-stable/nim/lib/system/io.nim(21, 3)]
 (3, 2): 'nimvm' [let declared in /root/nim-stable/nim/lib/system.nim(529, 5)]
qo4on commented 1 year ago

I managed to run it

%nimble -y install qrgen
%%nimc
import QRgen
let myQR = newQR("https://github.com/aruZeta/QRgen")
myQR.printTerminal

image

qo4on commented 1 year ago

Just wondering, have you tested the reliability of qr codes with pictures? What parameters should be used to create a qr code so that it works 100% of the time with all phones?

myQR.renderImg("#1d2021","#98971a",100,100,25,img=readImage("QRgen-logo.png"))
aruZeta commented 1 year ago

The problem is not the phone but the QR scaner used, depending on the scanner it may or may not read complicated QR codes, like ones with rounded corners, spacing or even images.

Also the image should not be a problem since it calculates it's max size depending on the selected ECC level, so the amount of broken bits can be recovered.

qo4on commented 1 year ago

For some reason printSvg doesn't work. Is there any way to save the svg to a file?

import QRgen
let myQR = newQR("https://github.com/aruZeta/QRgen")
myQR.printSvg

/root/code.nim(3, 5) Error: expression 'printSvg(myQR, "#ffffff", "#000000", 0, 0, 0, "qrCode", "", "",
         genDefaultCoords(myQR), false)' is of type 'string' and has to be used (or discarded); start of expression here: /root/code.nim(2, 1)
aruZeta commented 1 year ago

Yes, printSvg returns you a string, so you just need to pipe it into a file, take a look to the tests file tests/testQRGen.nim, here is an example:

  let qr = newQR("https://github.com/aruZeta/QRgen")
  writeFile(
    "build" / "testingSvg.svg",
    qr.printSvg
  )
qo4on commented 1 year ago

How do you get this qr code? I would like to remove the dots around the logo as in your example. image

!wget -O logo.svg https://upload.wikimedia.org/wikipedia/commons/b/b8/YouTube_play_button_icon_%282013%E2%80%932017%29.svg

%%nimc
import QRgen
let qr = newQR("https://github.com/aruZeta/QRgen", ecLevel=qrECH)
writeFile("testingSvg.svg", qr.printSvg("#1d2021", "#98971a", 100, 100, 25, svgImg=readFile("logo.svg")))

testingSvg

aruZeta commented 1 year ago

Yes, that's intended behaviour, to avoid it you would just add a non-transparent background to your image, but oh well there should be a way to force a square to be drawn behind it.

qo4on commented 1 year ago

Do you mean that all the svg need to be specially edited by placing them on an non-transparent background rectangle of the same color as the background of the qr code? Also, the size of this rectangle must be manually adjusted for each qr code so that its borders do not partially overlap the dots of the qr code itself and do not divide them into two parts, right?

aruZeta commented 1 year ago

The current implementation does not offer a way to force a background for transparent images. About the size, you don't need to mind it since it's actually calculated to fit perfectly (and you can even force an x,y position and a width,height, although not recommended since it could mess up the QR code and make it unreadable unless you test it or know what you're doing).

qo4on commented 1 year ago

Is there any reason why you can't get a qr code like this without editing the svg and placing it on an opaque background? image

aruZeta commented 1 year ago

As I said it's not that there is a reason of why you can't force a layer between the QR and the image, so as in a way to act as the background of the image, but that it's just not implemented.

It shouldn't be a difficult task, but I will try it whenever I have the time and energy to do it.

qo4on commented 1 year ago

I see, thank you. Anyway, you did a very good job. And it would be absolutely perfect if you manage to add a way to force a background for transparent images.

aruZeta commented 1 year ago

Yeah it's something that's missing from the library, but I'm very sorry I don't have any time atm.