erwanvivien / fast_qr

Ultra fast QRCode generation
https://fast-qr.com/
MIT License
197 stars 29 forks source link

Image branding #16

Closed sergiomeneses closed 1 year ago

sergiomeneses commented 1 year ago

Hey, just wanna know if there is wayt to brand the QR code generated, like colors and logo.

erwanvivien commented 1 year ago

Hello, available examples showing how to brand QRCode using colors is availables in example folder

There are two ways to add an image to the QRCode:

I agree this is cumbersome and will be doing an update to add those features.

AntoniosBarotsis commented 1 year ago

I manually added a logo on top here using the image crate in case that's useful! @sergiomeneses

erwanvivien commented 1 year ago

It is non-trivial to solve as from an SVG perspective an image is only a path, an URL (not handled) or a base64 image. It has no information on width, or height. The simplest API I'm thinking of right now requires you to pass the image width and height. Not convenient, but OK

Would that work for now ?

erwanvivien commented 1 year ago

I managed to get those results, without need to specify width and height for now.

erwanvivien commented 1 year ago

Playground code can be found here: https://github.com/erwanvivien/fast_qr/tree/embed-image

Square:

RoundedSquare:

CIRCLE:

This is kinda bad, as we lose lots of modules in the second one

erwanvivien commented 1 year ago

Please tell me which one you prefer between 2/3 and 4/5

erwanvivien commented 1 year ago

You can play around with the version at fast_qr/embed-image with the example as baseline

sergiomeneses commented 1 year ago

Hey, i just try the new api, it works nice, i think is better if the user set the percentage of the qr to display image (even ifthe qr it turns to unreadless). Also, i would like to know if there is a way to customize the shapes (both image and modules), i saw these are just svg definitions, this may allow us to make things like this:

QRCODE.

erwanvivien commented 1 year ago

I've added in 3c35904a5 the ability to customize the size of the embedded image

erwanvivien commented 1 year ago

On the example you provided I see an API where one would provide an Shape::Custom(_) and would be able to provide:

Shape::Square => format!("M{},{}h1v1h-1", j + self.margin, i + self.margin),
Shape::Circle => format!(
    "M{},{}a.5,.5 0 1,1 0,-.1",
    j + self.margin + 1,
    (i + self.margin) as f64 + 0.5f64
),
Shape::RoundedSquare => format!(
    "M{0}.2,{1}.2 {0}.8,{1}.2 {0}.8,{1}.8 {0}.2,{1}.8z",
    j + self.margin,
    i + self.margin,
),
Shape::Horizontal => {
    format!("M{}.1,{}h1v.8h-1", j + self.margin, i + self.margin)
}
Shape::Vertical => {
    format!("M{},{}.1h.8v1h-.8", j + self.margin, i + self.margin)
}
Shape::Diamond => {
    format!(
        "M{}.5,{}l.5,.5l-.5,.5l-.5,-.5z",
        j + self.margin,
        i + self.margin
    )
}

Here can be found the existing shapes, but what would be fed ? j + margin could do it, but some (Circle) needs more different, maybe a closure should be passed?

erwanvivien commented 1 year ago

Using a closure / function would allow previous snippet to be refactored too

erwanvivien commented 1 year ago

c8196e4 added possibility to move image

erwanvivien commented 1 year ago

On the fast_qr/embed-image branch, there is now an example showing how to combine & use custom shape. For now there still only have the same colors tho

here you see a circle and a rectangle.

AntoniosBarotsis commented 1 year ago

Oh so this also works for pngs? Nice!

erwanvivien commented 1 year ago

Oh so this also works for pngs? Nice!

Indeed! I also found a bug? in resvg where if the provided image is an svg it's not sized / rendered as it should (expected being Firefox's renderer)

Edit:

Firefox:

ReSVG:

erwanvivien commented 1 year ago

I've upgraded the API allowing user to provide different colors for each "layer" and check the type of the module, thus we are now able to have those outputs:

AntoniosBarotsis commented 1 year ago

Make sure to open an issue in the SVG crate! @erwanvivien

erwanvivien commented 1 year ago

I will do!

erwanvivien commented 1 year ago

After some digging, the inner svg (the one I'm using above) contains a width attribute, which makes it scale down.

To be honest, I don't know which one is right on this, Firefox is clearly ignoring the width attribute and resvg is having a weird behavior(?)

sergiomeneses commented 1 year ago

hey @erwanvivien, i will try it this week, thanks.

erwanvivien commented 1 year ago

Please do tell me if you need more! Thanks

erwanvivien commented 1 year ago

@sergiomeneses Did you manage to try the new API ? :)

erwanvivien commented 1 year ago

I will mark the issue as resolved, if you need something more, please file a new issue :)