Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for image in the center of QR Code #24

Open
collegeimprovements opened this issue Jul 8, 2022 · 4 comments
Open

Support for image in the center of QR Code #24

collegeimprovements opened this issue Jul 8, 2022 · 4 comments

Comments

@collegeimprovements
Copy link

Would be great if we can embed company logo in the middle of the QR Code.
It's just a feature request.

@nthock
Copy link
Collaborator

nthock commented Jul 17, 2022

Hello @collegeimprovements I am open for such feature to be supported in this package, though I must admit that it is not something that I will spend my time on, at least not the immediate priority. That's being said, I welcome any PR or any contributions that can make this a reality.

Thanks for the suggestion!

@collegeimprovements
Copy link
Author

Thanks a lot for your reply @nthock

@kuon
Copy link

kuon commented Dec 8, 2022

I looked into this issue, and what I ended up doing is to use H error correction level (30%) and the just draw the image above the gereated code. Just ensure that the image is not too large, about 15% of the area is good.

For example, if you encode the following string:

    matrix = EQRCode.encode("Hello! I am a QR code encoded with an image at the middle. It works well!", :h)
    
    matrix_size = Matrix.size(matrix)

    svg_data = EQRCode.svg(matrix)

and then modify the SVG by adding a rect:

    isize = (matrix_size * matrix_size * 0.15) |> :math.sqrt() # 15% here
    img = ~s(<rect x="#{(matrix_size - isize)/2}" y="#{(matrix_size - isize)/2}"
      fill="red"
      height="#{isize}" width="#{isize}" />)

You will end up with this:

foo

Note that I used SVG in this example to illustrate the process. But we use PNG because QRCode are MUCH smaller as PNG. And we annotate them in a separate process that is not elixir, but I am sure it wouldn't be hard to use a PNG library in elixir to write on it.

I don't know if modifying this library would provide a better solution. I am not familiar enough with exactly how QR code are encoded.

@coladarci
Copy link

Just discovered this thread; incredible library this is! How did you modify the SVG to add the image data, out of curiosity?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants