mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 04:12:34 +00:00
cleaned up tests
This commit is contained in:
@@ -14,7 +14,11 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use std::{collections::HashMap, hash::Hash, path::{Path, PathBuf}};
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
|
hash::Hash,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
use image::{DynamicImage, EncodableLayout};
|
use image::{DynamicImage, EncodableLayout};
|
||||||
|
|
||||||
@@ -44,7 +48,12 @@ fn loadImage(fileName: &str) -> DynamicImage {
|
|||||||
file.exists(),
|
file.exists(),
|
||||||
"Please download and install test images, and run from the 'core' directory"
|
"Please download and install test images, and run from the 'core' directory"
|
||||||
);
|
);
|
||||||
DynamicImage::from(image::io::Reader::open(file).expect("image should load").decode().expect("decode"))
|
DynamicImage::from(
|
||||||
|
image::io::Reader::open(file)
|
||||||
|
.expect("image should load")
|
||||||
|
.decode()
|
||||||
|
.expect("decode"),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// In case the golden images are not monochromatic, convert the RGB values to greyscale.
|
// In case the golden images are not monochromatic, convert the RGB values to greyscale.
|
||||||
@@ -53,19 +62,18 @@ fn createMatrixFromImage(image: DynamicImage) -> BitMatrix {
|
|||||||
let height = image.height() as usize;
|
let height = image.height() as usize;
|
||||||
// let pixels = vec![0u32; width * height]; //new int[width * height];
|
// let pixels = vec![0u32; width * height]; //new int[width * height];
|
||||||
// image.getRGB(0, 0, width, height, pixels, 0, width);
|
// image.getRGB(0, 0, width, height, pixels, 0, width);
|
||||||
let img_src = DynamicImage::from(image).into_rgb8();;//image.as_rgb8().unwrap().as_bytes();
|
let img_src = DynamicImage::from(image).into_rgb8(); //image.as_rgb8().unwrap().as_bytes();
|
||||||
let pixels = img_src.as_bytes();
|
// let pixels = img_src.as_bytes();
|
||||||
|
|
||||||
let mut matrix = BitMatrix::new(width as u32, height as u32).expect("create new bitmatrix");
|
let mut matrix = BitMatrix::new(width as u32, height as u32).expect("create new bitmatrix");
|
||||||
for y in 0..height {
|
for y in 0..height as u32 {
|
||||||
// for (int y = 0; y < height; y++) {
|
// for (int y = 0; y < height; y++) {
|
||||||
for x in 0..width {
|
for x in 0..width as u32 {
|
||||||
// for (int x = 0; x < width; x++) {
|
// for (int x = 0; x < width; x++) {
|
||||||
let pixel = pixels[y * width + x] as u32;
|
//let pixel = pixels[y * width + x] as u32;
|
||||||
let luminance =
|
let [red, green, blue] = img_src.get_pixel(x, y).0;
|
||||||
(306 * ((pixel >> 16) & 0xFF) + 601 * ((pixel >> 8) & 0xFF) + 117 * (pixel & 0xFF))
|
let luminance = (306 * red as u32 + 601 * green as u32 + 117 * blue as u32) >> 10;
|
||||||
>> 10;
|
if luminance <= 0x7F {
|
||||||
if (luminance <= 0x7F) {
|
|
||||||
matrix.set(x as u32, y as u32);
|
matrix.set(x as u32, y as u32);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -78,7 +86,7 @@ fn testQRCodeWriter() {
|
|||||||
// The QR should be multiplied up to fit, with extra padding if necessary
|
// The QR should be multiplied up to fit, with extra padding if necessary
|
||||||
let bigEnough = 256;
|
let bigEnough = 256;
|
||||||
let writer = QRCodeWriter {};
|
let writer = QRCodeWriter {};
|
||||||
let mut matrix = writer.encode_with_hints(
|
let matrix = writer.encode_with_hints(
|
||||||
"http://www.google.com/",
|
"http://www.google.com/",
|
||||||
&BarcodeFormat::QR_CODE,
|
&BarcodeFormat::QR_CODE,
|
||||||
bigEnough,
|
bigEnough,
|
||||||
@@ -92,13 +100,15 @@ fn testQRCodeWriter() {
|
|||||||
|
|
||||||
// The QR will not fit in this size, so the matrix should come back bigger
|
// The QR will not fit in this size, so the matrix should come back bigger
|
||||||
let tooSmall = 20;
|
let tooSmall = 20;
|
||||||
matrix = writer.encode_with_hints(
|
matrix = writer
|
||||||
"http://www.google.com/",
|
.encode_with_hints(
|
||||||
&BarcodeFormat::QR_CODE,
|
"http://www.google.com/",
|
||||||
tooSmall,
|
&BarcodeFormat::QR_CODE,
|
||||||
tooSmall,
|
tooSmall,
|
||||||
&HashMap::new(),
|
tooSmall,
|
||||||
).expect("should encode");
|
&HashMap::new(),
|
||||||
|
)
|
||||||
|
.expect("should encode");
|
||||||
// assertNotNull(matrix);
|
// assertNotNull(matrix);
|
||||||
assert!((tooSmall as u32) < matrix.getWidth());
|
assert!((tooSmall as u32) < matrix.getWidth());
|
||||||
assert!((tooSmall as u32) < matrix.getHeight());
|
assert!((tooSmall as u32) < matrix.getHeight());
|
||||||
@@ -106,13 +116,15 @@ fn testQRCodeWriter() {
|
|||||||
// We should also be able to handle non-square requests by padding them
|
// We should also be able to handle non-square requests by padding them
|
||||||
let strangeWidth = 500;
|
let strangeWidth = 500;
|
||||||
let strangeHeight = 100;
|
let strangeHeight = 100;
|
||||||
matrix = writer.encode_with_hints(
|
matrix = writer
|
||||||
"http://www.google.com/",
|
.encode_with_hints(
|
||||||
&BarcodeFormat::QR_CODE,
|
"http://www.google.com/",
|
||||||
strangeWidth,
|
&BarcodeFormat::QR_CODE,
|
||||||
strangeHeight,
|
strangeWidth,
|
||||||
&HashMap::new(),
|
strangeHeight,
|
||||||
).expect("should encode");
|
&HashMap::new(),
|
||||||
|
)
|
||||||
|
.expect("should encode");
|
||||||
// assertNotNull(matrix);
|
// assertNotNull(matrix);
|
||||||
assert_eq!(strangeWidth as u32, matrix.getWidth());
|
assert_eq!(strangeWidth as u32, matrix.getWidth());
|
||||||
assert_eq!(strangeHeight as u32, matrix.getHeight());
|
assert_eq!(strangeHeight as u32, matrix.getHeight());
|
||||||
@@ -135,13 +147,15 @@ fn compareToGoldenFile(
|
|||||||
EncodeHintValue::ErrorCorrection(ecLevel.get_value().to_string()),
|
EncodeHintValue::ErrorCorrection(ecLevel.get_value().to_string()),
|
||||||
);
|
);
|
||||||
let writer = QRCodeWriter {};
|
let writer = QRCodeWriter {};
|
||||||
let generatedRXingResult = writer.encode_with_hints(
|
let generatedRXingResult = writer
|
||||||
contents,
|
.encode_with_hints(
|
||||||
&BarcodeFormat::QR_CODE,
|
contents,
|
||||||
resolution as i32,
|
&BarcodeFormat::QR_CODE,
|
||||||
resolution as i32,
|
resolution as i32,
|
||||||
&hints,
|
resolution as i32,
|
||||||
).expect("should encode");
|
&hints,
|
||||||
|
)
|
||||||
|
.expect("should encode");
|
||||||
|
|
||||||
assert_eq!(resolution, generatedRXingResult.getWidth());
|
assert_eq!(resolution, generatedRXingResult.getWidth());
|
||||||
assert_eq!(resolution, generatedRXingResult.getHeight());
|
assert_eq!(resolution, generatedRXingResult.getHeight());
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::{qrcode::{
|
use crate::{
|
||||||
decoder::decoder, decoder::ErrorCorrectionLevel, detector::Detector, encoder::encoder,
|
common::{BitMatrix, DetectorRXingResult},
|
||||||
}, common::BitMatrix};
|
qrcode::{
|
||||||
|
decoder::decoder, decoder::ErrorCorrectionLevel, detector::Detector, encoder::encoder,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_simple() {
|
fn test_simple() {
|
||||||
@@ -25,20 +28,13 @@ fn test_encode_decode(value: &str) {
|
|||||||
ErrorCorrectionLevel::forBits(ec_level_v).expect("must get level");
|
ErrorCorrectionLevel::forBits(ec_level_v).expect("must get level");
|
||||||
let qr_code =
|
let qr_code =
|
||||||
encoder::encode_with_hints(value, ec_level, &HashMap::new()).expect("must encode");
|
encoder::encode_with_hints(value, ec_level, &HashMap::new()).expect("must encode");
|
||||||
// dbg!(&qr_code.to_string());
|
// dbg!(&qr_code.to_string());
|
||||||
let byt_matrix = qr_code.getMatrix().as_ref().unwrap().clone();
|
let byt_matrix = qr_code.getMatrix().as_ref().unwrap().clone();
|
||||||
// dbg!(BitMatrix::from(byt_matrix.clone()).to_string());
|
// dbg!(BitMatrix::from(byt_matrix.clone()).to_string());
|
||||||
// let mut detector = Detector::new(make_larger(&byt_matrix.into(),5));
|
// let mut detector = Detector::new(make_larger(&byt_matrix.into(),5));
|
||||||
let mut detector = Detector::new(byt_matrix.into());
|
let mut detector = Detector::new(byt_matrix.into());
|
||||||
let _detected_points = detector.detect().expect("must detect");
|
let detected_points = detector.detect().expect("must detect");
|
||||||
let decoded = decoder::decode_bitmatrix(
|
let decoded = decoder::decode_bitmatrix(detected_points.getBits()).expect("must decode");
|
||||||
&qr_code
|
|
||||||
.getMatrix()
|
|
||||||
.clone()
|
|
||||||
.expect("matrix must exist")
|
|
||||||
.into(),
|
|
||||||
)
|
|
||||||
.expect("must decode");
|
|
||||||
assert_eq!(decoded.getText(), value);
|
assert_eq!(decoded.getText(), value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -155,7 +155,7 @@ impl QRCodeReader {
|
|||||||
let moduleSize = Self::moduleSize(&leftTopBlack, image)?;
|
let moduleSize = Self::moduleSize(&leftTopBlack, image)?;
|
||||||
|
|
||||||
let mut top = leftTopBlack[1] as i32;
|
let mut top = leftTopBlack[1] as i32;
|
||||||
let mut bottom = rightBottomBlack[1] as i32;
|
let bottom = rightBottomBlack[1] as i32;
|
||||||
let mut left = leftTopBlack[0] as i32;
|
let mut left = leftTopBlack[0] as i32;
|
||||||
let mut right = rightBottomBlack[0] as i32;
|
let mut right = rightBottomBlack[0] as i32;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user