DecodeResult port

This commit is contained in:
Henry Schimke
2022-08-31 16:55:33 -05:00
parent 6063af0e93
commit 56559f4888
2 changed files with 203 additions and 176 deletions

View File

@@ -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;
/**
* <p>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.</p>
*
* @author Sean Owen
*/
public final class DecoderRXingResult {
private final byte[] rawBytes;
private int numBits;
private final String text;
private final List<byte[]> 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<byte[]> byteSegments,
String ecLevel) {
this(rawBytes, text, byteSegments, ecLevel, -1, -1, 0);
}
public DecoderRXingResult(byte[] rawBytes,
String text,
List<byte[]> byteSegments,
String ecLevel,
int symbologyModifier) {
this(rawBytes, text, byteSegments, ecLevel, -1, -1, symbologyModifier);
}
public DecoderRXingResult(byte[] rawBytes,
String text,
List<byte[]> byteSegments,
String ecLevel,
int saSequence,
int saParity) {
this(rawBytes, text, byteSegments, ecLevel, saSequence, saParity, 0);
}
public DecoderRXingResult(byte[] rawBytes,
String text,
List<byte[]> 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<byte[]> 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;
}
}

View File

@@ -2,6 +2,7 @@ pub mod detector;
pub mod reedsolomon; pub mod reedsolomon;
use core::num; use core::num;
use std::any::Any;
use std::cmp; use std::cmp;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; 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;
/**
* <p>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.</p>
*
* @author Sean Owen
*/
pub struct DecoderRXingResult {
rawBytes: Vec<u8>,
numBits: usize,
text: String,
byteSegments: Vec<u8>,
ecLevel: String,
errorsCorrected: u64,
erasures: u64,
other: Box<dyn Any>,
structuredAppendParity: i32,
structuredAppendSequenceNumber: i32,
symbologyModifier: u32,
}
impl DecoderRXingResult {
pub fn new(rawBytes: Vec<u8>, text: String, byteSegments: Vec<u8>, ecLevel: String) -> Self {
Self::with_all(rawBytes, text, byteSegments, ecLevel, -2, -2, 0)
}
pub fn with_symbology(
rawBytes: Vec<u8>,
text: String,
byteSegments: Vec<u8>,
ecLevel: String,
symbologyModifier: u32,
) -> Self {
Self::with_all(
rawBytes,
text,
byteSegments,
ecLevel,
-1,
-1,
symbologyModifier,
)
}
pub fn with_sa(
rawBytes: Vec<u8>,
text: String,
byteSegments: Vec<u8>,
ecLevel: String,
saSequence: i32,
saParity: i32,
) -> Self {
Self::with_all(
rawBytes,
text,
byteSegments,
ecLevel,
saSequence,
saParity,
0,
)
}
pub fn with_all(
rawBytes: Vec<u8>,
text: String,
byteSegments: Vec<u8>,
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<u8> {
&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<u8> {
&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<dyn Any> {
&self.other
}
pub fn setOther(&mut self, other: Box<dyn Any>) {
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
}
}