From 033b196731681e56d72624eeaef382bb6d4f88fa Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Sun, 21 Aug 2022 15:46:30 -0500 Subject: [PATCH] continued --- src/Binarizer.java | 87 ---- src/BinaryBitmap.java | 150 ------ src/Dimension.java | 62 --- src/LuminanceSource.java | 157 ------ src/Result.java | 0 src/ResultMetadataType.java | 0 src/ResultPoint.java | 0 src/ResultPointCallback.java | 29 -- src/common/detector/MathUtils.rs | 164 +++--- .../detector/MonochromeRectangleDetector.rs | 0 src/common/detector/WhiteRectangleDetector.rs | 0 src/lib.rs | 468 +++++++++++++++++- 12 files changed, 552 insertions(+), 565 deletions(-) delete mode 100644 src/Binarizer.java delete mode 100644 src/BinaryBitmap.java delete mode 100644 src/Dimension.java delete mode 100644 src/LuminanceSource.java delete mode 100644 src/Result.java delete mode 100644 src/ResultMetadataType.java delete mode 100644 src/ResultPoint.java delete mode 100644 src/ResultPointCallback.java delete mode 100644 src/common/detector/MonochromeRectangleDetector.rs delete mode 100644 src/common/detector/WhiteRectangleDetector.rs diff --git a/src/Binarizer.java b/src/Binarizer.java deleted file mode 100644 index 02af083..0000000 --- a/src/Binarizer.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright 2009 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; - -import com.google.zxing.common.BitArray; -import com.google.zxing.common.BitMatrix; - -/** - * This class hierarchy provides a set of methods to convert luminance data to 1 bit data. - * It allows the algorithm to vary polymorphically, for example allowing a very expensive - * thresholding technique for servers and a fast one for mobile. It also permits the implementation - * to vary, e.g. a JNI version for Android and a Java fallback version for other platforms. - * - * @author dswitkin@google.com (Daniel Switkin) - */ -public abstract class Binarizer { - - private final LuminanceSource source; - - protected Binarizer(LuminanceSource source) { - this.source = source; - } - - public final LuminanceSource getLuminanceSource() { - return source; - } - - /** - * Converts one row of luminance data to 1 bit data. May actually do the conversion, or return - * cached data. Callers should assume this method is expensive and call it as seldom as possible. - * This method is intended for decoding 1D barcodes and may choose to apply sharpening. - * For callers which only examine one row of pixels at a time, the same BitArray should be reused - * and passed in with each call for performance. However it is legal to keep more than one row - * at a time if needed. - * - * @param y The row to fetch, which must be in [0, bitmap height) - * @param row An optional preallocated array. If null or too small, it will be ignored. - * If used, the Binarizer will call BitArray.clear(). Always use the returned object. - * @return The array of bits for this row (true means black). - * @throws NotFoundException if row can't be binarized - */ - public abstract BitArray getBlackRow(int y, BitArray row) throws NotFoundException; - - /** - * Converts a 2D array of luminance data to 1 bit data. As above, assume this method is expensive - * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or - * may not apply sharpening. Therefore, a row from this matrix may not be identical to one - * fetched using getBlackRow(), so don't mix and match between them. - * - * @return The 2D array of bits for the image (true means black). - * @throws NotFoundException if image can't be binarized to make a matrix - */ - public abstract BitMatrix getBlackMatrix() throws NotFoundException; - - /** - * Creates a new object with the same type as this Binarizer implementation, but with pristine - * state. This is needed because Binarizer implementations may be stateful, e.g. keeping a cache - * of 1 bit data. See Effective Java for why we can't use Java's clone() method. - * - * @param source The LuminanceSource this Binarizer will operate on. - * @return A new concrete Binarizer implementation object. - */ - public abstract Binarizer createBinarizer(LuminanceSource source); - - public final int getWidth() { - return source.getWidth(); - } - - public final int getHeight() { - return source.getHeight(); - } - -} diff --git a/src/BinaryBitmap.java b/src/BinaryBitmap.java deleted file mode 100644 index c1ef8a1..0000000 --- a/src/BinaryBitmap.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright 2009 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; - -import com.google.zxing.common.BitArray; -import com.google.zxing.common.BitMatrix; - -/** - * This class is the core bitmap class used by ZXing to represent 1 bit data. Reader objects - * accept a BinaryBitmap and attempt to decode it. - * - * @author dswitkin@google.com (Daniel Switkin) - */ -public final class BinaryBitmap { - - private final Binarizer binarizer; - private BitMatrix matrix; - - public BinaryBitmap(Binarizer binarizer) { - if (binarizer == null) { - throw new IllegalArgumentException("Binarizer must be non-null."); - } - this.binarizer = binarizer; - } - - /** - * @return The width of the bitmap. - */ - public int getWidth() { - return binarizer.getWidth(); - } - - /** - * @return The height of the bitmap. - */ - public int getHeight() { - return binarizer.getHeight(); - } - - /** - * Converts one row of luminance data to 1 bit data. May actually do the conversion, or return - * cached data. Callers should assume this method is expensive and call it as seldom as possible. - * This method is intended for decoding 1D barcodes and may choose to apply sharpening. - * - * @param y The row to fetch, which must be in [0, bitmap height) - * @param row An optional preallocated array. If null or too small, it will be ignored. - * If used, the Binarizer will call BitArray.clear(). Always use the returned object. - * @return The array of bits for this row (true means black). - * @throws NotFoundException if row can't be binarized - */ - public BitArray getBlackRow(int y, BitArray row) throws NotFoundException { - return binarizer.getBlackRow(y, row); - } - - /** - * Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive - * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or - * may not apply sharpening. Therefore, a row from this matrix may not be identical to one - * fetched using getBlackRow(), so don't mix and match between them. - * - * @return The 2D array of bits for the image (true means black). - * @throws NotFoundException if image can't be binarized to make a matrix - */ - public BitMatrix getBlackMatrix() throws NotFoundException { - // The matrix is created on demand the first time it is requested, then cached. There are two - // reasons for this: - // 1. This work will never be done if the caller only installs 1D Reader objects, or if a - // 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 (matrix == null) { - matrix = binarizer.getBlackMatrix(); - } - return matrix; - } - - /** - * @return Whether this bitmap can be cropped. - */ - public boolean isCropSupported() { - return binarizer.getLuminanceSource().isCropSupported(); - } - - /** - * Returns a new object with cropped image data. Implementations may keep a reference to the - * original data rather than a copy. Only callable if isCropSupported() is true. - * - * @param left The left coordinate, which must be in [0,getWidth()) - * @param top The top coordinate, which must be in [0,getHeight()) - * @param width The width of the rectangle to crop. - * @param height The height of the rectangle to crop. - * @return A cropped version of this object. - */ - public BinaryBitmap crop(int left, int top, int width, int height) { - LuminanceSource newSource = binarizer.getLuminanceSource().crop(left, top, width, height); - return new BinaryBitmap(binarizer.createBinarizer(newSource)); - } - - /** - * @return Whether this bitmap supports counter-clockwise rotation. - */ - public boolean isRotateSupported() { - return binarizer.getLuminanceSource().isRotateSupported(); - } - - /** - * Returns a new object with rotated image data by 90 degrees counterclockwise. - * Only callable if {@link #isRotateSupported()} is true. - * - * @return A rotated version of this object. - */ - public BinaryBitmap rotateCounterClockwise() { - LuminanceSource newSource = binarizer.getLuminanceSource().rotateCounterClockwise(); - return new BinaryBitmap(binarizer.createBinarizer(newSource)); - } - - /** - * Returns a new object with rotated image data by 45 degrees counterclockwise. - * Only callable if {@link #isRotateSupported()} is true. - * - * @return A rotated version of this object. - */ - public BinaryBitmap rotateCounterClockwise45() { - LuminanceSource newSource = binarizer.getLuminanceSource().rotateCounterClockwise45(); - return new BinaryBitmap(binarizer.createBinarizer(newSource)); - } - - @Override - public String toString() { - try { - return getBlackMatrix().toString(); - } catch (NotFoundException e) { - return ""; - } - } - -} diff --git a/src/Dimension.java b/src/Dimension.java deleted file mode 100644 index 242e0f7..0000000 --- a/src/Dimension.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2012 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; - -/** - * Simply encapsulates a width and height. - */ -public final class Dimension { - - private final int width; - private final int height; - - public Dimension(int width, int height) { - if (width < 0 || height < 0) { - throw new IllegalArgumentException(); - } - this.width = width; - this.height = height; - } - - public int getWidth() { - return width; - } - - public int getHeight() { - return height; - } - - @Override - public boolean equals(Object other) { - if (other instanceof Dimension) { - Dimension d = (Dimension) other; - return width == d.width && height == d.height; - } - return false; - } - - @Override - public int hashCode() { - return width * 32713 + height; - } - - @Override - public String toString() { - return width + "x" + height; - } - -} diff --git a/src/LuminanceSource.java b/src/LuminanceSource.java deleted file mode 100644 index 1946d02..0000000 --- a/src/LuminanceSource.java +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Copyright 2009 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; - -/** - * The purpose of this class hierarchy is to abstract different bitmap implementations across - * platforms into a standard interface for requesting greyscale luminance values. The interface - * only provides immutable methods; therefore crop and rotation create copies. This is to ensure - * that one Reader does not modify the original luminance source and leave it in an unknown state - * for other Readers in the chain. - * - * @author dswitkin@google.com (Daniel Switkin) - */ -public abstract class LuminanceSource { - - private final int width; - private final int height; - - protected LuminanceSource(int width, int height) { - this.width = width; - this.height = height; - } - - /** - * Fetches one row of luminance data from the underlying platform's bitmap. Values range from - * 0 (black) to 255 (white). Because Java does not have an unsigned byte type, callers will have - * to bitwise and with 0xff for each value. It is preferable for implementations of this method - * to only fetch this row rather than the whole image, since no 2D Readers may be installed and - * getMatrix() may never be called. - * - * @param y The row to fetch, which must be in [0,getHeight()) - * @param row An optional preallocated array. If null or too small, it will be ignored. - * Always use the returned object, and ignore the .length of the array. - * @return An array containing the luminance data. - */ - public abstract byte[] getRow(int y, byte[] row); - - /** - * Fetches luminance data for the underlying bitmap. Values should be fetched using: - * {@code int luminance = array[y * width + x] & 0xff} - * - * @return A row-major 2D array of luminance values. Do not use result.length as it may be - * larger than width * height bytes on some platforms. Do not modify the contents - * of the result. - */ - public abstract byte[] getMatrix(); - - /** - * @return The width of the bitmap. - */ - public final int getWidth() { - return width; - } - - /** - * @return The height of the bitmap. - */ - public final int getHeight() { - return height; - } - - /** - * @return Whether this subclass supports cropping. - */ - public boolean isCropSupported() { - return false; - } - - /** - * Returns a new object with cropped image data. Implementations may keep a reference to the - * original data rather than a copy. Only callable if isCropSupported() is true. - * - * @param left The left coordinate, which must be in [0,getWidth()) - * @param top The top coordinate, which must be in [0,getHeight()) - * @param width The width of the rectangle to crop. - * @param height The height of the rectangle to crop. - * @return A cropped version of this object. - */ - public LuminanceSource crop(int left, int top, int width, int height) { - throw new UnsupportedOperationException("This luminance source does not support cropping."); - } - - /** - * @return Whether this subclass supports counter-clockwise rotation. - */ - public boolean isRotateSupported() { - return false; - } - - /** - * @return a wrapper of this {@code LuminanceSource} which inverts the luminances it returns -- black becomes - * white and vice versa, and each value becomes (255-value). - */ - public LuminanceSource invert() { - return new InvertedLuminanceSource(this); - } - - /** - * Returns a new object with rotated image data by 90 degrees counterclockwise. - * Only callable if {@link #isRotateSupported()} is true. - * - * @return A rotated version of this object. - */ - public LuminanceSource rotateCounterClockwise() { - throw new UnsupportedOperationException("This luminance source does not support rotation by 90 degrees."); - } - - /** - * Returns a new object with rotated image data by 45 degrees counterclockwise. - * Only callable if {@link #isRotateSupported()} is true. - * - * @return A rotated version of this object. - */ - public LuminanceSource rotateCounterClockwise45() { - throw new UnsupportedOperationException("This luminance source does not support rotation by 45 degrees."); - } - - @Override - public final String toString() { - byte[] row = new byte[width]; - StringBuilder result = new StringBuilder(height * (width + 1)); - for (int y = 0; y < height; y++) { - row = getRow(y, row); - for (int x = 0; x < width; x++) { - int luminance = row[x] & 0xFF; - char c; - if (luminance < 0x40) { - c = '#'; - } else if (luminance < 0x80) { - c = '+'; - } else if (luminance < 0xC0) { - c = '.'; - } else { - c = ' '; - } - result.append(c); - } - result.append('\n'); - } - return result.toString(); - } - -} diff --git a/src/Result.java b/src/Result.java deleted file mode 100644 index e69de29..0000000 diff --git a/src/ResultMetadataType.java b/src/ResultMetadataType.java deleted file mode 100644 index e69de29..0000000 diff --git a/src/ResultPoint.java b/src/ResultPoint.java deleted file mode 100644 index e69de29..0000000 diff --git a/src/ResultPointCallback.java b/src/ResultPointCallback.java deleted file mode 100644 index e3c0074..0000000 --- a/src/ResultPointCallback.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2009 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; - -/** - * Callback which is invoked when a possible result point (significant - * point in the barcode image such as a corner) is found. - * - * @see DecodeHintType#NEED_RESULT_POINT_CALLBACK - */ -public interface RXingResultPointCallback { - - void foundPossibleRXingResultPoint(RXingResultPoint point); - -} diff --git a/src/common/detector/MathUtils.rs b/src/common/detector/MathUtils.rs index 50160f5..dbc92e1 100644 --- a/src/common/detector/MathUtils.rs +++ b/src/common/detector/MathUtils.rs @@ -15,108 +15,114 @@ */ //package com.google.zxing.common.detector; -use std::{i32,f32}; +use std::{f32, i32}; /** * General math-related and numeric utility functions. */ - /** - * Ends up being a bit faster than {@link Math#round(float)}. This merely rounds its - * argument to the nearest int, where x.5 rounds up to x+1. Semantics of this shortcut - * differ slightly from {@link Math#round(float)} in that half rounds down for negative - * values. -2.5 rounds to -3, not -2. For purposes here it makes no difference. - * - * @param d real value to round - * @return nearest {@code int} - */ - pub fn round(d:f32) -> i32 { - return (d + (if d < 0.0f32 { -0.5f32 } else { 0.5f32})) as i32; - } +/** + * Ends up being a bit faster than {@link Math#round(float)}. This merely rounds its + * argument to the nearest int, where x.5 rounds up to x+1. Semantics of this shortcut + * differ slightly from {@link Math#round(float)} in that half rounds down for negative + * values. -2.5 rounds to -3, not -2. For purposes here it makes no difference. + * + * @param d real value to round + * @return nearest {@code int} + */ +pub fn round(d: f32) -> i32 { + return (d + (if d < 0.0f32 { -0.5f32 } else { 0.5f32 })) as i32; +} - /** - * @param aX point A x coordinate - * @param aY point A y coordinate - * @param bX point B x coordinate - * @param bY point B y coordinate - * @return Euclidean distance between points A and B - */ - pub fn distance_float( aX:f32, aY:f32, bX: f32, bY:f32) -> f32 { - let xDiff:f64 = (aX - bX).into(); - let yDiff:f64 = (aY - bY).into(); +/** + * @param aX point A x coordinate + * @param aY point A y coordinate + * @param bX point B x coordinate + * @param bY point B y coordinate + * @return Euclidean distance between points A and B + */ +pub fn distance_float(aX: f32, aY: f32, bX: f32, bY: f32) -> f32 { + let xDiff: f64 = (aX - bX).into(); + let yDiff: f64 = (aY - bY).into(); return (xDiff * xDiff + yDiff * yDiff).sqrt() as f32; - } +} - /** - * @param aX point A x coordinate - * @param aY point A y coordinate - * @param bX point B x coordinate - * @param bY point B y coordinate - * @return Euclidean distance between points A and B - */ - pub fn distance_int( aX : i32, aY: i32, bX: i32, bY: i32) -> f32 { - let xDiff : f64 = (aX - bX).into(); - let yDiff :f64 = (aY - bY).into(); - return (xDiff * xDiff + yDiff * yDiff).sqrt() as f32; - } +/** + * @param aX point A x coordinate + * @param aY point A y coordinate + * @param bX point B x coordinate + * @param bY point B y coordinate + * @return Euclidean distance between points A and B + */ +pub fn distance_int(aX: i32, aY: i32, bX: i32, bY: i32) -> f32 { + let xDiff: f64 = (aX - bX).into(); + let yDiff: f64 = (aY - bY).into(); + return (xDiff * xDiff + yDiff * yDiff).sqrt() as f32; +} - /** - * @param array values to sum - * @return sum of values in array - */ - pub fn sum( array : &[i32]) -> i32 { +/** + * @param array values to sum + * @return sum of values in array + */ +pub fn sum(array: &[i32]) -> i32 { let count = 0; - for a in array { - count += a; + for a in array { + count += a; } return count; - } +} - #[cfg(test)] - mod tests { +#[cfg(test)] +mod tests { use crate::common::detector::MathUtils; - - static EPSILON : f32 = 1.0E-8f32; + + static EPSILON: f32 = 1.0E-8f32; #[test] - fn testRound() { - assert_eq!(-1, MathUtils::round(-1.0f32)); - assert_eq!(0, MathUtils::round(0.0f32)); - assert_eq!(1, MathUtils::round(1.0f32)); + fn testRound() { + assert_eq!(-1, MathUtils::round(-1.0f32)); + assert_eq!(0, MathUtils::round(0.0f32)); + assert_eq!(1, MathUtils::round(1.0f32)); - assert_eq!(2, MathUtils::round(1.9f32)); - assert_eq!(2, MathUtils::round(2.1f32)); + assert_eq!(2, MathUtils::round(1.9f32)); + assert_eq!(2, MathUtils::round(2.1f32)); - assert_eq!(3, MathUtils::round(2.5f32)); + assert_eq!(3, MathUtils::round(2.5f32)); - assert_eq!(-2, MathUtils::round(-1.9f32)); - assert_eq!(-2, MathUtils::round(-2.1f32)); + assert_eq!(-2, MathUtils::round(-1.9f32)); + assert_eq!(-2, MathUtils::round(-2.1f32)); - assert_eq!(-3, MathUtils::round(-2.5f32)); // This differs from Math.round() + assert_eq!(-3, MathUtils::round(-2.5f32)); // This differs from Math.round() - assert_eq!(i32::MAX, MathUtils::round(i32::MAX as f32)); - assert_eq!(i32::MIN, MathUtils::round(i32::MIN as f32)); + assert_eq!(i32::MAX, MathUtils::round(i32::MAX as f32)); + assert_eq!(i32::MIN, MathUtils::round(i32::MIN as f32)); - assert_eq!(i32::MAX, MathUtils::round(f32::MAX)); - assert_eq!(i32::MIN, MathUtils::round(f32::NEG_INFINITY)); + assert_eq!(i32::MAX, MathUtils::round(f32::MAX)); + assert_eq!(i32::MIN, MathUtils::round(f32::NEG_INFINITY)); - assert_eq!(0, MathUtils::round(f32::NAN)); - } + assert_eq!(0, MathUtils::round(f32::NAN)); + } - #[test] - fn testDistance() { - assert_eq!((8.0f32).sqrt(), MathUtils::distance_float(1.0f32, 2.0f32, 3.0f32, 4.0f32)); - assert_eq!(0.0f32, MathUtils::distance_float(1.0f32, 2.0f32, 1.0f32, 2.0f32)); + #[test] + fn testDistance() { + assert_eq!( + (8.0f32).sqrt(), + MathUtils::distance_float(1.0f32, 2.0f32, 3.0f32, 4.0f32) + ); + assert_eq!( + 0.0f32, + MathUtils::distance_float(1.0f32, 2.0f32, 1.0f32, 2.0f32) + ); - assert_eq!((8.0f32).sqrt(), MathUtils::distance_int(1, 2, 3, 4)); - assert_eq!(0.0f32, MathUtils::distance_int(1, 2, 1, 2)); - } + assert_eq!((8.0f32).sqrt(), MathUtils::distance_int(1, 2, 3, 4)); + assert_eq!(0.0f32, MathUtils::distance_int(1, 2, 1, 2)); + } - #[test] - fn testSum() { - assert_eq!(0, MathUtils::sum(&vec![])); - assert_eq!(1, MathUtils::sum(&[1])); - assert_eq!(4, MathUtils::sum(&[1, 3])); - assert_eq!(0, MathUtils::sum(&[-1, 1])); - } - } \ No newline at end of file + #[test] + fn testSum() { + assert_eq!(0, MathUtils::sum(&vec![])); + assert_eq!(1, MathUtils::sum(&[1])); + assert_eq!(4, MathUtils::sum(&[1, 3])); + assert_eq!(0, MathUtils::sum(&[-1, 1])); + } +} diff --git a/src/common/detector/MonochromeRectangleDetector.rs b/src/common/detector/MonochromeRectangleDetector.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/common/detector/WhiteRectangleDetector.rs b/src/common/detector/WhiteRectangleDetector.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/lib.rs b/src/lib.rs index 16e6669..b78e3d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1047,4 +1047,470 @@ impl fmt::Display for RXingResultPoint { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f,"({},{})", self.x, self.y) } -} \ No newline at end of file +} + + +/* + * Copyright 2012 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; + +/** + * Simply encapsulates a width and height. + */ +#[derive(Eq,PartialEq,Hash)] +pub struct Dimension { + + width : usize, + height :usize, +} + +impl Dimension { + pub fn new ( width:usize, height:usize) -> Result{ + if (width < 0 || height < 0) { + return Err( IllegalArgumentException::new()); + } + Ok(Self{ + width, + height, + }) + } + + pub fn getWidth(&self) -> usize { + return self.width; + } + + pub fn getHeight(&self) -> usize { + return self.height; + } + +} + +impl fmt::Display for Dimension { + + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f,"{}x{}", self.width, self.height) + } +} + + +/* + * Copyright 2009 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; +use crate::common::{BitArray,BitMatrix}; + +/** + * This class hierarchy provides a set of methods to convert luminance data to 1 bit data. + * It allows the algorithm to vary polymorphically, for example allowing a very expensive + * thresholding technique for servers and a fast one for mobile. It also permits the implementation + * to vary, e.g. a JNI version for Android and a Java fallback version for other platforms. + * + * @author dswitkin@google.com (Daniel Switkin) + */ +pub trait Binarizer { + + //private final LuminanceSource source; + fn new(source:LuminanceSource) -> Self; + + fn getLuminanceSource() -> LuminanceSource; + + /** + * Converts one row of luminance data to 1 bit data. May actually do the conversion, or return + * cached data. Callers should assume this method is expensive and call it as seldom as possible. + * This method is intended for decoding 1D barcodes and may choose to apply sharpening. + * For callers which only examine one row of pixels at a time, the same BitArray should be reused + * and passed in with each call for performance. However it is legal to keep more than one row + * at a time if needed. + * + * @param y The row to fetch, which must be in [0, bitmap height) + * @param row An optional preallocated array. If null or too small, it will be ignored. + * If used, the Binarizer will call BitArray.clear(). Always use the returned object. + * @return The array of bits for this row (true means black). + * @throws NotFoundException if row can't be binarized + */ + fn getBlackRow( y:usize, row:BitArray) -> Result; + + /** + * Converts a 2D array of luminance data to 1 bit data. As above, assume this method is expensive + * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or + * may not apply sharpening. Therefore, a row from this matrix may not be identical to one + * fetched using getBlackRow(), so don't mix and match between them. + * + * @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() -> Result; + + /** + * Creates a new object with the same type as this Binarizer implementation, but with pristine + * state. This is needed because Binarizer implementations may be stateful, e.g. keeping a cache + * of 1 bit data. See Effective Java for why we can't use Java's clone() method. + * + * @param source The LuminanceSource this Binarizer will operate on. + * @return A new concrete Binarizer implementation object. + */ + fn createBinarizer( source:LuminanceSource) -> dyn Binarizer; + + fn getWidth() -> usize; + + fn getHeight()->usize; + +} + + +/* + * Copyright 2009 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; + +/** + * This class is the core bitmap class used by ZXing to represent 1 bit data. Reader objects + * accept a BinaryBitmap and attempt to decode it. + * + * @author dswitkin@google.com (Daniel Switkin) + */ +pub struct BinaryBitmap { + + binarizer :dyn Binarizer, + matrix:BitMatrix, + +} + +impl BinaryBitmap{ + pub fn new( binarizer:dyn Binarizer) -> Self{ + Self{ + binarizer: binarizer, + matrix: binarizer.getBlackMatrix() + } + } + + /** + * @return The width of the bitmap. + */ + pub fn getWidth(&self) -> usize { + return self.binarizer.getWidth(); + } + + /** + * @return The height of the bitmap. + */ + pub fn getHeight(&self) -> usize{ + return self.binarizer.getHeight(); + } + + /** + * Converts one row of luminance data to 1 bit data. May actually do the conversion, or return + * cached data. Callers should assume this method is expensive and call it as seldom as possible. + * This method is intended for decoding 1D barcodes and may choose to apply sharpening. + * + * @param y The row to fetch, which must be in [0, bitmap height) + * @param row An optional preallocated array. If null or too small, it will be ignored. + * If used, the Binarizer will call BitArray.clear(). Always use the returned object. + * @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, row:BitArray) -> Result { + return self.binarizer.getBlackRow(y, row); + } + + /** + * Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive + * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or + * may not apply sharpening. Therefore, a row from this matrix may not be identical to one + * fetched using getBlackRow(), so don't mix and match between them. + * + * @return The 2D array of bits for the image (true means black). + * @throws NotFoundException if image can't be binarized to make a matrix + */ + pub fn getBlackMatrix(&self) -> Result { + // The matrix is created on demand the first time it is requested, then cached. There are two + // reasons for this: + // 1. This work will never be done if the caller only installs 1D Reader objects, or if a + // 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. + return self.matrix; + } + + /** + * @return Whether this bitmap can be cropped. + */ + pub fn isCropSupported(&self) -> bool{ + return self.binarizer.getLuminanceSource().isCropSupported(); + } + + /** + * Returns a new object with cropped image data. Implementations may keep a reference to the + * original data rather than a copy. Only callable if isCropSupported() is true. + * + * @param left The left coordinate, which must be in [0,getWidth()) + * @param top The top coordinate, which must be in [0,getHeight()) + * @param width The width of the rectangle to crop. + * @param height The height of the rectangle to crop. + * @return A cropped version of this object. + */ + pub fn crop(&self, left:usize, top:usize, width:usize, height:usize) ->BinaryBitmap{ + let newSource = self.binarizer.getLuminanceSource().crop(left, top, width, height); + return BinaryBitmap::new(self.binarizer.createBinarizer(newSource)); + } + + /** + * @return Whether this bitmap supports counter-clockwise rotation. + */ + pub fn isRotateSupported(&self) -> bool{ + return self.binarizer.getLuminanceSource().isRotateSupported(); + } + + /** + * Returns a new object with rotated image data by 90 degrees counterclockwise. + * Only callable if {@link #isRotateSupported()} is true. + * + * @return A rotated version of this object. + */ + pub fn rotateCounterClockwise(&self) -> BinaryBitmap{ + let newSource = self.binarizer.getLuminanceSource().rotateCounterClockwise(); + return BinaryBitmap::new(self.binarizer.createBinarizer(newSource)); + } + + /** + * Returns a new object with rotated image data by 45 degrees counterclockwise. + * Only callable if {@link #isRotateSupported()} is true. + * + * @return A rotated version of this object. + */ + pub fn rotateCounterClockwise45(&self) ->BinaryBitmap{ + let newSource = self.binarizer.getLuminanceSource().rotateCounterClockwise45(); + return BinaryBitmap::new(self.binarizer.createBinarizer(newSource)); + } + +} + +impl fmt::Display for BinaryBitmap { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f,"{}",self.getBlackMatrix()) + } +} + + +/* + * Copyright 2009 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; + +/** + * Callback which is invoked when a possible result point (significant + * point in the barcode image such as a corner) is found. + * + * @see DecodeHintType#NEED_RESULT_POINT_CALLBACK + */ +pub trait RXingResultPointCallback { + + fn foundPossibleRXingResultPoint( point: RXingResultPoint); + +} + + + +/* + * Copyright 2009 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; + +/** + * The purpose of this class hierarchy is to abstract different bitmap implementations across + * platforms into a standard interface for requesting greyscale luminance values. The interface + * only provides immutable methods; therefore crop and rotation create copies. This is to ensure + * that one Reader does not modify the original luminance source and leave it in an unknown state + * for other Readers in the chain. + * + * @author dswitkin@google.com (Daniel Switkin) + */ +pub trait LuminanceSource { + + //private final int width; + //private final int height; + + fn new( width:usize, height:usize) -> Self; + + /** + * Fetches one row of luminance data from the underlying platform's bitmap. Values range from + * 0 (black) to 255 (white). Because Java does not have an unsigned byte type, callers will have + * to bitwise and with 0xff for each value. It is preferable for implementations of this method + * to only fetch this row rather than the whole image, since no 2D Readers may be installed and + * getMatrix() may never be called. + * + * @param y The row to fetch, which must be in [0,getHeight()) + * @param row An optional preallocated array. If null or too small, it will be ignored. + * Always use the returned object, and ignore the .length of the array. + * @return An array containing the luminance data. + */ + fn getRow(&self, y:usize, row:&Vec) -> Vec; + + /** + * Fetches luminance data for the underlying bitmap. Values should be fetched using: + * {@code int luminance = array[y * width + x] & 0xff} + * + * @return A row-major 2D array of luminance values. Do not use result.length as it may be + * larger than width * height bytes on some platforms. Do not modify the contents + * of the result. + */ + fn getMatrix(&self) -> Vec; + + /** + * @return The width of the bitmap. + */ + fn getWidth(&self) -> usize; + + /** + * @return The height of the bitmap. + */ + fn getHeight(&self) -> usize; + + /** + * @return Whether this subclass supports cropping. + */ + fn isCropSupported(&self) -> bool { + return false; + } + + /** + * Returns a new object with cropped image data. Implementations may keep a reference to the + * original data rather than a copy. Only callable if isCropSupported() is true. + * + * @param left The left coordinate, which must be in [0,getWidth()) + * @param top The top coordinate, which must be in [0,getHeight()) + * @param width The width of the rectangle to crop. + * @param height The height of the rectangle to crop. + * @return A cropped version of this object. + */ + fn crop(&self, left:usize, top:usize, width:usize, height:usize) -> Result { + return Err( UnsupportedOperationException::new("This luminance source does not support cropping.")); + } + + /** + * @return Whether this subclass supports counter-clockwise rotation. + */ + fn isRotateSupported(&self) -> bool { + return false; + } + + /** + * @return a wrapper of this {@code LuminanceSource} which inverts the luminances it returns -- black becomes + * white and vice versa, and each value becomes (255-value). + */ + fn invert(&self) -> InvertedLuminanceSource{ + return InvertedLuminanceSource::new(self); + } + + /** + * Returns a new object with rotated image data by 90 degrees counterclockwise. + * Only callable if {@link #isRotateSupported()} is true. + * + * @return A rotated version of this object. + */ + fn rotateCounterClockwise(&self) -> Result { + return Err(UnsupportedOperationException::new("This luminance source does not support rotation by 90 degrees.")); + } + + /** + * Returns a new object with rotated image data by 45 degrees counterclockwise. + * Only callable if {@link #isRotateSupported()} is true. + * + * @return A rotated version of this object. + */ + fn rotateCounterClockwise45() -> Result { + return Err( UnsupportedOperationException::new("This luminance source does not support rotation by 45 degrees.")); + } + + /* + @Override + public final String toString() { + byte[] row = new byte[width]; + StringBuilder result = new StringBuilder(height * (width + 1)); + for (int y = 0; y < height; y++) { + row = getRow(y, row); + for (int x = 0; x < width; x++) { + int luminance = row[x] & 0xFF; + char c; + if (luminance < 0x40) { + c = '#'; + } else if (luminance < 0x80) { + c = '+'; + } else if (luminance < 0xC0) { + c = '.'; + } else { + c = ' '; + } + result.append(c); + } + result.append('\n'); + } + return result.toString(); + }*/ + +}