Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve qr detection and add jpg support #21

Merged
merged 5 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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