[BE] Use imagemagick instead of printpdf
This commit is contained in:
parent
57f69a6096
commit
f86131d3a5
@ -2,4 +2,11 @@
|
|||||||
|
|
||||||
Internal system for EEGSAC's internal processes.
|
Internal system for EEGSAC's internal processes.
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
This system requires the following:
|
||||||
|
|
||||||
|
- Imagequick, available through the command `convert`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,13 +1,10 @@
|
|||||||
use crate::{json_result::JsonResult, model::register::Register};
|
use crate::{json_result::JsonResult, model::register::Register};
|
||||||
use printpdf::{Mm, PdfDocument};
|
|
||||||
use rocket::{http::Status, serde::json::Json};
|
use rocket::{http::Status, serde::json::Json};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::process::Command;
|
||||||
use std::{
|
use std::{
|
||||||
f32::consts::E,
|
fs,
|
||||||
fs::{self, File},
|
|
||||||
io::BufWriter,
|
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
result,
|
|
||||||
time::{SystemTime, UNIX_EPOCH},
|
time::{SystemTime, UNIX_EPOCH},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -164,6 +161,8 @@ fn get_image_info(path: PathBuf) -> ScanInfo {
|
|||||||
// crop image
|
// crop image
|
||||||
let cropped_img = img.crop_imm(0, third_point_height, mid_point_width, remaining_height);
|
let cropped_img = img.crop_imm(0, third_point_height, mid_point_width, remaining_height);
|
||||||
|
|
||||||
|
// TODO: threshold image before getting qr
|
||||||
|
|
||||||
// get qr from cropped image
|
// get qr from cropped image
|
||||||
let results = bardecoder::default_decoder().decode(&cropped_img);
|
let results = bardecoder::default_decoder().decode(&cropped_img);
|
||||||
|
|
||||||
@ -335,47 +334,41 @@ async fn convert_scans_from_data(data: &Vec<(PathBuf, ScanInfo)>) -> Result<(),
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn convert_to_pdf(image_path: &PathBuf, output_path: &PathBuf) -> Result<(), String> {
|
fn convert_to_pdf(image_path: &PathBuf, output_path: &PathBuf) -> Result<(), String> {
|
||||||
let img = match image::open(image_path) {
|
// Just use imagemagick to convert the image to pdf
|
||||||
Ok(i) => i,
|
// -interlace Plane -gaussian-blur 0.05 -quality 75% -rotate 270 doc.jpg doc.jpg.pdf
|
||||||
|
let call = Command::new("convert")
|
||||||
|
.arg("-strip")
|
||||||
|
.arg("-interlace")
|
||||||
|
.arg("Plane")
|
||||||
|
.arg("-gaussian-blur")
|
||||||
|
.arg("0.05")
|
||||||
|
.arg("-quality")
|
||||||
|
.arg("75%")
|
||||||
|
.arg("-rotate")
|
||||||
|
.arg("270")
|
||||||
|
.arg(image_path)
|
||||||
|
.arg(output_path)
|
||||||
|
.spawn();
|
||||||
|
|
||||||
|
let mut call = match call {
|
||||||
|
Ok(r) => r,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
log::error!("Error opening image: {:?}", err);
|
log::error!("Error starting imagemagick: {:?}", err);
|
||||||
return Err(format!("Error abriendo imagen"));
|
return Err(format!("Error convirtiendo imagen"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Rotate image 45 degrees clockwise
|
match call.wait() {
|
||||||
// let img = img.rotate270();
|
Ok(exit_status) if exit_status.success() => Ok(()),
|
||||||
|
Ok(exit_status) => {
|
||||||
// Generate PDF
|
log::error!("Error calling imagemagick: {:?}", exit_status);
|
||||||
let (doc, page1, layer1) =
|
Err(format!(
|
||||||
// PdfDocument::new("Certificado - EEGSAC", Mm(297.0), Mm(210.0), "Layer 1");
|
"Error ejecutando imagemagick: codigo de salida no exitoso"
|
||||||
PdfDocument::new("Certificado - EEGSAC", Mm(210.0), Mm(297.0), "Layer 1");
|
))
|
||||||
|
}
|
||||||
let current_layer = doc.get_page(page1).get_layer(layer1);
|
Err(err) => {
|
||||||
|
log::error!("Error calling imagemagick: {:?}", err);
|
||||||
let pdf_image = printpdf::image::Image::from_dynamic_image(&img);
|
Err(format!("Error ejecutando imagemagick"))
|
||||||
let img_transform = printpdf::ImageTransform::default();
|
|
||||||
|
|
||||||
/*
|
|
||||||
This section of code assumes that the image is scaned at 200 DPI.
|
|
||||||
IF the image is scanned at a different DPI, this will give
|
|
||||||
wrong results.
|
|
||||||
*/
|
|
||||||
let img_transform = printpdf::ImageTransform {
|
|
||||||
dpi: Some(200.0),
|
|
||||||
..img_transform
|
|
||||||
};
|
|
||||||
|
|
||||||
pdf_image.add_to_layer(current_layer, img_transform);
|
|
||||||
|
|
||||||
let result = doc.save(&mut BufWriter::new(File::create(output_path).unwrap()));
|
|
||||||
|
|
||||||
// TODO: Delete image
|
|
||||||
match result {
|
|
||||||
Ok(_) => Ok(()),
|
|
||||||
Err(reason) => {
|
|
||||||
log::error!("Error saving pdf: {:?}", reason);
|
|
||||||
Err(format!("Error guardando pdf"))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -225,4 +225,3 @@ impl Register {
|
|||||||
Ok(data)
|
Ok(data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user