Giter VIP home page Giter VIP logo

qrcode-generator's Introduction

QR Code Generator

CI

This crate provides functions to generate QR Code matrices and images in RAW, PNG and SVG formats.

Examples

Encode any data to a QR Code matrix which is Vec<Vec<bool>>.

use qrcode_generator::QrCodeEcc;

let result: Vec<Vec<bool>> = qrcode_generator::to_matrix("Hello world!", QrCodeEcc::Low).unwrap();

println!("{:?}", result);

Encode any data to a PNG image stored in a Vec instance.

use qrcode_generator::QrCodeEcc;

let result: Vec<u8> = qrcode_generator::to_png_to_vec("Hello world!", QrCodeEcc::Low, 1024).unwrap();

println!("{:?}", result);

Encode any data to a PNG image stored in a file.

use qrcode_generator::QrCodeEcc;

qrcode_generator::to_png_to_file("Hello world!", QrCodeEcc::Low, 1024, "tests/data/file_output.png").unwrap();

Encode any data to a SVG image stored in a String instance.

use qrcode_generator::QrCodeEcc;

let result: String = qrcode_generator::to_svg_to_string("Hello world!", QrCodeEcc::Low, 1024, None::<&str>).unwrap();

println!("{:?}", result);

Encode any data to a SVG image stored in a file.

use qrcode_generator::QrCodeEcc;

qrcode_generator::to_svg_to_file("Hello world!", QrCodeEcc::Low, 1024, None::<&str>, "tests/data/file_output.svg").unwrap();

Low-level Usage

Raw Image Data

The to_image and to_image_buffer functions can be used, if you want to modify your image.

Segments

Every to_* function has a corresponding _from_segments function. You can concatenate segments by using different encoding methods, such as numeric, alphanumeric or binary to reduce the size (level) of your QR code matrix/image.

use qrcode_generator::{QrCodeEcc, QrSegment};

let first = "1234567";

let second = "ABCDEFG";

let segments = [QrSegment::make_numeric(&first), QrSegment::make_alphanumeric(&second)];

let result: Vec<Vec<bool>> = qrcode_generator::to_matrix_from_segments(&segments, QrCodeEcc::Low).unwrap();

println!("{:?}", result);

More segments optimization apporaches: magiclen/qrcode-segments-optimizer

Crates.io

https://crates.io/crates/qrcode-generator

Documentation

https://docs.rs/qrcode-generator

License

MIT

qrcode-generator's People

Contributors

awquadros avatar magiclen avatar ozten avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

qrcode-generator's Issues

Bloated dependency list

Great crate! Thanks for the work.

I'd like to use the image dependency, however it pulls in the full image crate, including encoding and decoding of all image types. Especially JPEG pulls in rayon, which pulls in crossbeam - blowing the dependency list very high.

It would be cool to have the image feature list represented, so I could only select the image types I care about (in our case png). For the moment, I will use the crate without image support and instead copy & paste the code that converts from matrix to ImageBuffer into my own code.

SVG produced are not properly rendered in some apps

Here is the code I used to generate the svg :

use qrcode_generator::QrCodeEcc;

fn main() {
    qrcode_generator::to_svg_to_file("Hello world!", QrCodeEcc::Low, 1024, None::<&str>, "svg_output.svg").unwrap();
}

The issue in image (Firefox)

100 % 90 %
image image

The issue in text form

Some applications, such as Firefox, do not render the svg properly when it is not at its original size (if the svg is zoomed in/out). There are white spaces between the black squares.

Apps that I tested:

App Render properly?
Firefox No
Chromium browsers Yes
Inkscape No
GIMP Yes

Possible solution

I am not a svg specialist so don't take my words quite literally.

  1. Enlarging the squares should fix the problem but I don't know if this is the way to go
  2. Render svg like qrcode (it doesn't have this problem), it uses path instead of multiple rect (this also makes the svg smaller to store)

Does not compile for `no_std` projects

After setting default-features = false in cargo.toml this crate does not compile on no_std environments as it has some dependencies (on qrcodegen) depending on std.

error[E0463]: can't find crate for `std`
  |
  = note: the `thumbv7em-none-eabihf` target may not support the standard library
  = note: `std` is required by `qrcodegen` because it does not declare `#![no_std]`

For more information about this error, try `rustc --explain E0463`.
error: could not compile `qrcodegen` due to previous error
warning: build failed, waiting for other jobs to finish...
error[E0463]: can't find crate for `std`
  |
  = note: the `thumbv7em-none-eabihf` target may not support the standard library
  = note: `std` is required by `html_escape` because it does not declare `#![no_std]`

error: build failed

Potential integer overflow causes runtime panic

In function to_image_from_str, it is possible to construct malformed input and trigger an integer overflow when calculating the length in function to_image_inner:

// Line 445 in lib.rs
let length = size * size;

This would result in a smaller buffer being allocated. Accessing this buffer would cause runtime panics.

// Line 447 in lib.rs
let mut img_raw: Vec<u8> = vec![255u8; length];

Example code that triggers the problem:

use qrcode_generator::to_image_from_str;
use qrcode_generator::QrCodeEcc;

fn main() {
    to_image_from_str("hello", QrCodeEcc::Low, std::usize::MAX);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.