-
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.
Merge pull request #4 from demarches-simplifiees/read_from_png_and_pdf
Read from png and pdf
- Loading branch information
Showing
8 changed files
with
172 additions
and
22 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ chrono = "*" | |
p256 = "*" | ||
x509-cert = "*" | ||
base32 = "*" | ||
image = "*" | ||
tempfile = "*" |
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use image::DynamicImage; | ||
use std::process::Command; | ||
use tempfile::Builder; | ||
|
||
pub fn file_to_img(file_name: &str) -> DynamicImage { | ||
if file_name.ends_with(".pdf") { | ||
pdf_to_img(file_name) | ||
} else { | ||
image::open(file_name).expect("Failed to open file") | ||
} | ||
} | ||
|
||
fn pdf_to_img(file_name: &str) -> DynamicImage { | ||
let temp_file = Builder::new() | ||
.suffix(".png") | ||
.tempfile() | ||
.expect("Failed to create temp file"); | ||
|
||
let path_without = temp_file.path().with_extension(""); | ||
let temp_file_without_extension = path_without.to_str().unwrap(); | ||
|
||
let status = Command::new("pdftoppm") | ||
.args([ | ||
"-png", | ||
"-singlefile", | ||
file_name, | ||
temp_file_without_extension, | ||
]) | ||
.status() | ||
.expect("failed to execute process"); | ||
|
||
assert!(status.success(), "pdftoppm command failed"); | ||
|
||
image::io::Reader::open(temp_file.path()) | ||
.expect("Failed to open temp file") | ||
.decode() | ||
.expect("Failed to decode image") | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_file_to_img() { | ||
let img_from_pdf = file_to_img("tests/fixtures/2ddoc/justificatif_de_domicile.pdf"); | ||
assert_eq!(img_from_pdf.width(), 1241); | ||
assert_eq!(img_from_pdf.height(), 1754); | ||
|
||
let img_from_png = file_to_img("tests/fixtures/2ddoc/justificatif_de_domicile.png"); | ||
assert_eq!(img_from_png.width(), 119); | ||
assert_eq!(img_from_png.height(), 122); | ||
} | ||
} |
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,2 +1,3 @@ | ||
pub mod datamatrix; | ||
pub mod file_utils; | ||
pub mod twoddoc; |
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
Binary file not shown.