Skip to content

Commit

Permalink
Fixing QR code reading (see WanzenBug/rqrr#2).
Browse files Browse the repository at this point in the history
  • Loading branch information
danya02 committed Jul 25, 2020
1 parent 85d36f6 commit 1157418
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 101 deletions.
94 changes: 4 additions & 90 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rqrr = "0.2.0"
image = "0.23.7"
rqrr = "0.2.1"
image = "0.21.3"
clap = "2.33.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
base64 = "0.12.3"
log = "0.4.11"
simple_logger = "1.6.0"
qrcode = "0.11"
qrcode = { version = "0.11", default-features = true }
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {
)
).get_matches();

if let Some(matches) = matches.subcommand_matches("qrtest") {
if let Some(matches) = matches.subcommand_matches("qrread") {
let filename = matches.value_of("file").expect("file name required");
info!("Loading image {} ...", filename);
let img = open(filename).expect("image invalid").to_rgb();
Expand Down
25 changes: 19 additions & 6 deletions src/qr_writer.rs
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)
}
2 changes: 1 addition & 1 deletion src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,6 @@ impl ContentSymbol {
Ok(ContentSymbol{ sequence: seq, index: ind, data: data })
}
pub fn to_str(&self) -> String {
format!("{:02x}{:x}@{}", self.sequence, self.index, encode(self.data))
format!("{:02x}{:x}@{}", self.sequence, self.index, encode(&self.data))
}
}
Binary file modified src/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/test2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1157418

Please sign in to comment.