Skip to content

Commit

Permalink
feat: improve qr detection and add jpg support (#21)
Browse files Browse the repository at this point in the history
* feat: change image from mono to gray-scale

* feat: use rayon to process pages in parallel

* feat: increase resolution from 300dpi to 400dpi

* feat: add support for jpg/jpeg images

* feat: code formatting

---------

Co-authored-by: mike <[email protected]>
  • Loading branch information
mike-schmid and mike authored Jul 29, 2024
1 parent acbcabb commit b06b87c
Show file tree
Hide file tree
Showing 16 changed files with 232 additions and 233 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ rqrr = "0.7.1"
image = "0.25.1"
tempfile = "3.10.1"
serde = { version = "1.0.203", features = ["derive"] }
rayon = "1.10.0"

[dev-dependencies]
assert_cmd = "2.0.14"
Expand Down
24 changes: 14 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod qr_parser;

use crate::models::qr_data::QRData;
use image;
use rayon::prelude::*;
use rqrr::PreparedImage;
use tempfile::tempdir;

Expand All @@ -19,21 +20,24 @@ pub fn get_qr_bill_data(file_path: String, fail_on_error: bool) -> Vec<QRData> {
input if input.ends_with(".pdf") => {
pdf_converter::convert_to_png(&file_path, &tmp_dir.path())
}
input if input.ends_with(".png") => {
input if input.ends_with(".png") || input.ends_with(".jpg") || input.ends_with(".jpeg") => {
vec![image::open(&file_path).expect("Error loading image")]
}
_ => panic!("Unsupported file format"),
};

let mut all_qr_codes = Vec::new();
for img in images {
let mut img = PreparedImage::prepare(img.to_luma8());
img.detect_grids()
.into_iter()
.filter_map(|result| result.decode().ok())
.map(|(_, content)| qr_parser::get_qr_code_data(&content))
.for_each(|qr_data| all_qr_codes.push(qr_data));
}
let all_qr_codes: Vec<_> = images
.into_par_iter()
.map(|img| {
let mut img = PreparedImage::prepare(img.to_luma8());
img.detect_grids()
.into_par_iter()
.filter_map(|result| result.decode().ok())
.map(|(_, content)| qr_parser::get_qr_code_data(&content))
.collect::<Vec<_>>()
})
.flatten()
.collect();

// check if there were any errors
if fail_on_error && all_qr_codes.iter().any(|result| result.is_err()) {
Expand Down
4 changes: 2 additions & 2 deletions src/pdf_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ where
"-dSAFER",
"-dNOPAUSE",
"-dFILTERTEXT",
"-r300",
"-sDEVICE=pngmono",
"-r400",
"-sDEVICE=pnggray",
&format!(
"-sOutputFile={}/%03d.png",
tmp_path.as_ref().to_string_lossy()
Expand Down
Binary file added tests/data/double.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/double.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/full.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/full.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/minimal.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/minimal.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/none.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/none.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/rotated.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/rotated.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit b06b87c

Please sign in to comment.