From 56559f48883f6ce500d67ad26f58095012bc302a Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Wed, 31 Aug 2022 16:55:33 -0500 Subject: [PATCH] DecodeResult port --- src/common/DecoderResult.java | 176 ----------------------------- src/common/mod.rs | 203 ++++++++++++++++++++++++++++++++++ 2 files changed, 203 insertions(+), 176 deletions(-) delete mode 100644 src/common/DecoderResult.java diff --git a/src/common/DecoderResult.java b/src/common/DecoderResult.java deleted file mode 100644 index 8dcd2bd..0000000 --- a/src/common/DecoderResult.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright 2007 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.common; - -import java.util.List; - -/** - *

Encapsulates the result of decoding a matrix of bits. This typically - * applies to 2D barcode formats. For now it contains the raw bytes obtained, - * as well as a String interpretation of those bytes, if applicable.

- * - * @author Sean Owen - */ -public final class DecoderRXingResult { - - private final byte[] rawBytes; - private int numBits; - private final String text; - private final List byteSegments; - private final String ecLevel; - private Integer errorsCorrected; - private Integer erasures; - private Object other; - private final int structuredAppendParity; - private final int structuredAppendSequenceNumber; - private final int symbologyModifier; - - public DecoderRXingResult(byte[] rawBytes, - String text, - List byteSegments, - String ecLevel) { - this(rawBytes, text, byteSegments, ecLevel, -1, -1, 0); - } - - public DecoderRXingResult(byte[] rawBytes, - String text, - List byteSegments, - String ecLevel, - int symbologyModifier) { - this(rawBytes, text, byteSegments, ecLevel, -1, -1, symbologyModifier); - } - - public DecoderRXingResult(byte[] rawBytes, - String text, - List byteSegments, - String ecLevel, - int saSequence, - int saParity) { - this(rawBytes, text, byteSegments, ecLevel, saSequence, saParity, 0); - } - - public DecoderRXingResult(byte[] rawBytes, - String text, - List byteSegments, - String ecLevel, - int saSequence, - int saParity, - int symbologyModifier) { - this.rawBytes = rawBytes; - this.numBits = rawBytes == null ? 0 : 8 * rawBytes.length; - this.text = text; - this.byteSegments = byteSegments; - this.ecLevel = ecLevel; - this.structuredAppendParity = saParity; - this.structuredAppendSequenceNumber = saSequence; - this.symbologyModifier = symbologyModifier; - } - - /** - * @return raw bytes representing the result, or {@code null} if not applicable - */ - public byte[] getRawBytes() { - return rawBytes; - } - - /** - * @return how many bits of {@link #getRawBytes()} are valid; typically 8 times its length - * @since 3.3.0 - */ - public int getNumBits() { - return numBits; - } - - /** - * @param numBits overrides the number of bits that are valid in {@link #getRawBytes()} - * @since 3.3.0 - */ - public void setNumBits(int numBits) { - this.numBits = numBits; - } - - /** - * @return text representation of the result - */ - public String getText() { - return text; - } - - /** - * @return list of byte segments in the result, or {@code null} if not applicable - */ - public List getByteSegments() { - return byteSegments; - } - - /** - * @return name of error correction level used, or {@code null} if not applicable - */ - public String getECLevel() { - return ecLevel; - } - - /** - * @return number of errors corrected, or {@code null} if not applicable - */ - public Integer getErrorsCorrected() { - return errorsCorrected; - } - - public void setErrorsCorrected(Integer errorsCorrected) { - this.errorsCorrected = errorsCorrected; - } - - /** - * @return number of erasures corrected, or {@code null} if not applicable - */ - public Integer getErasures() { - return erasures; - } - - public void setErasures(Integer erasures) { - this.erasures = erasures; - } - - /** - * @return arbitrary additional metadata - */ - public Object getOther() { - return other; - } - - public void setOther(Object other) { - this.other = other; - } - - public boolean hasStructuredAppend() { - return structuredAppendParity >= 0 && structuredAppendSequenceNumber >= 0; - } - - public int getStructuredAppendParity() { - return structuredAppendParity; - } - - public int getStructuredAppendSequenceNumber() { - return structuredAppendSequenceNumber; - } - - public int getSymbologyModifier() { - return symbologyModifier; - } - -} diff --git a/src/common/mod.rs b/src/common/mod.rs index 285c4ad..44ba495 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -2,6 +2,7 @@ pub mod detector; pub mod reedsolomon; use core::num; +use std::any::Any; use std::cmp; use std::collections::HashMap; use std::fmt; @@ -1796,3 +1797,205 @@ impl PerspectiveTransform { ); } } + +/* + * Copyright 2007 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.common; + +// import java.util.List; + +/** + *

Encapsulates the result of decoding a matrix of bits. This typically + * applies to 2D barcode formats. For now it contains the raw bytes obtained, + * as well as a String interpretation of those bytes, if applicable.

+ * + * @author Sean Owen + */ +pub struct DecoderRXingResult { + rawBytes: Vec, + numBits: usize, + text: String, + byteSegments: Vec, + ecLevel: String, + errorsCorrected: u64, + erasures: u64, + other: Box, + structuredAppendParity: i32, + structuredAppendSequenceNumber: i32, + symbologyModifier: u32, +} + +impl DecoderRXingResult { + pub fn new(rawBytes: Vec, text: String, byteSegments: Vec, ecLevel: String) -> Self { + Self::with_all(rawBytes, text, byteSegments, ecLevel, -2, -2, 0) + } + + pub fn with_symbology( + rawBytes: Vec, + text: String, + byteSegments: Vec, + ecLevel: String, + symbologyModifier: u32, + ) -> Self { + Self::with_all( + rawBytes, + text, + byteSegments, + ecLevel, + -1, + -1, + symbologyModifier, + ) + } + + pub fn with_sa( + rawBytes: Vec, + text: String, + byteSegments: Vec, + ecLevel: String, + saSequence: i32, + saParity: i32, + ) -> Self { + Self::with_all( + rawBytes, + text, + byteSegments, + ecLevel, + saSequence, + saParity, + 0, + ) + } + + pub fn with_all( + rawBytes: Vec, + text: String, + byteSegments: Vec, + ecLevel: String, + saSequence: i32, + saParity: i32, + symbologyModifier: u32, + ) -> Self { + let nb = rawBytes.len(); + Self { + rawBytes, + numBits: nb, + text, + byteSegments, + ecLevel, + errorsCorrected: 0, + erasures: 0, + other: Box::new(false), + structuredAppendParity: saParity, + structuredAppendSequenceNumber: saSequence, + symbologyModifier, + } + } + + /** + * @return raw bytes representing the result, or {@code null} if not applicable + */ + pub fn getRawBytes(&self) -> &Vec { + &self.rawBytes + } + + /** + * @return how many bits of {@link #getRawBytes()} are valid; typically 8 times its length + * @since 3.3.0 + */ + pub fn getNumBits(&self) -> usize { + self.numBits + } + + /** + * @param numBits overrides the number of bits that are valid in {@link #getRawBytes()} + * @since 3.3.0 + */ + pub fn setNumBits(&mut self, numBits: usize) { + self.numBits = numBits; + } + + /** + * @return text representation of the result + */ + pub fn getText(&self) -> &str { + &self.text + } + + /** + * @return list of byte segments in the result, or {@code null} if not applicable + */ + pub fn getByteSegments(&self) -> &Vec { + &self.byteSegments + } + + /** + * @return name of error correction level used, or {@code null} if not applicable + */ + pub fn getECLevel(&self) -> &str { + &self.ecLevel + } + + /** + * @return number of errors corrected, or {@code null} if not applicable + */ + pub fn getErrorsCorrected(&self) -> u64 { + self.errorsCorrected + } + + pub fn setErrorsCorrected(&mut self, errorsCorrected: u64) { + self.errorsCorrected = errorsCorrected; + } + + /** + * @return number of erasures corrected, or {@code null} if not applicable + */ + pub fn getErasures(&self) -> u64 { + self.erasures + } + + pub fn setErasures(&mut self, erasures: u64) { + self.erasures = erasures + } + + /** + * @return arbitrary additional metadata + */ + pub fn getOther(&self) -> &Box { + &self.other + } + + pub fn setOther(&mut self, other: Box) { + self.other = other + } + + pub fn hasStructuredAppend(&self) -> bool { + self.structuredAppendParity >= 0 && self.structuredAppendSequenceNumber >= 0 + } + + pub fn getStructuredAppendParity(&self) -> i32 { + self.structuredAppendParity + } + + pub fn getStructuredAppendSequenceNumber(&self) -> i32 { + self.structuredAppendSequenceNumber + } + + pub fn getSymbologyModifier(&self) -> u32 { + self.symbologyModifier + } +}