-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing QR code reading (see WanzenBug/rqrr#2).
- Loading branch information
Showing
7 changed files
with
28 additions
and
101 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,40 @@ | ||
use qrcode::QrCode; | ||
use qrcode::types::QrError; | ||
use image::{Rgb, ImageBuffer}; | ||
use qrcode::types::{QrError, Color}; | ||
use image::{Rgb, RgbImage}; | ||
|
||
use crate::symbol; | ||
|
||
enum QrEncodeError { | ||
pub enum QrEncodeError { | ||
EncodingLibError(QrError), | ||
} | ||
|
||
type QrEncodeResult = Result<ImageBuffer<Rgb<u8>, Vec<u8>>, QrEncodeError>; | ||
pub type QrEncodeResult = Result<RgbImage, QrEncodeError>; | ||
|
||
pub fn symbol_to_qrcode(symb: symbol::Symbol) -> QrEncodeResult { | ||
string_to_qrcode(symb.to_str()) | ||
} | ||
|
||
fn qrcode_to_image(code: QrCode) -> RgbImage { | ||
let width = code.width() as u32; | ||
let mut img = RgbImage::new(width+8, width+8); // leaving 4 pixels for the quiet zone | ||
let colors = code.to_colors(); | ||
let mut colors_iter = colors.iter(); | ||
for x in 4..=(width+4) { | ||
for y in 4..=(width+4) { | ||
let pixel = colors_iter.next().unwrap_or(&Color::Light); | ||
img.put_pixel(x, y, pixel.select(Rgb([0,0,0]), Rgb([255,255,255]))); | ||
} | ||
} | ||
img | ||
} | ||
|
||
pub fn string_to_qrcode(data: String) -> QrEncodeResult { | ||
let code = QrCode::new(data); | ||
let to_render; | ||
match code { | ||
Ok(res) => {to_render = res;}, | ||
Err(error) => {return Err(QrEncodeError::EncodingLibError(error));} | ||
} | ||
let img = to_render.render::<Rgb<u8>>().build(); | ||
// .light_color(Rgb([255, 255, 255])).build(); | ||
let img = qrcode_to_image(to_render); | ||
Ok(img) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.