use std::{collections::HashMap, path::PathBuf};
use image::DynamicImage;
use crate::{
common::BitMatrix, qrcode::QRCodeWriter, BarcodeFormat, EncodeHintType, EncodeHintValue, Writer,
};
use super::decoder::ErrorCorrectionLevel;
const BASE_IMAGE_PATH: &str = "test_resources/golden/qrcode/";
fn loadImage(fileName: &str) -> DynamicImage {
let mut file = PathBuf::from(BASE_IMAGE_PATH);
file.push(fileName);
if !file.exists() {
file = PathBuf::from("core/");
file.push(BASE_IMAGE_PATH);
file.push(fileName); }
assert!(
file.exists(),
"Please download and install test images, and run from the 'core' directory"
);
image::io::Reader::open(file)
.expect("image should load")
.decode()
.expect("decode")
}
fn createMatrixFromImage(image: DynamicImage) -> BitMatrix {
let width = image.width() as usize;
let height = image.height() as usize;
let img_src = image.into_rgb8();
let mut matrix = BitMatrix::new(width as u32, height as u32).expect("create new bitmatrix");
for y in 0..height as u32 {
for x in 0..width as u32 {
let [red, green, blue] = img_src.get_pixel(x, y).0;
let luminance = (306 * red as u32 + 601 * green as u32 + 117 * blue as u32) >> 10;
if luminance <= 0x7F {
matrix.set(x, y);
}
}
}
matrix
}
#[test]
fn testQRCodeWriter() {
let bigEnough = 256;
let writer = QRCodeWriter {};
let matrix = writer.encode_with_hints(
"http://www.google.com/",
&BarcodeFormat::QR_CODE,
bigEnough,
bigEnough,
&HashMap::new(),
);
assert!(matrix.is_ok());
let mut matrix = matrix.unwrap();
assert_eq!(bigEnough as u32, matrix.getWidth());
assert_eq!(bigEnough as u32, matrix.getHeight());
let tooSmall = 20;
matrix = writer
.encode_with_hints(
"http://www.google.com/",
&BarcodeFormat::QR_CODE,
tooSmall,
tooSmall,
&HashMap::new(),
)
.expect("should encode");
assert!((tooSmall as u32) < matrix.getWidth());
assert!((tooSmall as u32) < matrix.getHeight());
let strangeWidth = 500;
let strangeHeight = 100;
matrix = writer
.encode_with_hints(
"http://www.google.com/",
&BarcodeFormat::QR_CODE,
strangeWidth,
strangeHeight,
&HashMap::new(),
)
.expect("should encode");
assert_eq!(strangeWidth as u32, matrix.getWidth());
assert_eq!(strangeHeight as u32, matrix.getHeight());
}
fn compareToGoldenFile(
contents: &str,
ecLevel: &ErrorCorrectionLevel,
resolution: u32,
fileName: &str,
) {
let image = loadImage(fileName);
let goldenRXingResult = createMatrixFromImage(image);
let mut hints = HashMap::new();
hints.insert(
EncodeHintType::ERROR_CORRECTION,
EncodeHintValue::ErrorCorrection(ecLevel.get_value().to_string()),
);
let writer = QRCodeWriter {};
let generatedRXingResult = writer
.encode_with_hints(
contents,
&BarcodeFormat::QR_CODE,
resolution as i32,
resolution as i32,
&hints,
)
.expect("should encode");
assert_eq!(resolution, generatedRXingResult.getWidth());
assert_eq!(resolution, generatedRXingResult.getHeight());
assert_eq!(goldenRXingResult, generatedRXingResult);
}
#[test]
fn testRegressionTest() {
compareToGoldenFile(
"http://www.google.com/",
&ErrorCorrectionLevel::M,
99,
"renderer-test-01.png",
);
}