From 60fdd8e79418d4786401a3a16aa005380bf6bae3 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Mon, 19 Dec 2022 13:54:54 -0600 Subject: [PATCH] metadata, value, bounding, codword, column port --- src/pdf417/decoder/BarcodeMetadata.java | 58 ------ src/pdf417/decoder/BarcodeValue.java | 68 ------- src/pdf417/decoder/BoundingBox.java | 157 --------------- src/pdf417/decoder/Codeword.java | 84 -------- src/pdf417/decoder/DetectionResultColumn.java | 95 ---------- src/pdf417/decoder/barcode_metadata.rs | 60 ++++++ src/pdf417/decoder/barcode_value.rs | 78 ++++++++ src/pdf417/decoder/bounding_box.rs | 179 ++++++++++++++++++ src/pdf417/decoder/codeword.rs | 92 +++++++++ src/pdf417/decoder/detection_result_column.rs | 111 +++++++++++ src/pdf417/decoder/mod.rs | 15 ++ 11 files changed, 535 insertions(+), 462 deletions(-) delete mode 100644 src/pdf417/decoder/BarcodeMetadata.java delete mode 100644 src/pdf417/decoder/BarcodeValue.java delete mode 100644 src/pdf417/decoder/BoundingBox.java delete mode 100644 src/pdf417/decoder/Codeword.java delete mode 100644 src/pdf417/decoder/DetectionResultColumn.java create mode 100644 src/pdf417/decoder/barcode_metadata.rs create mode 100644 src/pdf417/decoder/barcode_value.rs create mode 100644 src/pdf417/decoder/bounding_box.rs create mode 100644 src/pdf417/decoder/codeword.rs create mode 100644 src/pdf417/decoder/detection_result_column.rs diff --git a/src/pdf417/decoder/BarcodeMetadata.java b/src/pdf417/decoder/BarcodeMetadata.java deleted file mode 100644 index 9c1acbf..0000000 --- a/src/pdf417/decoder/BarcodeMetadata.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2013 ZXing authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.zxing.pdf417.decoder; - -/** - * @author Guenther Grau - */ -final class BarcodeMetadata { - - private final int columnCount; - private final int errorCorrectionLevel; - private final int rowCountUpperPart; - private final int rowCountLowerPart; - private final int rowCount; - - BarcodeMetadata(int columnCount, int rowCountUpperPart, int rowCountLowerPart, int errorCorrectionLevel) { - this.columnCount = columnCount; - this.errorCorrectionLevel = errorCorrectionLevel; - this.rowCountUpperPart = rowCountUpperPart; - this.rowCountLowerPart = rowCountLowerPart; - this.rowCount = rowCountUpperPart + rowCountLowerPart; - } - - int getColumnCount() { - return columnCount; - } - - int getErrorCorrectionLevel() { - return errorCorrectionLevel; - } - - int getRowCount() { - return rowCount; - } - - int getRowCountUpperPart() { - return rowCountUpperPart; - } - - int getRowCountLowerPart() { - return rowCountLowerPart; - } - -} diff --git a/src/pdf417/decoder/BarcodeValue.java b/src/pdf417/decoder/BarcodeValue.java deleted file mode 100644 index 55a1c68..0000000 --- a/src/pdf417/decoder/BarcodeValue.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2013 ZXing authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.zxing.pdf417.decoder; - -import com.google.zxing.pdf417.PDF417Common; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; - -/** - * @author Guenther Grau - */ -final class BarcodeValue { - private final Map values = new HashMap<>(); - - /** - * Add an occurrence of a value - */ - void setValue(int value) { - Integer confidence = values.get(value); - if (confidence == null) { - confidence = 0; - } - confidence++; - values.put(value, confidence); - } - - /** - * Determines the maximum occurrence of a set value and returns all values which were set with this occurrence. - * @return an array of int, containing the values with the highest occurrence, or null, if no value was set - */ - int[] getValue() { - int maxConfidence = -1; - Collection result = new ArrayList<>(); - for (Entry entry : values.entrySet()) { - if (entry.getValue() > maxConfidence) { - maxConfidence = entry.getValue(); - result.clear(); - result.add(entry.getKey()); - } else if (entry.getValue() == maxConfidence) { - result.add(entry.getKey()); - } - } - return PDF417Common.toIntArray(result); - } - - Integer getConfidence(int value) { - return values.get(value); - } - -} diff --git a/src/pdf417/decoder/BoundingBox.java b/src/pdf417/decoder/BoundingBox.java deleted file mode 100644 index 2d30885..0000000 --- a/src/pdf417/decoder/BoundingBox.java +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Copyright 2013 ZXing authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.zxing.pdf417.decoder; - -import com.google.zxing.NotFoundException; -import com.google.zxing.RXingResultPoint; -import com.google.zxing.common.BitMatrix; - -/** - * @author Guenther Grau - */ -final class BoundingBox { - - private final BitMatrix image; - private final RXingResultPoint topLeft; - private final RXingResultPoint bottomLeft; - private final RXingResultPoint topRight; - private final RXingResultPoint bottomRight; - private final int minX; - private final int maxX; - private final int minY; - private final int maxY; - - BoundingBox(BitMatrix image, - RXingResultPoint topLeft, - RXingResultPoint bottomLeft, - RXingResultPoint topRight, - RXingResultPoint bottomRight) throws NotFoundException { - boolean leftUnspecified = topLeft == null || bottomLeft == null; - boolean rightUnspecified = topRight == null || bottomRight == null; - if (leftUnspecified && rightUnspecified) { - throw NotFoundException.getNotFoundInstance(); - } - if (leftUnspecified) { - topLeft = new RXingResultPoint(0, topRight.getY()); - bottomLeft = new RXingResultPoint(0, bottomRight.getY()); - } else if (rightUnspecified) { - topRight = new RXingResultPoint(image.getWidth() - 1, topLeft.getY()); - bottomRight = new RXingResultPoint(image.getWidth() - 1, bottomLeft.getY()); - } - this.image = image; - this.topLeft = topLeft; - this.bottomLeft = bottomLeft; - this.topRight = topRight; - this.bottomRight = bottomRight; - this.minX = (int) Math.min(topLeft.getX(), bottomLeft.getX()); - this.maxX = (int) Math.max(topRight.getX(), bottomRight.getX()); - this.minY = (int) Math.min(topLeft.getY(), topRight.getY()); - this.maxY = (int) Math.max(bottomLeft.getY(), bottomRight.getY()); - } - - BoundingBox(BoundingBox boundingBox) { - this.image = boundingBox.image; - this.topLeft = boundingBox.topLeft; - this.bottomLeft = boundingBox.bottomLeft; - this.topRight = boundingBox.topRight; - this.bottomRight = boundingBox.bottomRight; - this.minX = boundingBox.minX; - this.maxX = boundingBox.maxX; - this.minY = boundingBox.minY; - this.maxY = boundingBox.maxY; - } - - static BoundingBox merge(BoundingBox leftBox, BoundingBox rightBox) throws NotFoundException { - if (leftBox == null) { - return rightBox; - } - if (rightBox == null) { - return leftBox; - } - return new BoundingBox(leftBox.image, leftBox.topLeft, leftBox.bottomLeft, rightBox.topRight, rightBox.bottomRight); - } - - BoundingBox addMissingRows(int missingStartRows, int missingEndRows, boolean isLeft) throws NotFoundException { - RXingResultPoint newTopLeft = topLeft; - RXingResultPoint newBottomLeft = bottomLeft; - RXingResultPoint newTopRight = topRight; - RXingResultPoint newBottomRight = bottomRight; - - if (missingStartRows > 0) { - RXingResultPoint top = isLeft ? topLeft : topRight; - int newMinY = (int) top.getY() - missingStartRows; - if (newMinY < 0) { - newMinY = 0; - } - RXingResultPoint newTop = new RXingResultPoint(top.getX(), newMinY); - if (isLeft) { - newTopLeft = newTop; - } else { - newTopRight = newTop; - } - } - - if (missingEndRows > 0) { - RXingResultPoint bottom = isLeft ? bottomLeft : bottomRight; - int newMaxY = (int) bottom.getY() + missingEndRows; - if (newMaxY >= image.getHeight()) { - newMaxY = image.getHeight() - 1; - } - RXingResultPoint newBottom = new RXingResultPoint(bottom.getX(), newMaxY); - if (isLeft) { - newBottomLeft = newBottom; - } else { - newBottomRight = newBottom; - } - } - - return new BoundingBox(image, newTopLeft, newBottomLeft, newTopRight, newBottomRight); - } - - int getMinX() { - return minX; - } - - int getMaxX() { - return maxX; - } - - int getMinY() { - return minY; - } - - int getMaxY() { - return maxY; - } - - RXingResultPoint getTopLeft() { - return topLeft; - } - - RXingResultPoint getTopRight() { - return topRight; - } - - RXingResultPoint getBottomLeft() { - return bottomLeft; - } - - RXingResultPoint getBottomRight() { - return bottomRight; - } - -} diff --git a/src/pdf417/decoder/Codeword.java b/src/pdf417/decoder/Codeword.java deleted file mode 100644 index bb5477d..0000000 --- a/src/pdf417/decoder/Codeword.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright 2013 ZXing authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.zxing.pdf417.decoder; - -/** - * @author Guenther Grau - */ -final class Codeword { - - private static final int BARCODE_ROW_UNKNOWN = -1; - - private final int startX; - private final int endX; - private final int bucket; - private final int value; - private int rowNumber = BARCODE_ROW_UNKNOWN; - - Codeword(int startX, int endX, int bucket, int value) { - this.startX = startX; - this.endX = endX; - this.bucket = bucket; - this.value = value; - } - - boolean hasValidRowNumber() { - return isValidRowNumber(rowNumber); - } - - boolean isValidRowNumber(int rowNumber) { - return rowNumber != BARCODE_ROW_UNKNOWN && bucket == (rowNumber % 3) * 3; - } - - void setRowNumberAsRowIndicatorColumn() { - rowNumber = (value / 30) * 3 + bucket / 3; - } - - int getWidth() { - return endX - startX; - } - - int getStartX() { - return startX; - } - - int getEndX() { - return endX; - } - - int getBucket() { - return bucket; - } - - int getValue() { - return value; - } - - int getRowNumber() { - return rowNumber; - } - - void setRowNumber(int rowNumber) { - this.rowNumber = rowNumber; - } - - @Override - public String toString() { - return rowNumber + "|" + value; - } - -} diff --git a/src/pdf417/decoder/DetectionResultColumn.java b/src/pdf417/decoder/DetectionResultColumn.java deleted file mode 100644 index f4f62ad..0000000 --- a/src/pdf417/decoder/DetectionResultColumn.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright 2013 ZXing authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.zxing.pdf417.decoder; - -import java.util.Formatter; - -/** - * @author Guenther Grau - */ -class DetectionRXingResultColumn { - - private static final int MAX_NEARBY_DISTANCE = 5; - - private final BoundingBox boundingBox; - private final Codeword[] codewords; - - DetectionRXingResultColumn(BoundingBox boundingBox) { - this.boundingBox = new BoundingBox(boundingBox); - codewords = new Codeword[boundingBox.getMaxY() - boundingBox.getMinY() + 1]; - } - - final Codeword getCodewordNearby(int imageRow) { - Codeword codeword = getCodeword(imageRow); - if (codeword != null) { - return codeword; - } - for (int i = 1; i < MAX_NEARBY_DISTANCE; i++) { - int nearImageRow = imageRowToCodewordIndex(imageRow) - i; - if (nearImageRow >= 0) { - codeword = codewords[nearImageRow]; - if (codeword != null) { - return codeword; - } - } - nearImageRow = imageRowToCodewordIndex(imageRow) + i; - if (nearImageRow < codewords.length) { - codeword = codewords[nearImageRow]; - if (codeword != null) { - return codeword; - } - } - } - return null; - } - - final int imageRowToCodewordIndex(int imageRow) { - return imageRow - boundingBox.getMinY(); - } - - final void setCodeword(int imageRow, Codeword codeword) { - codewords[imageRowToCodewordIndex(imageRow)] = codeword; - } - - final Codeword getCodeword(int imageRow) { - return codewords[imageRowToCodewordIndex(imageRow)]; - } - - final BoundingBox getBoundingBox() { - return boundingBox; - } - - final Codeword[] getCodewords() { - return codewords; - } - - @Override - public String toString() { - try (Formatter formatter = new Formatter()) { - int row = 0; - for (Codeword codeword : codewords) { - if (codeword == null) { - formatter.format("%3d: | %n", row++); - continue; - } - formatter.format("%3d: %3d|%3d%n", row++, codeword.getRowNumber(), codeword.getValue()); - } - return formatter.toString(); - } - } - -} diff --git a/src/pdf417/decoder/barcode_metadata.rs b/src/pdf417/decoder/barcode_metadata.rs new file mode 100644 index 0000000..e03462d --- /dev/null +++ b/src/pdf417/decoder/barcode_metadata.rs @@ -0,0 +1,60 @@ +/* + * Copyright 2013 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @author Guenther Grau + */ +pub struct BarcodeMetadata { + + columnCount: u32, + errorCorrectionLevel: u32, + rowCountUpperPart: u32, + rowCountLowerPart: u32, + rowCount: u32, +} +impl BarcodeMetadata { + + pub fn new( columnCount:u32, rowCountUpperPart:u32, rowCountLowerPart:u32, errorCorrectionLevel:u32) -> Self{ + Self { + columnCount, + errorCorrectionLevel, + rowCountUpperPart, + rowCountLowerPart, + rowCount: rowCountUpperPart + rowCountLowerPart, + } + } + + pub fn getColumnCount(&self) -> u32{ + self. columnCount + } + + pub fn getErrorCorrectionLevel(&self) -> u32{ + self. errorCorrectionLevel + } + + pub fn getRowCount(&self) -> u32 { + self. rowCount + } + + pub fn getRowCountUpperPart(&self) -> u32{ + self. rowCountUpperPart + } + + pub fn getRowCountLowerPart(&self) -> u32 { + self. rowCountLowerPart + } + +} diff --git a/src/pdf417/decoder/barcode_value.rs b/src/pdf417/decoder/barcode_value.rs new file mode 100644 index 0000000..6f85d7d --- /dev/null +++ b/src/pdf417/decoder/barcode_value.rs @@ -0,0 +1,78 @@ +/* + * Copyright 2013 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use std::collections::HashMap; + +/** + * @author Guenther Grau + */ +pub struct BarcodeValue(HashMap); + // private final Map values = new HashMap<>(); + + impl BarcodeValue { +pub fn new() -> Self { + Self::default() +} + + /** + * Add an occurrence of a value + */ + pub fn setValue(&mut self, value:u32) { + let mut confidence = if let Some(value) = self.0.get(&value) { + *value + }else { + 0 + }; + confidence+=1; + self.0.insert(value, confidence); + } + + /** + * Determines the maximum occurrence of a set value and returns all values which were set with this occurrence. + * @return an array of int, containing the values with the highest occurrence, or null, if no value was set + */ + pub fn getValue(&self) -> Vec { + let mut maxConfidence = -1_i32; + let mut result = Vec::new(); + for (key,value) in &self.0 { + // for (Entry entry : values.entrySet()) { + if *value as i32 > maxConfidence { + maxConfidence = *value as i32; + result.clear(); + result.push(*key); + } else if *value as i32 == maxConfidence { + result.push(*key); + } + } + + result + } + + pub fn getConfidence(&self, value:u32) -> u32{ + if let Some(v) = self.0.get(&value) { + *v + }else { + 0 + } + } + +} + +impl Default for BarcodeValue { + fn default() -> Self { + Self(Default::default()) + } +} \ No newline at end of file diff --git a/src/pdf417/decoder/bounding_box.rs b/src/pdf417/decoder/bounding_box.rs new file mode 100644 index 0000000..8e9870c --- /dev/null +++ b/src/pdf417/decoder/bounding_box.rs @@ -0,0 +1,179 @@ +/* + * Copyright 2013 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use crate::{common::BitMatrix, RXingResultPoint, Exceptions, ResultPoint}; + +/** + * @author Guenther Grau + */ +#[derive(Clone)] +pub struct BoundingBox<'a> { + + image:&'a BitMatrix, + topLeft:RXingResultPoint, + bottomLeft:RXingResultPoint, + topRight:RXingResultPoint, + bottomRight:RXingResultPoint, + minX:u32, + maxX:u32, + minY:u32, + maxY:u32, +} +impl<'a> BoundingBox<'_> { + + pub fn new( image:&'a BitMatrix, + topLeft:Option, + bottomLeft:Option, + topRight:Option, + bottomRight:Option) -> Result,Exceptions> { + let leftUnspecified = topLeft.is_none() || bottomLeft .is_none(); + let rightUnspecified = topRight .is_none() || bottomRight .is_none(); + if leftUnspecified && rightUnspecified { + return Err(Exceptions::NotFoundException("".to_owned())) + } + + let newTopLeft; + let newBottomLeft; + let newTopRight; + let newBottomRight; + + if leftUnspecified { + newTopRight = topRight.unwrap(); + newBottomRight = bottomRight.unwrap(); + newTopLeft = RXingResultPoint::new(0.0, newTopRight.getY()); + newBottomLeft = RXingResultPoint::new(0.0, newBottomRight.getY()); + } else if rightUnspecified { + newTopLeft = topLeft.unwrap(); + newBottomLeft = bottomLeft.unwrap(); + newTopRight = RXingResultPoint::new(image.getWidth() as f32 - 1.0, newTopLeft.getY()); + newBottomRight = RXingResultPoint::new(image.getWidth() as f32 - 1.0, newBottomLeft.getY()); + }else { + newTopLeft = topLeft.unwrap(); + newTopRight = topRight.unwrap(); + newBottomLeft = bottomLeft.unwrap(); + newBottomRight = bottomRight.unwrap(); + } + + Ok(BoundingBox { + image, + minX: newTopLeft.getX().min(newBottomLeft.getX()) as u32, + maxX: newTopRight.getX().max(newBottomRight.getX()) as u32, + minY: newTopLeft.getY().min(newTopRight.getY()) as u32, + maxY: newBottomLeft.getY().max(newBottomRight.getY()) as u32, + topLeft: newTopLeft, + bottomLeft: newBottomLeft, + topRight: newTopRight, + bottomRight: newBottomRight, + }) + } + + pub fn from_other( boundingBox:&'a BoundingBox) -> BoundingBox<'a> { + BoundingBox { + image: boundingBox.image, + topLeft: boundingBox.topLeft, + bottomLeft: boundingBox.bottomLeft, + topRight: boundingBox.topRight, + bottomRight: boundingBox.bottomRight, + minX: boundingBox.minX, + maxX: boundingBox.maxX, + minY: boundingBox.minY, + maxY: boundingBox.maxY, + } + } + + pub fn merge( leftBox:Option<&'a BoundingBox>, rightBox:Option<&'a BoundingBox>) -> Result,Exceptions> { + if leftBox.is_none() { + return Ok(rightBox.unwrap().clone()) + } + if rightBox .is_none() { + return Ok(leftBox.unwrap().clone()) + } + let leftBox = leftBox.unwrap(); + let rightBox = rightBox.unwrap(); + + BoundingBox::new(leftBox.image, Some(leftBox.topLeft), Some(leftBox.bottomLeft), Some(rightBox.topRight), Some(rightBox.bottomRight)) + } + + pub fn addMissingRows(&self, missingStartRows:u32, missingEndRows:u32, isLeft:bool) -> Result { + let mut newTopLeft = self.topLeft; + let mut newBottomLeft = self.bottomLeft; + let mut newTopRight = self.topRight; + let mut newBottomRight = self.bottomRight; + + if missingStartRows > 0 { + let top = if isLeft {self.topLeft} else {self.topRight}; + let mut newMinY = top.getY() - missingStartRows as f32; + if newMinY < 0.0 { + newMinY = 0.0; + } + let newTop = RXingResultPoint::new(top.getX(), newMinY); + if isLeft { + newTopLeft = newTop; + } else { + newTopRight = newTop; + } + } + + if missingEndRows > 0 { + let bottom = if isLeft { self.bottomLeft} else {self.bottomRight}; + let mut newMaxY = bottom.getY() as u32 + missingEndRows; + if newMaxY >= self.image.getHeight() { + newMaxY = self.image.getHeight() - 1; + } + let newBottom = RXingResultPoint::new(bottom.getX(), newMaxY as f32); + if isLeft { + newBottomLeft = newBottom; + } else { + newBottomRight = newBottom; + } + } + + BoundingBox::new(self.image, Some(newTopLeft), Some(newBottomLeft), Some(newTopRight), Some(newBottomRight)) + } + + pub fn getMinX(&self) -> u32{ + self. minX + } + + pub fn getMaxX(&self) -> u32{ + self. maxX + } + + pub fn getMinY(&self) -> u32{ + self. minY + } + + pub fn getMaxY(&self) -> u32{ + self. maxY + } + + pub fn getTopLeft(&self) -> &RXingResultPoint{ + &self. topLeft + } + + pub fn getTopRight(&self) -> &RXingResultPoint{ + &self. topRight + } + + pub fn getBottomLeft(&self) -> &RXingResultPoint{ + &self. bottomLeft + } + + pub fn getBottomRight(&self) -> &RXingResultPoint{ + &self. bottomRight + } + +} diff --git a/src/pdf417/decoder/codeword.rs b/src/pdf417/decoder/codeword.rs new file mode 100644 index 0000000..7626dd0 --- /dev/null +++ b/src/pdf417/decoder/codeword.rs @@ -0,0 +1,92 @@ +/* + * Copyright 2013 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use std::fmt::Display; + + const BARCODE_ROW_UNKNOWN : i32 = -1; + +/** + * @author Guenther Grau + */ +#[derive(Clone, Copy)] +pub struct Codeword { + + startX:u32, + endX:u32, + bucket:u32, + value:u32, + rowNumber : i32, +} + +impl Codeword { + + pub fn new( startX:u32, endX:u32, bucket:u32, value:u32)->Self { + Self { + startX, + endX, + bucket, + value, + rowNumber: BARCODE_ROW_UNKNOWN, + } + } + + pub fn hasValidRowNumber(&self) -> bool{ + self.isValidRowNumber(self.rowNumber) + } + + pub fn isValidRowNumber(&self, rowNumber:i32) -> bool{ + rowNumber != BARCODE_ROW_UNKNOWN && self.bucket == (rowNumber as u32 % 3) * 3 + } + + pub fn setRowNumberAsRowIndicatorColumn(&mut self) { + self.rowNumber =( (self.value / 30) * 3 + self.bucket / 3) as i32; + } + + pub fn getWidth(&self) -> u32{ + self.endX - self.startX + } + + pub fn getStartX(&self) -> u32 { + self. startX + } + + pub fn getEndX(&self) -> u32{ + self. endX + } + + pub fn getBucket(&self) -> u32 { + self. bucket + } + + pub fn getValue(&self) -> u32 { + self.value + } + + pub fn getRowNumber(&self) -> i32{ + self. rowNumber + } + + pub fn setRowNumber(&mut self, rowNumber:i32) { + self.rowNumber = rowNumber; + } + +} + +impl Display for Codeword { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f,"{}|{}", self.rowNumber, self.value) + } +} \ No newline at end of file diff --git a/src/pdf417/decoder/detection_result_column.rs b/src/pdf417/decoder/detection_result_column.rs new file mode 100644 index 0000000..e614d1c --- /dev/null +++ b/src/pdf417/decoder/detection_result_column.rs @@ -0,0 +1,111 @@ +/* + * Copyright 2013 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use std::fmt::Display; + +use super::{BoundingBox, Codeword}; + + const MAX_NEARBY_DISTANCE :u32 = 5; + +/** + * @author Guenther Grau + */ +pub struct DetectionRXingResultColumn<'a> { + boundingBox: BoundingBox<'a>, + codewords:Vec>, +} + +impl<'a> DetectionRXingResultColumn<'_> { + + pub fn new( boundingBox:&'a BoundingBox) -> DetectionRXingResultColumn<'a> { + DetectionRXingResultColumn { + boundingBox: BoundingBox::from_other(boundingBox), + codewords: vec![None;(boundingBox.getMaxY() - boundingBox.getMinY() + 1) as usize], + } + // this.boundingBox = new BoundingBox(boundingBox); + // codewords = new Codeword[boundingBox.getMaxY() - boundingBox.getMinY() + 1]; + } + + pub fn getCodewordNearby(&self, imageRow:u32) -> &Option { + let mut codeword = self.getCodeword(imageRow); + if codeword.is_some() { + return codeword; + } + for i in 1..MAX_NEARBY_DISTANCE as usize{ + // for (int i = 1; i < MAX_NEARBY_DISTANCE; i++) { + let mut nearImageRow = self.imageRowToCodewordIndex(imageRow) - i; + if nearImageRow >= 0 { + codeword = &self.codewords[nearImageRow]; + if codeword.is_some() { + return codeword; + } + } + nearImageRow = self.imageRowToCodewordIndex(imageRow) + i ; + if nearImageRow < self.codewords.len() { + codeword = &self.codewords[nearImageRow]; + if codeword.is_some() { + return codeword; + } + } + } + &None + } + + pub fn imageRowToCodewordIndex(&self, imageRow:u32) -> usize{ + (imageRow - self.boundingBox.getMinY()) as usize + } + + pub fn setCodeword(&mut self, imageRow:u32, codeword:Codeword) { + let pos = self.imageRowToCodewordIndex(imageRow); + self.codewords[pos] = Some(codeword); + } + + pub fn getCodeword(&self, imageRow:u32) -> &Option{ + &self. codewords[self.imageRowToCodewordIndex(imageRow)] + } + + pub fn getBoundingBox(&self) -> &BoundingBox { + &self. boundingBox + } + + pub fn getCodewords(&self) -> &[Option] { + &self.codewords + } + + + +} + +impl Display for DetectionRXingResultColumn<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + todo!() + } + + // @Override + // public String toString() { + // try (Formatter formatter = new Formatter()) { + // int row = 0; + // for (Codeword codeword : codewords) { + // if (codeword == null) { + // formatter.format("%3d: | %n", row++); + // continue; + // } + // formatter.format("%3d: %3d|%3d%n", row++, codeword.getRowNumber(), codeword.getValue()); + // } + // return formatter.toString(); + // } + // } +} \ No newline at end of file diff --git a/src/pdf417/decoder/mod.rs b/src/pdf417/decoder/mod.rs index ef7b2e7..ed2d7db 100644 --- a/src/pdf417/decoder/mod.rs +++ b/src/pdf417/decoder/mod.rs @@ -1 +1,16 @@ pub mod ec; + +mod codeword; +pub use codeword::*; + +mod barcode_value; +pub use barcode_value::*; + +mod barcode_metadata; +pub use barcode_metadata::*; + +mod bounding_box; +pub use bounding_box::*; + +mod detection_result_column; +pub use detection_result_column::*; \ No newline at end of file