mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 04:12:34 +00:00
cargo fmt
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
|
||||
//package com.google.zxing;
|
||||
|
||||
use std::{rc::Rc, borrow::Cow};
|
||||
use std::{borrow::Cow, rc::Rc};
|
||||
|
||||
use crate::{
|
||||
common::{BitArray, BitMatrix},
|
||||
@@ -62,7 +62,7 @@ pub trait Binarizer {
|
||||
* @return The 2D array of bits for the image (true means black).
|
||||
* @throws NotFoundException if image can't be binarized to make a matrix
|
||||
*/
|
||||
fn getBlackMatrix(& self) -> Result<&BitMatrix, Exceptions>;
|
||||
fn getBlackMatrix(&self) -> Result<&BitMatrix, Exceptions>;
|
||||
|
||||
/**
|
||||
* Creates a new object with the same type as this Binarizer implementation, but with pristine
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
//package com.google.zxing;
|
||||
|
||||
use std::{fmt, rc::Rc, borrow::Cow};
|
||||
use std::{borrow::Cow, fmt, rc::Rc};
|
||||
|
||||
use crate::{
|
||||
common::{BitArray, BitMatrix},
|
||||
@@ -68,8 +68,8 @@ impl BinaryBitmap {
|
||||
* @return The array of bits for this row (true means black).
|
||||
* @throws NotFoundException if row can't be binarized
|
||||
*/
|
||||
pub fn getBlackRow(& self, y: usize) -> Result<Cow<BitArray>, Exceptions> {
|
||||
self.binarizer.getBlackRow(y)
|
||||
pub fn getBlackRow(&self, y: usize) -> Result<Cow<BitArray>, Exceptions> {
|
||||
self.binarizer.getBlackRow(y)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,12 +88,7 @@ impl BinaryBitmap {
|
||||
// 1D Reader finds a barcode before the 2D Readers run.
|
||||
// 2. This work will only be done once even if the caller installs multiple 2D Readers.
|
||||
if self.matrix.is_none() {
|
||||
self.matrix = Some(
|
||||
self.binarizer
|
||||
.getBlackMatrix()
|
||||
.unwrap()
|
||||
.clone(),
|
||||
);
|
||||
self.matrix = Some(self.binarizer.getBlackMatrix().unwrap().clone());
|
||||
}
|
||||
self.matrix.as_mut().unwrap()
|
||||
}
|
||||
@@ -114,12 +109,7 @@ impl BinaryBitmap {
|
||||
// 1D Reader finds a barcode before the 2D Readers run.
|
||||
// 2. This work will only be done once even if the caller installs multiple 2D Readers.
|
||||
if self.matrix.is_none() {
|
||||
self.matrix = Some(
|
||||
self.binarizer
|
||||
.getBlackMatrix()
|
||||
.unwrap()
|
||||
.clone(),
|
||||
)
|
||||
self.matrix = Some(self.binarizer.getBlackMatrix().unwrap().clone())
|
||||
}
|
||||
self.matrix.as_ref().unwrap()
|
||||
}
|
||||
@@ -160,10 +150,7 @@ impl BinaryBitmap {
|
||||
* @return Whether this bitmap supports counter-clockwise rotation.
|
||||
*/
|
||||
pub fn isRotateSupported(&self) -> bool {
|
||||
return self
|
||||
.binarizer
|
||||
.getLuminanceSource()
|
||||
.isRotateSupported();
|
||||
return self.binarizer.getLuminanceSource().isRotateSupported();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -173,10 +160,7 @@ impl BinaryBitmap {
|
||||
* @return A rotated version of this object.
|
||||
*/
|
||||
pub fn rotateCounterClockwise(&mut self) -> BinaryBitmap {
|
||||
let newSource = self
|
||||
.binarizer
|
||||
.getLuminanceSource()
|
||||
.rotateCounterClockwise();
|
||||
let newSource = self.binarizer.getLuminanceSource().rotateCounterClockwise();
|
||||
return BinaryBitmap::new(
|
||||
self.binarizer
|
||||
.createBinarizer(newSource.expect("new lum source expected")),
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
// import com.google.zxing.LuminanceSource;
|
||||
// import com.google.zxing.NotFoundException;
|
||||
|
||||
use std::{rc::Rc, borrow::Cow};
|
||||
use std::{borrow::Cow, rc::Rc};
|
||||
|
||||
use once_cell::unsync::OnceCell;
|
||||
|
||||
@@ -54,22 +54,22 @@ impl Binarizer for GlobalHistogramBinarizer {
|
||||
}
|
||||
|
||||
// Applies simple sharpening to the row data to improve performance of the 1D Readers.
|
||||
fn getBlackRow(& self, y: usize) -> Result<Cow<BitArray>, Exceptions> {
|
||||
let row = self.black_row_cache[y].get_or_try_init(||{
|
||||
fn getBlackRow(&self, y: usize) -> Result<Cow<BitArray>, Exceptions> {
|
||||
let row = self.black_row_cache[y].get_or_try_init(|| {
|
||||
let source = self.getLuminanceSource();
|
||||
let width = source.getWidth();
|
||||
let mut row = BitArray::with_size(width);
|
||||
|
||||
|
||||
// self.initArrays(width);
|
||||
let localLuminances = source.getRow(y);
|
||||
let mut localBuckets = [0; GlobalHistogramBinarizer::LUMINANCE_BUCKETS]; //self.buckets.clone();
|
||||
for x in 0..width {
|
||||
// for (int x = 0; x < width; x++) {
|
||||
localBuckets
|
||||
[((localLuminances[x]) >> GlobalHistogramBinarizer::LUMINANCE_SHIFT) as usize] += 1;
|
||||
localBuckets[((localLuminances[x]) >> GlobalHistogramBinarizer::LUMINANCE_SHIFT)
|
||||
as usize] += 1;
|
||||
}
|
||||
let blackPoint = Self::estimateBlackPoint(&localBuckets)?;
|
||||
|
||||
|
||||
if width < 3 {
|
||||
// Special case for very small images
|
||||
for (x, lum) in localLuminances.iter().enumerate().take(width) {
|
||||
@@ -98,24 +98,22 @@ impl Binarizer for GlobalHistogramBinarizer {
|
||||
})?;
|
||||
|
||||
Ok(Cow::Borrowed(row))
|
||||
|
||||
}
|
||||
|
||||
// Does not sharpen the data, as this call is intended to only be used by 2D Readers.
|
||||
fn getBlackMatrix(& self) -> Result<&BitMatrix, Exceptions> {
|
||||
fn getBlackMatrix(&self) -> Result<&BitMatrix, Exceptions> {
|
||||
// if self.black_matrix.is_none() {
|
||||
// self.black_matrix =
|
||||
// Some(Self::build_black_matrix(&self.source).expect("matrix must generate"))
|
||||
// }
|
||||
// Ok(self.black_matrix.as_ref().unwrap())
|
||||
let matrix = self.black_matrix.get_or_try_init(||Self::build_black_matrix(&self.source))?;
|
||||
let matrix = self
|
||||
.black_matrix
|
||||
.get_or_try_init(|| Self::build_black_matrix(&self.source))?;
|
||||
Ok(matrix)
|
||||
}
|
||||
|
||||
fn createBinarizer(
|
||||
&self,
|
||||
source: Box<dyn crate::LuminanceSource>,
|
||||
) -> Rc<dyn Binarizer> {
|
||||
fn createBinarizer(&self, source: Box<dyn crate::LuminanceSource>) -> Rc<dyn Binarizer> {
|
||||
Rc::new(GlobalHistogramBinarizer::new(source))
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
// import com.google.zxing.LuminanceSource;
|
||||
// import com.google.zxing.NotFoundException;
|
||||
|
||||
use std::{rc::Rc, borrow::Cow};
|
||||
use std::{borrow::Cow, rc::Rc};
|
||||
|
||||
use once_cell::unsync::OnceCell;
|
||||
|
||||
@@ -57,7 +57,7 @@ impl Binarizer for HybridBinarizer {
|
||||
self.ghb.getLuminanceSource()
|
||||
}
|
||||
|
||||
fn getBlackRow(& self, y: usize) -> Result<Cow<BitArray>, Exceptions> {
|
||||
fn getBlackRow(&self, y: usize) -> Result<Cow<BitArray>, Exceptions> {
|
||||
self.ghb.getBlackRow(y)
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ impl Binarizer for HybridBinarizer {
|
||||
* constructor instead, but there are some advantages to doing it lazily, such as making
|
||||
* profiling easier, and not doing heavy lifting when callers don't expect it.
|
||||
*/
|
||||
fn getBlackMatrix(& self) -> Result<&BitMatrix, Exceptions> {
|
||||
fn getBlackMatrix(&self) -> Result<&BitMatrix, Exceptions> {
|
||||
// if self.black_matrix.is_none() {
|
||||
// self.black_matrix = Some(
|
||||
// Self::calculateBlackMatrix(&mut self.ghb)
|
||||
@@ -74,7 +74,9 @@ impl Binarizer for HybridBinarizer {
|
||||
// )
|
||||
// }
|
||||
// Ok(self.black_matrix.as_ref().unwrap())
|
||||
let matrix = self.black_matrix.get_or_try_init(||Self::calculateBlackMatrix(& self.ghb))?;
|
||||
let matrix = self
|
||||
.black_matrix
|
||||
.get_or_try_init(|| Self::calculateBlackMatrix(&self.ghb))?;
|
||||
Ok(matrix)
|
||||
}
|
||||
|
||||
@@ -107,7 +109,7 @@ impl HybridBinarizer {
|
||||
}
|
||||
}
|
||||
|
||||
fn calculateBlackMatrix(ghb: & GlobalHistogramBinarizer) -> Result<BitMatrix, Exceptions> {
|
||||
fn calculateBlackMatrix(ghb: &GlobalHistogramBinarizer) -> Result<BitMatrix, Exceptions> {
|
||||
// let matrix;
|
||||
let source = ghb.getLuminanceSource();
|
||||
let width = source.getWidth();
|
||||
|
||||
@@ -38,9 +38,7 @@ fn testMulti() {
|
||||
.decode()
|
||||
.expect("must decode");
|
||||
let source = BufferedImageLuminanceSource::new(image);
|
||||
let mut bitmap = BinaryBitmap::new(Rc::new(HybridBinarizer::new(Box::new(
|
||||
source,
|
||||
))));
|
||||
let mut bitmap = BinaryBitmap::new(Rc::new(HybridBinarizer::new(Box::new(source))));
|
||||
|
||||
let mut reader = GenericMultipleBarcodeReader::new(MultiFormatReader::default());
|
||||
let results = reader
|
||||
@@ -67,9 +65,7 @@ fn testMultiQR() {
|
||||
.decode()
|
||||
.expect("must decode");
|
||||
let source = BufferedImageLuminanceSource::new(image);
|
||||
let mut bitmap = BinaryBitmap::new(Rc::new(HybridBinarizer::new(Box::new(
|
||||
source,
|
||||
))));
|
||||
let mut bitmap = BinaryBitmap::new(Rc::new(HybridBinarizer::new(Box::new(source))));
|
||||
|
||||
let mut reader = GenericMultipleBarcodeReader::new(MultiFormatReader::default());
|
||||
let results = reader
|
||||
|
||||
@@ -250,9 +250,7 @@ mod multi_qr_code_test_case {
|
||||
.decode()
|
||||
.expect("must decode");
|
||||
let source = BufferedImageLuminanceSource::new(image);
|
||||
let mut bitmap = BinaryBitmap::new(Rc::new(HybridBinarizer::new(Box::new(
|
||||
source,
|
||||
))));
|
||||
let mut bitmap = BinaryBitmap::new(Rc::new(HybridBinarizer::new(Box::new(source))));
|
||||
|
||||
let mut reader = QRCodeMultiReader::new();
|
||||
let results = reader.decode_multiple(&mut bitmap).expect("must decode");
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
* http://www.piramidepse.com/
|
||||
*/
|
||||
|
||||
use std::{rc::Rc};
|
||||
use std::rc::Rc;
|
||||
|
||||
/**
|
||||
* @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es)
|
||||
@@ -174,9 +174,9 @@ fn assertCorrectImage2binary(fileName: &str, expected: &str) {
|
||||
let path = format!("test_resources/blackbox/rssexpanded-1/{}", fileName);
|
||||
|
||||
let image = image::open(path).expect("file exists");
|
||||
let binaryMap = BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(
|
||||
Box::new(BufferedImageLuminanceSource::new(image)),
|
||||
)));
|
||||
let binaryMap = BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(Box::new(
|
||||
BufferedImageLuminanceSource::new(image),
|
||||
))));
|
||||
let rowNumber = binaryMap.getHeight() / 2;
|
||||
let row = binaryMap.getBlackRow(rowNumber).expect("row");
|
||||
|
||||
|
||||
@@ -71,9 +71,9 @@ fn assertCorrectImage2result(fileName: &str, expected: ExpandedProductParsedRXin
|
||||
let path = format!("test_resources/blackbox/rssexpanded-1/{}", fileName);
|
||||
|
||||
let image = image::open(path).expect("image must exist");
|
||||
let binaryMap = BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(
|
||||
Box::new(BufferedImageLuminanceSource::new(image)),
|
||||
)));
|
||||
let binaryMap = BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(Box::new(
|
||||
BufferedImageLuminanceSource::new(image),
|
||||
))));
|
||||
let rowNumber = binaryMap.getHeight() / 2;
|
||||
let row = binaryMap.getBlackRow(rowNumber).expect("get row");
|
||||
|
||||
|
||||
@@ -184,9 +184,9 @@ fn assertCorrectImage2string(fileName: &str, expected: &str) {
|
||||
let path = format!("test_resources/blackbox/rssexpanded-1/{}", fileName);
|
||||
|
||||
let image = image::open(path).expect("load image");
|
||||
let binaryMap = BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(
|
||||
Box::new(BufferedImageLuminanceSource::new(image)),
|
||||
)));
|
||||
let binaryMap = BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(Box::new(
|
||||
BufferedImageLuminanceSource::new(image),
|
||||
))));
|
||||
let rowNumber = binaryMap.getHeight() / 2;
|
||||
let row = binaryMap.getBlackRow(rowNumber).expect("get row");
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
* http://www.piramidepse.com/
|
||||
*/
|
||||
|
||||
use std::{rc::Rc};
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::{
|
||||
common::GlobalHistogramBinarizer,
|
||||
@@ -42,9 +42,9 @@ use super::RSSExpandedReader;
|
||||
#[test]
|
||||
fn testFindFinderPatterns() {
|
||||
let image = readImage("2.png");
|
||||
let binaryMap = BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(
|
||||
Box::new(BufferedImageLuminanceSource::new(image)),
|
||||
)));
|
||||
let binaryMap = BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(Box::new(
|
||||
BufferedImageLuminanceSource::new(image),
|
||||
))));
|
||||
let rowNumber = binaryMap.getHeight() as u32 / 2;
|
||||
let row = binaryMap.getBlackRow(rowNumber as usize).expect("ok");
|
||||
let mut previousPairs = Vec::new(); //new ArrayList<>();
|
||||
@@ -88,9 +88,9 @@ fn testFindFinderPatterns() {
|
||||
#[test]
|
||||
fn testRetrieveNextPairPatterns() {
|
||||
let image = readImage("3.png");
|
||||
let binaryMap = BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(
|
||||
Box::new(BufferedImageLuminanceSource::new(image)),
|
||||
)));
|
||||
let binaryMap = BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(Box::new(
|
||||
BufferedImageLuminanceSource::new(image),
|
||||
))));
|
||||
let rowNumber = binaryMap.getHeight() as u32 / 2;
|
||||
let row = binaryMap.getBlackRow(rowNumber as usize).expect("create");
|
||||
let mut previousPairs = Vec::new(); //new ArrayList<>();
|
||||
@@ -116,9 +116,9 @@ fn testRetrieveNextPairPatterns() {
|
||||
#[test]
|
||||
fn testDecodeCheckCharacter() {
|
||||
let image = readImage("3.png");
|
||||
let binaryMap = BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(
|
||||
Box::new(BufferedImageLuminanceSource::new(image.clone())),
|
||||
)));
|
||||
let binaryMap = BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(Box::new(
|
||||
BufferedImageLuminanceSource::new(image.clone()),
|
||||
))));
|
||||
let row = binaryMap
|
||||
.getBlackRow(binaryMap.getHeight() / 2)
|
||||
.expect("create");
|
||||
@@ -144,9 +144,9 @@ fn testDecodeCheckCharacter() {
|
||||
#[test]
|
||||
fn testDecodeDataCharacter() {
|
||||
let image = readImage("3.png");
|
||||
let binaryMap = BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(
|
||||
Box::new(BufferedImageLuminanceSource::new(image.clone())),
|
||||
)));
|
||||
let binaryMap = BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(Box::new(
|
||||
BufferedImageLuminanceSource::new(image.clone()),
|
||||
))));
|
||||
let row = binaryMap
|
||||
.getBlackRow(binaryMap.getHeight() / 2)
|
||||
.expect("create");
|
||||
|
||||
@@ -72,7 +72,10 @@ fn testDecodingRowByRow() {
|
||||
.getStartEndMut()[1] = 0;
|
||||
|
||||
let secondRowNumber = 2 * binaryMap.getHeight() / 3;
|
||||
let mut secondRow = binaryMap.getBlackRow(secondRowNumber).expect("get row").into_owned();
|
||||
let mut secondRow = binaryMap
|
||||
.getBlackRow(secondRowNumber)
|
||||
.expect("get row")
|
||||
.into_owned();
|
||||
secondRow.reverse();
|
||||
|
||||
let totalPairs = rssExpandedReader
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
* http://www.piramidepse.com/
|
||||
*/
|
||||
|
||||
use std::{rc::Rc};
|
||||
use std::rc::Rc;
|
||||
|
||||
use image::DynamicImage;
|
||||
|
||||
@@ -39,7 +39,7 @@ fn getBufferedImage(fileName: &str) -> DynamicImage {
|
||||
pub(crate) fn getBinaryBitmap(fileName: &str) -> BinaryBitmap {
|
||||
let bufferedImage = getBufferedImage(fileName);
|
||||
|
||||
BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(
|
||||
Box::new(BufferedImageLuminanceSource::new(bufferedImage)),
|
||||
)))
|
||||
BinaryBitmap::new(Rc::new(GlobalHistogramBinarizer::new(Box::new(
|
||||
BufferedImageLuminanceSource::new(bufferedImage),
|
||||
))))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user