mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 04:12:34 +00:00
update for shared state and improved performance
This commit is contained in:
@@ -31,14 +31,14 @@ pub struct ByQuadrantReader<T: Reader>(T);
|
||||
impl<T: Reader> Reader for ByQuadrantReader<T> {
|
||||
fn decode(
|
||||
&mut self,
|
||||
image: &crate::BinaryBitmap,
|
||||
image: &mut crate::BinaryBitmap,
|
||||
) -> Result<crate::RXingResult, crate::Exceptions> {
|
||||
self.decode_with_hints(image, &HashMap::new())
|
||||
}
|
||||
|
||||
fn decode_with_hints(
|
||||
&mut self,
|
||||
image: &crate::BinaryBitmap,
|
||||
image: &mut crate::BinaryBitmap,
|
||||
hints: &crate::DecodingHintDictionary,
|
||||
) -> Result<crate::RXingResult, crate::Exceptions> {
|
||||
let width = image.getWidth();
|
||||
@@ -49,7 +49,7 @@ impl<T: Reader> Reader for ByQuadrantReader<T> {
|
||||
// try {
|
||||
let attempt = self
|
||||
.0
|
||||
.decode_with_hints(&image.crop(0, 0, halfWidth, halfHeight), hints);
|
||||
.decode_with_hints(&mut image.crop(0, 0, halfWidth, halfHeight), hints);
|
||||
// No need to call makeAbsolute as results will be relative to original top left here
|
||||
match attempt {
|
||||
// Ok() => return attempt,
|
||||
@@ -63,7 +63,7 @@ impl<T: Reader> Reader for ByQuadrantReader<T> {
|
||||
// try {
|
||||
let result = self
|
||||
.0
|
||||
.decode_with_hints(&image.crop(halfWidth, 0, halfWidth, halfHeight), hints);
|
||||
.decode_with_hints(&mut image.crop(halfWidth, 0, halfWidth, halfHeight), hints);
|
||||
match result {
|
||||
Ok(res) => {
|
||||
let points = Self::makeAbsolute(res.getRXingResultPoints(), halfWidth as f32, 0.0);
|
||||
@@ -80,7 +80,7 @@ impl<T: Reader> Reader for ByQuadrantReader<T> {
|
||||
|
||||
let result = self
|
||||
.0
|
||||
.decode_with_hints(&image.crop(0, halfHeight, halfWidth, halfHeight), hints);
|
||||
.decode_with_hints(&mut image.crop(0, halfHeight, halfWidth, halfHeight), hints);
|
||||
match result {
|
||||
Ok(res) => {
|
||||
let points = Self::makeAbsolute(res.getRXingResultPoints(), 0.0, halfHeight as f32);
|
||||
@@ -98,7 +98,7 @@ impl<T: Reader> Reader for ByQuadrantReader<T> {
|
||||
// }
|
||||
|
||||
let result = self.0.decode_with_hints(
|
||||
&image.crop(halfWidth, halfHeight, halfWidth, halfHeight),
|
||||
&mut image.crop(halfWidth, halfHeight, halfWidth, halfHeight),
|
||||
hints,
|
||||
);
|
||||
match result {
|
||||
@@ -124,8 +124,8 @@ impl<T: Reader> Reader for ByQuadrantReader<T> {
|
||||
|
||||
let quarterWidth = halfWidth / 2;
|
||||
let quarterHeight = halfHeight / 2;
|
||||
let center = image.crop(quarterWidth, quarterHeight, halfWidth, halfHeight);
|
||||
let result = self.0.decode_with_hints(¢er, hints)?;
|
||||
let mut center = image.crop(quarterWidth, quarterHeight, halfWidth, halfHeight);
|
||||
let result = self.0.decode_with_hints(&mut center, hints)?;
|
||||
|
||||
let points = Self::makeAbsolute(
|
||||
result.getRXingResultPoints(),
|
||||
|
||||
@@ -42,14 +42,14 @@ pub struct GenericMultipleBarcodeReader<T: Reader>(T);
|
||||
impl<T: Reader> MultipleBarcodeReader for GenericMultipleBarcodeReader<T> {
|
||||
fn decode_multiple(
|
||||
&mut self,
|
||||
image: &crate::BinaryBitmap,
|
||||
image: &mut crate::BinaryBitmap,
|
||||
) -> Result<Vec<crate::RXingResult>, crate::Exceptions> {
|
||||
self.decode_multiple_with_hints(image, &HashMap::new())
|
||||
}
|
||||
|
||||
fn decode_multiple_with_hints(
|
||||
&mut self,
|
||||
image: &crate::BinaryBitmap,
|
||||
image: &mut crate::BinaryBitmap,
|
||||
hints: &crate::DecodingHintDictionary,
|
||||
) -> Result<Vec<crate::RXingResult>, crate::Exceptions> {
|
||||
let mut results = Vec::new();
|
||||
@@ -70,7 +70,7 @@ impl<T: Reader> GenericMultipleBarcodeReader<T> {
|
||||
|
||||
fn doDecodeMultiple(
|
||||
&mut self,
|
||||
image: &BinaryBitmap,
|
||||
image: &mut BinaryBitmap,
|
||||
hints: &DecodingHintDictionary,
|
||||
results: &mut Vec<RXingResult>,
|
||||
xOffset: u32,
|
||||
@@ -110,7 +110,7 @@ impl<T: Reader> GenericMultipleBarcodeReader<T> {
|
||||
let mut minY: f32 = height as f32;
|
||||
let mut maxX: f32 = 0.0;
|
||||
let mut maxY: f32 = 0.0;
|
||||
for point in resultPoints {
|
||||
for point in &resultPoints {
|
||||
// if (point == null) {
|
||||
// continue;
|
||||
// }
|
||||
@@ -133,7 +133,7 @@ impl<T: Reader> GenericMultipleBarcodeReader<T> {
|
||||
// Decode left of barcode
|
||||
if minX > Self::MIN_DIMENSION_TO_RECUR {
|
||||
self.doDecodeMultiple(
|
||||
&image.crop(0, 0, minX as usize, height),
|
||||
&mut image.crop(0, 0, minX as usize, height),
|
||||
hints,
|
||||
results,
|
||||
xOffset,
|
||||
@@ -144,7 +144,7 @@ impl<T: Reader> GenericMultipleBarcodeReader<T> {
|
||||
// Decode above barcode
|
||||
if minY > Self::MIN_DIMENSION_TO_RECUR {
|
||||
self.doDecodeMultiple(
|
||||
&image.crop(0, 0, width, minY as usize),
|
||||
&mut image.crop(0, 0, width, minY as usize),
|
||||
hints,
|
||||
results,
|
||||
xOffset,
|
||||
@@ -155,7 +155,7 @@ impl<T: Reader> GenericMultipleBarcodeReader<T> {
|
||||
// Decode right of barcode
|
||||
if maxX < (width as f32) - Self::MIN_DIMENSION_TO_RECUR {
|
||||
self.doDecodeMultiple(
|
||||
&image.crop(maxX as usize, 0, width - maxX as usize, height),
|
||||
&mut image.crop(maxX as usize, 0, width - maxX as usize, height),
|
||||
hints,
|
||||
results,
|
||||
xOffset + maxX as u32,
|
||||
@@ -166,7 +166,7 @@ impl<T: Reader> GenericMultipleBarcodeReader<T> {
|
||||
// Decode below barcode
|
||||
if maxY < (height as f32) - Self::MIN_DIMENSION_TO_RECUR {
|
||||
self.doDecodeMultiple(
|
||||
&image.crop(0, maxY as usize, width, height - maxY as usize),
|
||||
&mut image.crop(0, maxY as usize, width, height - maxY as usize),
|
||||
hints,
|
||||
results,
|
||||
xOffset,
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use std::{collections::HashSet, path::PathBuf, rc::Rc};
|
||||
use std::{cell::RefCell, collections::HashSet, path::PathBuf, rc::Rc};
|
||||
|
||||
use crate::{
|
||||
common::HybridBinarizer, BarcodeFormat, BinaryBitmap, BufferedImageLuminanceSource,
|
||||
@@ -38,10 +38,14 @@ fn testMulti() {
|
||||
.decode()
|
||||
.expect("must decode");
|
||||
let source = BufferedImageLuminanceSource::new(image);
|
||||
let bitmap = BinaryBitmap::new(Rc::new(HybridBinarizer::new(Box::new(source))));
|
||||
let mut bitmap = BinaryBitmap::new(Rc::new(RefCell::new(HybridBinarizer::new(Box::new(
|
||||
source,
|
||||
)))));
|
||||
|
||||
let mut reader = GenericMultipleBarcodeReader::new(MultiFormatReader::default());
|
||||
let results = reader.decode_multiple(&bitmap).expect("must decode multi");
|
||||
let results = reader
|
||||
.decode_multiple(&mut bitmap)
|
||||
.expect("must decode multi");
|
||||
// assertNotNull(results);
|
||||
assert_eq!(2, results.len());
|
||||
|
||||
@@ -63,10 +67,14 @@ fn testMultiQR() {
|
||||
.decode()
|
||||
.expect("must decode");
|
||||
let source = BufferedImageLuminanceSource::new(image);
|
||||
let bitmap = BinaryBitmap::new(Rc::new(HybridBinarizer::new(Box::new(source))));
|
||||
let mut bitmap = BinaryBitmap::new(Rc::new(RefCell::new(HybridBinarizer::new(Box::new(
|
||||
source,
|
||||
)))));
|
||||
|
||||
let mut reader = GenericMultipleBarcodeReader::new(MultiFormatReader::default());
|
||||
let results = reader.decode_multiple(&bitmap).expect("must decode multi");
|
||||
let results = reader
|
||||
.decode_multiple(&mut bitmap)
|
||||
.expect("must decode multi");
|
||||
assert_eq!(4, results.len());
|
||||
|
||||
let mut barcodeContents = HashSet::new();
|
||||
|
||||
@@ -23,11 +23,12 @@ use crate::{BinaryBitmap, DecodingHintDictionary, Exceptions, RXingResult};
|
||||
* @author Sean Owen
|
||||
*/
|
||||
pub trait MultipleBarcodeReader {
|
||||
fn decode_multiple(&mut self, image: &BinaryBitmap) -> Result<Vec<RXingResult>, Exceptions>;
|
||||
fn decode_multiple(&mut self, image: &mut BinaryBitmap)
|
||||
-> Result<Vec<RXingResult>, Exceptions>;
|
||||
|
||||
fn decode_multiple_with_hints(
|
||||
&mut self,
|
||||
image: &BinaryBitmap,
|
||||
image: &mut BinaryBitmap,
|
||||
hints: &DecodingHintDictionary,
|
||||
) -> Result<Vec<RXingResult>, Exceptions>;
|
||||
}
|
||||
|
||||
@@ -38,14 +38,14 @@ pub struct QRCodeMultiReader(QRCodeReader);
|
||||
impl MultipleBarcodeReader for QRCodeMultiReader {
|
||||
fn decode_multiple(
|
||||
&mut self,
|
||||
image: &crate::BinaryBitmap,
|
||||
image: &mut crate::BinaryBitmap,
|
||||
) -> Result<Vec<crate::RXingResult>, crate::Exceptions> {
|
||||
self.decode_multiple_with_hints(image, &HashMap::new())
|
||||
}
|
||||
|
||||
fn decode_multiple_with_hints(
|
||||
&mut self,
|
||||
image: &crate::BinaryBitmap,
|
||||
image: &mut crate::BinaryBitmap,
|
||||
hints: &crate::DecodingHintDictionary,
|
||||
) -> Result<Vec<crate::RXingResult>, crate::Exceptions> {
|
||||
let mut results = Vec::new();
|
||||
@@ -221,7 +221,7 @@ mod multi_qr_code_test_case {
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use std::{collections::HashSet, path::PathBuf, rc::Rc};
|
||||
use std::{cell::RefCell, collections::HashSet, path::PathBuf, rc::Rc};
|
||||
|
||||
use image;
|
||||
|
||||
@@ -249,10 +249,12 @@ mod multi_qr_code_test_case {
|
||||
.decode()
|
||||
.expect("must decode");
|
||||
let source = BufferedImageLuminanceSource::new(image);
|
||||
let bitmap = BinaryBitmap::new(Rc::new(HybridBinarizer::new(Box::new(source))));
|
||||
let mut bitmap = BinaryBitmap::new(Rc::new(RefCell::new(HybridBinarizer::new(Box::new(
|
||||
source,
|
||||
)))));
|
||||
|
||||
let mut reader = QRCodeMultiReader::new();
|
||||
let results = reader.decode_multiple(&bitmap).expect("must decode");
|
||||
let results = reader.decode_multiple(&mut bitmap).expect("must decode");
|
||||
// assertNotNull(results);
|
||||
assert_eq!(4, results.len());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user