mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-27 21:02:35 +00:00
Implement clippy suggestions
This commit is contained in:
@@ -64,7 +64,7 @@ impl OneDReader for CodaBarReader {
|
||||
loop {
|
||||
let charOffset = self.toNarrowWidePattern(nextStart);
|
||||
if charOffset == -1 {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
// Hack: We store the position in the alphabet table into a
|
||||
// StringBuilder, so that we can access the decoded patterns in
|
||||
@@ -81,7 +81,7 @@ impl OneDReader for CodaBarReader {
|
||||
{
|
||||
break;
|
||||
}
|
||||
if !(nextStart < self.counterLength) {
|
||||
if nextStart >= self.counterLength {
|
||||
break;
|
||||
} // no fixed end pattern so keep on reading while data is available
|
||||
} //while (nextStart < counterLength); // no fixed end pattern so keep on reading while data is available
|
||||
@@ -98,7 +98,7 @@ impl OneDReader for CodaBarReader {
|
||||
// otherwise this is probably a false positive. The exception is if we are
|
||||
// at the end of the row. (I.e. the barcode barely fits.)
|
||||
if nextStart < self.counterLength && trailingWhitespace < lastPatternSize / 2 {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
self.validatePattern(startOffset)?;
|
||||
@@ -114,9 +114,9 @@ impl OneDReader for CodaBarReader {
|
||||
// self.decodeRowRXingResult.setCharAt(i, Self::ALPHABET[self.decodeRowRXingResult.chars().nth(i).unwrap() as usize]);
|
||||
}
|
||||
// Ensure a valid start and end character
|
||||
let startchar = self.decodeRowRXingResult.chars().nth(0).unwrap();
|
||||
let startchar = self.decodeRowRXingResult.chars().next().unwrap();
|
||||
if !Self::arrayContains(&Self::STARTEND_ENCODING, startchar) {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
let endchar = self
|
||||
.decodeRowRXingResult
|
||||
@@ -124,13 +124,13 @@ impl OneDReader for CodaBarReader {
|
||||
.nth(self.decodeRowRXingResult.chars().count() - 1)
|
||||
.unwrap();
|
||||
if !Self::arrayContains(&Self::STARTEND_ENCODING, endchar) {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
// remove stop/start characters character and check if a long enough string is contained
|
||||
if (self.decodeRowRXingResult.chars().count()) <= Self::MIN_CHARACTER_LENGTH as usize {
|
||||
// Almost surely a false positive ( start + stop + at least 1 character)
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
if !hints.contains_key(&DecodeHintType::RETURN_CODABAR_START_END) {
|
||||
@@ -231,7 +231,7 @@ impl CodaBarReader {
|
||||
// Even j = bars, while odd j = spaces. Categories 2 and 3 are for
|
||||
// long stripes, while 0 and 1 are for short stripes.
|
||||
let category = (j & 1) + ((pattern as usize) & 1) * 2;
|
||||
sizes[category] += self.counters[(pos + j) as usize];
|
||||
sizes[category] += self.counters[(pos + j)];
|
||||
counts[category] += 1;
|
||||
pattern >>= 1;
|
||||
}
|
||||
@@ -269,7 +269,7 @@ impl CodaBarReader {
|
||||
let category = (j & 1) + ((pattern as usize) & 1) * 2;
|
||||
let size = self.counters[(pos + j)];
|
||||
if (size as f32) < mins[category] || (size as f32) > maxes[category] {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
pattern >>= 1;
|
||||
}
|
||||
@@ -290,7 +290,7 @@ impl CodaBarReader {
|
||||
let mut i = row.getNextUnset(0);
|
||||
let end = row.getSize();
|
||||
if i >= end {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
let mut isWhite = true;
|
||||
let mut count = 0;
|
||||
@@ -344,7 +344,7 @@ impl CodaBarReader {
|
||||
|
||||
i += 2;
|
||||
}
|
||||
Err(Exceptions::NotFoundException("".to_owned()))
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
|
||||
pub fn arrayContains(array: &[char], key: char) -> bool {
|
||||
@@ -355,7 +355,7 @@ impl CodaBarReader {
|
||||
}
|
||||
}
|
||||
// }
|
||||
return false;
|
||||
false
|
||||
}
|
||||
|
||||
// Assumes that counters[position] is a bar.
|
||||
@@ -422,6 +422,6 @@ impl CodaBarReader {
|
||||
return i as i32;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
-1
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ const DEFAULT_GUARD: char = START_END_CHARS[0];
|
||||
*
|
||||
* @author dsbnatut@gmail.com (Kazuki Nishiura)
|
||||
*/
|
||||
#[derive(OneDWriter)]
|
||||
#[derive(OneDWriter, Default)]
|
||||
pub struct CodaBarWriter;
|
||||
|
||||
impl OneDimensionalCodeWriter for CodaBarWriter {
|
||||
@@ -40,7 +40,7 @@ impl OneDimensionalCodeWriter for CodaBarWriter {
|
||||
format!("{}{}{}", DEFAULT_GUARD, contents, DEFAULT_GUARD)
|
||||
} else {
|
||||
// Verify input and calculate decoded length.
|
||||
let firstChar = contents.chars().nth(0).unwrap().to_ascii_uppercase();
|
||||
let firstChar = contents.chars().next().unwrap().to_ascii_uppercase();
|
||||
let lastChar = contents
|
||||
.chars()
|
||||
.nth(contents.chars().count() - 1)
|
||||
@@ -52,29 +52,29 @@ impl OneDimensionalCodeWriter for CodaBarWriter {
|
||||
let endsAlt = CodaBarReader::arrayContains(&ALT_START_END_CHARS, lastChar);
|
||||
if startsNormal {
|
||||
if !endsNormal {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Invalid start/end guards: {}",
|
||||
contents
|
||||
)));
|
||||
))));
|
||||
}
|
||||
// else already has valid start/end
|
||||
contents.to_owned()
|
||||
} else if startsAlt {
|
||||
if !endsAlt {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Invalid start/end guards: {}",
|
||||
contents
|
||||
)));
|
||||
))));
|
||||
}
|
||||
// else already has valid start/end
|
||||
contents.to_owned()
|
||||
} else {
|
||||
// Doesn't start with a guard
|
||||
if endsNormal || endsAlt {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Invalid start/end guards: {}",
|
||||
contents
|
||||
)));
|
||||
))));
|
||||
}
|
||||
// else doesn't end with guard either, so add a default
|
||||
format!("{}{}{}", DEFAULT_GUARD, contents, DEFAULT_GUARD)
|
||||
@@ -86,7 +86,7 @@ impl OneDimensionalCodeWriter for CodaBarWriter {
|
||||
//for i in 1..contents.chars().count() {
|
||||
for ch in contents[1..contents.chars().count() - 1].chars() {
|
||||
// for (int i = 1; i < contents.length() - 1; i++) {
|
||||
if ch.is_digit(10) || ch == '-' || ch == '$' {
|
||||
if ch.is_ascii_digit() || ch == '-' || ch == '$' {
|
||||
resultLength += 9;
|
||||
} else if CodaBarReader::arrayContains(
|
||||
&CHARS_WHICH_ARE_TEN_LENGTH_EACH_AFTER_DECODED,
|
||||
@@ -94,10 +94,10 @@ impl OneDimensionalCodeWriter for CodaBarWriter {
|
||||
) {
|
||||
resultLength += 10;
|
||||
} else {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Cannot encode : '{}'",
|
||||
ch
|
||||
)));
|
||||
))));
|
||||
}
|
||||
}
|
||||
// A blank is placed between each character.
|
||||
@@ -155,12 +155,6 @@ impl OneDimensionalCodeWriter for CodaBarWriter {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for CodaBarWriter {
|
||||
fn default() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author dsbnatut@gmail.com (Kazuki Nishiura)
|
||||
* @author Sean Owen
|
||||
|
||||
@@ -25,7 +25,7 @@ use super::{one_d_reader, OneDReader};
|
||||
*
|
||||
* @author Sean Owen
|
||||
*/
|
||||
#[derive(OneDReader)]
|
||||
#[derive(OneDReader, Default)]
|
||||
pub struct Code128Reader;
|
||||
|
||||
impl OneDReader for Code128Reader {
|
||||
@@ -50,7 +50,7 @@ impl OneDReader for Code128Reader {
|
||||
CODE_START_A => CODE_CODE_A,
|
||||
CODE_START_B => CODE_CODE_B,
|
||||
CODE_START_C => CODE_CODE_C,
|
||||
_ => return Err(Exceptions::FormatException("".to_owned())),
|
||||
_ => return Err(Exceptions::FormatException(None)),
|
||||
};
|
||||
|
||||
let mut done = false;
|
||||
@@ -106,7 +106,7 @@ impl OneDReader for Code128Reader {
|
||||
// Take care of illegal start codes
|
||||
match code {
|
||||
CODE_START_A | CODE_START_B | CODE_START_C => {
|
||||
return Err(Exceptions::FormatException("".to_owned()))
|
||||
return Err(Exceptions::FormatException(None))
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -301,21 +301,21 @@ impl OneDReader for Code128Reader {
|
||||
row.getSize().min(nextStart + (nextStart - lastStart) / 2),
|
||||
false,
|
||||
)? {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
// Pull out from sum the value of the penultimate check code
|
||||
checksumTotal -= multiplier as usize * lastCode as usize;
|
||||
// lastCode is the checksum then:
|
||||
if (checksumTotal % 103) as u8 != lastCode {
|
||||
return Err(Exceptions::ChecksumException("".to_owned()));
|
||||
return Err(Exceptions::ChecksumException(None));
|
||||
}
|
||||
|
||||
// Need to pull out the check digits from string
|
||||
let resultLength = result.chars().count();
|
||||
if resultLength == 0 {
|
||||
// false positive
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
// Only bother if the result had at least one character, and if the checksum digit happened to
|
||||
@@ -340,9 +340,9 @@ impl OneDReader for Code128Reader {
|
||||
|
||||
let rawCodesSize = rawCodes.len();
|
||||
let mut rawBytes = vec![0u8; rawCodesSize];
|
||||
for i in 0..rawCodesSize {
|
||||
for (i, rawByte) in rawBytes.iter_mut().enumerate().take(rawCodesSize) {
|
||||
// for (int i = 0; i < rawCodesSize; i++) {
|
||||
rawBytes[i] = *rawCodes.get(i).unwrap();
|
||||
*rawByte = *rawCodes.get(i).unwrap();
|
||||
}
|
||||
let mut resultObject = RXingResult::new(
|
||||
&result,
|
||||
@@ -419,7 +419,7 @@ impl Code128Reader {
|
||||
}
|
||||
}
|
||||
|
||||
Err(Exceptions::NotFoundException("".to_owned()))
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
|
||||
fn decodeCode(
|
||||
@@ -435,7 +435,7 @@ impl Code128Reader {
|
||||
// for (int d = 0; d < CODE_PATTERNS.len(); d++) {
|
||||
let pattern = &CODE_PATTERNS[d];
|
||||
let variance =
|
||||
one_d_reader::patternMatchVariance(counters, &pattern, MAX_INDIVIDUAL_VARIANCE);
|
||||
one_d_reader::patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE);
|
||||
if variance < bestVariance {
|
||||
bestVariance = variance;
|
||||
bestMatch = d as isize;
|
||||
@@ -445,17 +445,11 @@ impl Code128Reader {
|
||||
if bestMatch >= 0 {
|
||||
Ok(bestMatch as u8)
|
||||
} else {
|
||||
Err(Exceptions::NotFoundException("".to_owned()))
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Code128Reader {
|
||||
fn default() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
lazy_static! {
|
||||
|
||||
@@ -43,10 +43,10 @@ const CODE_FNC_4_B: usize = 100; // Code B
|
||||
// RXingResults of minimal lookahead for code C
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum CType {
|
||||
UNCODABLE,
|
||||
ONE_DIGIT,
|
||||
TWO_DIGITS,
|
||||
FNC_1,
|
||||
Uncodable,
|
||||
OneDigit,
|
||||
TwoDigits,
|
||||
Fnc1,
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,13 +54,9 @@ enum CType {
|
||||
*
|
||||
* @author erik.barbara@gmail.com (Erik Barbara)
|
||||
*/
|
||||
#[derive(OneDWriter)]
|
||||
#[derive(OneDWriter, Default)]
|
||||
pub struct Code128Writer;
|
||||
impl Default for Code128Writer {
|
||||
fn default() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl OneDimensionalCodeWriter for Code128Writer {
|
||||
fn encode_oned(&self, contents: &str) -> Result<Vec<bool>, Exceptions> {
|
||||
self.encode_oned_with_hints(contents, &HashMap::new())
|
||||
@@ -98,11 +94,11 @@ impl OneDimensionalCodeWriter for Code128Writer {
|
||||
fn check(contents: &str, hints: &crate::EncodingHintDictionary) -> Result<i32, Exceptions> {
|
||||
let length = contents.chars().count();
|
||||
// Check length
|
||||
if length < 1 || length > 80 {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
if !(1..=80).contains(&length) {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Contents length should be between 1 and 80 characters, but got {}",
|
||||
length
|
||||
)));
|
||||
))));
|
||||
}
|
||||
|
||||
// Check for forced code set hint.
|
||||
@@ -114,10 +110,10 @@ fn check(contents: &str, hints: &crate::EncodingHintDictionary) -> Result<i32, E
|
||||
"B" => forcedCodeSet = CODE_CODE_B as i32,
|
||||
"C" => forcedCodeSet = CODE_CODE_C as i32,
|
||||
_ => {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Unsupported code set hint: {}",
|
||||
codeSetHint
|
||||
)))
|
||||
))))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -136,10 +132,10 @@ fn check(contents: &str, hints: &crate::EncodingHintDictionary) -> Result<i32, E
|
||||
if c > 127 {
|
||||
// no full Latin-1 character set available at the moment
|
||||
// shift and manual code change are not supported
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Bad character in input: ASCII value={}",
|
||||
c
|
||||
)));
|
||||
))));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -152,20 +148,20 @@ fn check(contents: &str, hints: &crate::EncodingHintDictionary) -> Result<i32, E
|
||||
// allows no ascii above 95 (no lower caps, no special symbols)
|
||||
{
|
||||
if c > 95 && c <= 127 {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Bad character in input for forced code set A: ASCII value={}",
|
||||
c
|
||||
)));
|
||||
))));
|
||||
}
|
||||
}
|
||||
CODE_CODE_B_I32 =>
|
||||
// allows no ascii below 32 (terminal symbols)
|
||||
{
|
||||
if c <= 32 {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Bad character in input for forced code set B: ASCII value={}",
|
||||
c
|
||||
)));
|
||||
))));
|
||||
}
|
||||
}
|
||||
CODE_CODE_C_I32 =>
|
||||
@@ -177,10 +173,10 @@ fn check(contents: &str, hints: &crate::EncodingHintDictionary) -> Result<i32, E
|
||||
|| ch == ESCAPE_FNC_3
|
||||
|| ch == ESCAPE_FNC_4
|
||||
{
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Bad character in input for forced code set C: ASCII value={}",
|
||||
c
|
||||
)));
|
||||
))));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
@@ -225,7 +221,7 @@ fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>, Exception
|
||||
_ =>
|
||||
// Then handle normal characters otherwise
|
||||
{
|
||||
match codeSet as usize {
|
||||
match codeSet {
|
||||
CODE_CODE_A => {
|
||||
patternIndex =
|
||||
contents.chars().nth(position).unwrap() as isize - ' ' as isize;
|
||||
@@ -242,9 +238,9 @@ fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>, Exception
|
||||
// CODE_CODE_C
|
||||
if position + 1 == length {
|
||||
// this is the last character, but the encoding is C, which always encodes two characers
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"Bad number of characters for digit only encoding.".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
let s: String = contents
|
||||
.char_indices()
|
||||
@@ -266,7 +262,7 @@ fn encodeFast(contents: &str, forcedCodeSet: i32) -> Result<Vec<bool>, Exception
|
||||
// Do we have a code set?
|
||||
if codeSet == 0 {
|
||||
// No, we don't have a code set
|
||||
match newCodeSet as usize {
|
||||
match newCodeSet {
|
||||
CODE_CODE_A => patternIndex = CODE_START_A as isize,
|
||||
CODE_CODE_B => patternIndex = CODE_START_B as isize,
|
||||
_ => patternIndex = CODE_START_C as isize,
|
||||
@@ -321,7 +317,7 @@ fn produceRXingResult(patterns: &mut Vec<Vec<usize>>, checkSum: usize) -> Vec<bo
|
||||
// for (int[] pattern : patterns) {
|
||||
for width in pattern {
|
||||
// for (int width : pattern) {
|
||||
codeWidth += *width as usize;
|
||||
codeWidth += *width;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -333,40 +329,40 @@ fn produceRXingResult(patterns: &mut Vec<Vec<usize>>, checkSum: usize) -> Vec<bo
|
||||
pos += Code128Writer::appendPattern(&mut result, pos, pattern, true) as usize;
|
||||
}
|
||||
|
||||
return result;
|
||||
result
|
||||
}
|
||||
|
||||
fn findCType(value: &str, start: usize) -> CType {
|
||||
let last = value.chars().count();
|
||||
if start >= last {
|
||||
return CType::UNCODABLE;
|
||||
return CType::Uncodable;
|
||||
}
|
||||
let c = value.chars().nth(start).unwrap();
|
||||
if c == ESCAPE_FNC_1 {
|
||||
return CType::FNC_1;
|
||||
return CType::Fnc1;
|
||||
}
|
||||
if c < '0' || c > '9' {
|
||||
return CType::UNCODABLE;
|
||||
if !('0'..='9').contains(&c) {
|
||||
return CType::Uncodable;
|
||||
}
|
||||
if start + 1 >= last {
|
||||
return CType::ONE_DIGIT;
|
||||
return CType::OneDigit;
|
||||
}
|
||||
let c = value.chars().nth(start + 1).unwrap();
|
||||
if c < '0' || c > '9' {
|
||||
return CType::ONE_DIGIT;
|
||||
if !('0'..='9').contains(&c) {
|
||||
return CType::OneDigit;
|
||||
}
|
||||
return CType::TWO_DIGITS;
|
||||
CType::TwoDigits
|
||||
}
|
||||
|
||||
fn chooseCode(value: &str, start: usize, oldCode: usize) -> usize {
|
||||
let mut lookahead = findCType(value, start);
|
||||
if lookahead == CType::ONE_DIGIT {
|
||||
if lookahead == CType::OneDigit {
|
||||
if oldCode == CODE_CODE_A {
|
||||
return CODE_CODE_A;
|
||||
}
|
||||
return CODE_CODE_B;
|
||||
}
|
||||
if lookahead == CType::UNCODABLE {
|
||||
if lookahead == CType::Uncodable {
|
||||
if start < value.chars().count() {
|
||||
let c = value.chars().nth(start).unwrap();
|
||||
if c < ' '
|
||||
@@ -378,7 +374,7 @@ fn chooseCode(value: &str, start: usize, oldCode: usize) -> usize {
|
||||
}
|
||||
return CODE_CODE_B; // no choice
|
||||
}
|
||||
if oldCode == CODE_CODE_A && lookahead == CType::FNC_1 {
|
||||
if oldCode == CODE_CODE_A && lookahead == CType::Fnc1 {
|
||||
return CODE_CODE_A;
|
||||
}
|
||||
if oldCode == CODE_CODE_C {
|
||||
@@ -386,18 +382,18 @@ fn chooseCode(value: &str, start: usize, oldCode: usize) -> usize {
|
||||
return CODE_CODE_C;
|
||||
}
|
||||
if oldCode == CODE_CODE_B {
|
||||
if lookahead == CType::FNC_1 {
|
||||
if lookahead == CType::Fnc1 {
|
||||
return CODE_CODE_B; // can continue in code B
|
||||
}
|
||||
// Seen two consecutive digits, see what follows
|
||||
lookahead = findCType(value, start + 2);
|
||||
if lookahead == CType::UNCODABLE || lookahead == CType::ONE_DIGIT {
|
||||
if lookahead == CType::Uncodable || lookahead == CType::OneDigit {
|
||||
return CODE_CODE_B; // not worth switching now
|
||||
}
|
||||
if lookahead == CType::FNC_1 {
|
||||
if lookahead == CType::Fnc1 {
|
||||
// two digits, then FNC_1...
|
||||
lookahead = findCType(value, start + 3);
|
||||
if lookahead == CType::TWO_DIGITS {
|
||||
if lookahead == CType::TwoDigits {
|
||||
// then two more digits, switch
|
||||
return CODE_CODE_C;
|
||||
} else {
|
||||
@@ -408,27 +404,27 @@ fn chooseCode(value: &str, start: usize, oldCode: usize) -> usize {
|
||||
// Look ahead to choose whether to switch now or on the next round.
|
||||
let mut index = start + 4;
|
||||
let mut lookahead = findCType(value, index);
|
||||
while lookahead == CType::TWO_DIGITS {
|
||||
while lookahead == CType::TwoDigits {
|
||||
// while (lookahead = findCType(value, index)) == CType::TWO_DIGITS {
|
||||
index += 2;
|
||||
lookahead = findCType(value, index);
|
||||
}
|
||||
if lookahead == CType::ONE_DIGIT {
|
||||
if lookahead == CType::OneDigit {
|
||||
// odd number of digits, switch later
|
||||
return CODE_CODE_B;
|
||||
}
|
||||
return CODE_CODE_C; // even number of digits, switch now
|
||||
}
|
||||
// Here oldCode == 0, which means we are choosing the initial code
|
||||
if lookahead == CType::FNC_1 {
|
||||
if lookahead == CType::Fnc1 {
|
||||
// ignore FNC_1
|
||||
lookahead = findCType(value, start + 1);
|
||||
}
|
||||
if lookahead == CType::TWO_DIGITS {
|
||||
if lookahead == CType::TwoDigits {
|
||||
// at least two digits, start in code C
|
||||
return CODE_CODE_C;
|
||||
}
|
||||
return CODE_CODE_B;
|
||||
CODE_CODE_B
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -452,15 +448,15 @@ mod MinimalEncoder {
|
||||
A,
|
||||
B,
|
||||
C,
|
||||
NONE,
|
||||
None,
|
||||
}
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum Latch {
|
||||
A,
|
||||
B,
|
||||
C,
|
||||
SHIFT,
|
||||
NONE,
|
||||
Shift,
|
||||
None,
|
||||
}
|
||||
|
||||
const A : &str = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_\u{0000}\u{0001}\u{0002}/
|
||||
@@ -476,14 +472,14 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|
||||
pub fn encode(contents: &str) -> Result<Vec<bool>, Exceptions> {
|
||||
let length = contents.chars().count();
|
||||
let mut memoizedCost = vec![vec![0_u32; length]; 4]; //new int[4][contents.length()];
|
||||
let mut minPath = vec![vec![Latch::NONE; length]; 4]; //new Latch[4][contents.length()];
|
||||
let mut minPath = vec![vec![Latch::None; length]; 4]; //new Latch[4][contents.length()];
|
||||
|
||||
encode_with_start_position(contents, Charset::NONE, 0, &mut memoizedCost, &mut minPath)?;
|
||||
encode_with_start_position(contents, Charset::None, 0, &mut memoizedCost, &mut minPath)?;
|
||||
|
||||
let mut patterns: Vec<Vec<usize>> = Vec::new(); //new ArrayList<>();
|
||||
let mut checkSum = vec![0_usize]; //new int[] {0};
|
||||
let mut checkWeight = vec![1]; //new int[] {1};
|
||||
let mut charset = Charset::NONE;
|
||||
let mut charset = Charset::None;
|
||||
let mut i = 0;
|
||||
while i < length {
|
||||
// for i in 0..length {
|
||||
@@ -520,14 +516,14 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|
||||
i,
|
||||
);
|
||||
}
|
||||
Latch::SHIFT => addPattern(
|
||||
Latch::Shift => addPattern(
|
||||
&mut patterns,
|
||||
CODE_SHIFT,
|
||||
&mut checkSum,
|
||||
&mut checkWeight,
|
||||
i,
|
||||
),
|
||||
Latch::NONE => { /* skip */ }
|
||||
Latch::None => { /* skip */ }
|
||||
}
|
||||
if charset == Charset::C {
|
||||
if contents.chars().nth(i).unwrap() == ESCAPE_FNC_1 {
|
||||
@@ -564,8 +560,8 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|
||||
ESCAPE_FNC_2 => CODE_FNC_2 as isize,
|
||||
ESCAPE_FNC_3 => CODE_FNC_3 as isize,
|
||||
ESCAPE_FNC_4 => {
|
||||
if (charset == Charset::A && latch != Latch::SHIFT)
|
||||
|| (charset == Charset::B && latch == Latch::SHIFT)
|
||||
if (charset == Charset::A && latch != Latch::Shift)
|
||||
|| (charset == Charset::B && latch == Latch::Shift)
|
||||
{
|
||||
CODE_FNC_4_A as isize
|
||||
} else {
|
||||
@@ -574,12 +570,11 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|
||||
}
|
||||
_ => contents.chars().nth(i).unwrap() as isize - ' ' as isize,
|
||||
};
|
||||
if (charset == Charset::A && latch != Latch::SHIFT)
|
||||
|| (charset == Charset::B && latch == Latch::SHIFT)
|
||||
if ((charset == Charset::A && latch != Latch::Shift)
|
||||
|| (charset == Charset::B && latch == Latch::Shift))
|
||||
&& patternIndex < 0
|
||||
{
|
||||
if patternIndex < 0 {
|
||||
patternIndex += '`' as isize;
|
||||
}
|
||||
patternIndex += '`' as isize;
|
||||
}
|
||||
addPattern(
|
||||
&mut patterns,
|
||||
@@ -618,7 +613,7 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|
||||
}
|
||||
|
||||
fn isDigit(c: char) -> bool {
|
||||
return c >= '0' && c <= '9';
|
||||
('0'..='9').contains(&c)
|
||||
}
|
||||
|
||||
fn canEncode(contents: &str, charset: Charset, position: usize) -> bool {
|
||||
@@ -665,7 +660,7 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|
||||
}
|
||||
|
||||
let mut minCost = u32::MAX;
|
||||
let mut minLatch = Latch::NONE;
|
||||
let mut minLatch = Latch::None;
|
||||
let atEnd = position + 1 >= contents.chars().count();
|
||||
|
||||
let sets = [Charset::A, Charset::B];
|
||||
@@ -673,7 +668,7 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|
||||
// for (int i = 0; i <= 1; i++) {
|
||||
if canEncode(contents, sets[i], position) {
|
||||
let mut cost = 1;
|
||||
let mut latch = Latch::NONE;
|
||||
let mut latch = Latch::None;
|
||||
if charset != sets[i] {
|
||||
cost += 1;
|
||||
latch = sets[i].into(); //Latch::valueOf(sets[i].toString());
|
||||
@@ -694,7 +689,7 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|
||||
cost = 1;
|
||||
if charset == sets[(i + 1) % 2] {
|
||||
cost += 1;
|
||||
latch = Latch::SHIFT;
|
||||
latch = Latch::Shift;
|
||||
if !atEnd {
|
||||
cost += encode_with_start_position(
|
||||
contents,
|
||||
@@ -713,7 +708,7 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|
||||
}
|
||||
if canEncode(contents, Charset::C, position) {
|
||||
let mut cost = 1;
|
||||
let mut latch = Latch::NONE;
|
||||
let mut latch = Latch::None;
|
||||
if charset != Charset::C {
|
||||
cost += 1;
|
||||
latch = Latch::C;
|
||||
@@ -738,10 +733,10 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|
||||
}
|
||||
}
|
||||
if minCost == u32::MAX {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Bad character in input: ASCII value={}",
|
||||
contents.chars().nth(position).unwrap_or('x')
|
||||
)));
|
||||
))));
|
||||
// throw new IllegalArgumentException("Bad character in input: ASCII value=" + (int) contents.charAt(position));
|
||||
}
|
||||
memoizedCost[charset.ordinal()][position] = minCost;
|
||||
@@ -759,7 +754,7 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|
||||
Charset::A => 0,
|
||||
Charset::B => 1,
|
||||
Charset::C => 2,
|
||||
Charset::NONE => 3,
|
||||
Charset::None => 3,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -769,8 +764,8 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|
||||
Latch::A => 0,
|
||||
Latch::B => 1,
|
||||
Latch::C => 2,
|
||||
Latch::SHIFT => 3,
|
||||
Latch::NONE => 4,
|
||||
Latch::Shift => 3,
|
||||
Latch::None => 4,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -780,7 +775,7 @@ stuvwxyz{|}~\u{007F}\u{00FF}";
|
||||
Charset::A => Latch::A,
|
||||
Charset::B => Latch::B,
|
||||
Charset::C => Latch::C,
|
||||
Charset::NONE => Latch::NONE,
|
||||
Charset::None => Latch::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ impl OneDReader for Code39Reader {
|
||||
one_d_reader::recordPattern(row, nextStart, &mut counters)?;
|
||||
let pattern = Self::toNarrowWidePattern(&counters);
|
||||
if pattern < 0 {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
decodedChar = Self::patternToChar(pattern as u32)?;
|
||||
self.decodeRowRXingResult.push(decodedChar);
|
||||
@@ -76,7 +76,7 @@ impl OneDReader for Code39Reader {
|
||||
// Read off white space
|
||||
nextStart = row.getNextSet(nextStart);
|
||||
|
||||
if !(decodedChar != '*') {
|
||||
if decodedChar == '*' {
|
||||
break;
|
||||
}
|
||||
} //(decodedChar != '*');
|
||||
@@ -93,7 +93,7 @@ impl OneDReader for Code39Reader {
|
||||
// If 50% of last pattern size, following last pattern, is not whitespace, fail
|
||||
// (but if it's whitespace to the very end of the image, that's OK)
|
||||
if nextStart != end && (whiteSpaceAfterEnd * 2) < lastPatternSize as usize {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
if self.usingCheckDigit {
|
||||
@@ -111,22 +111,21 @@ impl OneDReader for Code39Reader {
|
||||
if self.decodeRowRXingResult.chars().nth(max).unwrap()
|
||||
!= Self::ALPHABET_STRING.chars().nth(total % 43).unwrap()
|
||||
{
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
self.decodeRowRXingResult.truncate(max);
|
||||
}
|
||||
|
||||
if self.decodeRowRXingResult.chars().count() == 0 {
|
||||
// false positive
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
let resultString;
|
||||
if self.extendedMode {
|
||||
resultString = Self::decodeExtended(&self.decodeRowRXingResult)?;
|
||||
let resultString = if self.extendedMode {
|
||||
Self::decodeExtended(&self.decodeRowRXingResult)?
|
||||
} else {
|
||||
resultString = self.decodeRowRXingResult.clone();
|
||||
}
|
||||
self.decodeRowRXingResult.clone()
|
||||
};
|
||||
|
||||
let left = (start[1] + start[0]) as f32 / 2.0;
|
||||
let right = (lastStart + lastPatternSize as usize) as f32 / 2.0;
|
||||
@@ -247,7 +246,7 @@ impl Code39Reader {
|
||||
isWhite = !isWhite;
|
||||
}
|
||||
}
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
|
||||
// For efficiency, returns -1 on failure. Not throwing here saved as many as 700 exceptions
|
||||
@@ -268,13 +267,12 @@ impl Code39Reader {
|
||||
wideCounters = 0;
|
||||
let mut totalWideCountersWidth = 0;
|
||||
let mut pattern = 0;
|
||||
for i in 0..numCounters {
|
||||
for (i, counter) in counters.iter().enumerate().take(numCounters) {
|
||||
// for (int i = 0; i < numCounters; i++) {
|
||||
let counter = counters[i];
|
||||
if counter > maxNarrowCounter {
|
||||
if *counter > maxNarrowCounter {
|
||||
pattern |= 1 << (numCounters - 1 - i);
|
||||
wideCounters += 1;
|
||||
totalWideCountersWidth += counter;
|
||||
totalWideCountersWidth += *counter;
|
||||
}
|
||||
}
|
||||
if wideCounters == 3 {
|
||||
@@ -298,11 +296,11 @@ impl Code39Reader {
|
||||
return pattern;
|
||||
}
|
||||
|
||||
if !(wideCounters > 3) {
|
||||
if wideCounters <= 3 {
|
||||
break;
|
||||
}
|
||||
} //while ;
|
||||
return -1;
|
||||
-1
|
||||
}
|
||||
|
||||
fn patternToChar(pattern: u32) -> Result<char, Exceptions> {
|
||||
@@ -315,7 +313,7 @@ impl Code39Reader {
|
||||
if pattern == Self::ASTERISK_ENCODING {
|
||||
return Ok('*');
|
||||
}
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
|
||||
fn decodeExtended(encoded: &str) -> Result<String, Exceptions> {
|
||||
@@ -332,29 +330,29 @@ impl Code39Reader {
|
||||
match c {
|
||||
'+' => {
|
||||
// +A to +Z map to a to z
|
||||
if next >= 'A' && next <= 'Z' {
|
||||
if ('A'..='Z').contains(&next) {
|
||||
decodedChar = char::from_u32(next as u32 + 32).unwrap();
|
||||
} else {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
}
|
||||
'$' => {
|
||||
// $A to $Z map to control codes SH to SB
|
||||
if next >= 'A' && next <= 'Z' {
|
||||
if ('A'..='Z').contains(&next) {
|
||||
decodedChar = char::from_u32(next as u32 - 64).unwrap();
|
||||
} else {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
}
|
||||
'%' => {
|
||||
// %A to %E map to control codes ESC to US
|
||||
if next >= 'A' && next <= 'E' {
|
||||
if ('A'..='E').contains(&next) {
|
||||
decodedChar = char::from_u32(next as u32 - 38).unwrap();
|
||||
} else if next >= 'F' && next <= 'J' {
|
||||
} else if ('F'..='J').contains(&next) {
|
||||
decodedChar = char::from_u32(next as u32 - 11).unwrap();
|
||||
} else if next >= 'K' && next <= 'O' {
|
||||
} else if ('K'..='O').contains(&next) {
|
||||
decodedChar = char::from_u32(next as u32 + 16).unwrap();
|
||||
} else if next >= 'P' && next <= 'T' {
|
||||
} else if ('P'..='T').contains(&next) {
|
||||
decodedChar = char::from_u32(next as u32 + 43).unwrap();
|
||||
} else if next == 'U' {
|
||||
decodedChar = 0 as char;
|
||||
@@ -365,17 +363,17 @@ impl Code39Reader {
|
||||
} else if next == 'X' || next == 'Y' || next == 'Z' {
|
||||
decodedChar = 127 as char;
|
||||
} else {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
}
|
||||
'/' => {
|
||||
// /A to /O map to ! to , and /Z maps to :
|
||||
if next >= 'A' && next <= 'O' {
|
||||
if ('A'..='O').contains(&next) {
|
||||
decodedChar = char::from_u32(next as u32 - 32).unwrap();
|
||||
} else if next == 'Z' {
|
||||
decodedChar = ':';
|
||||
} else {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
|
||||
@@ -25,33 +25,32 @@ use super::{Code39Reader, OneDimensionalCodeWriter};
|
||||
*
|
||||
* @author erik.barbara@gmail.com (Erik Barbara)
|
||||
*/
|
||||
#[derive(OneDWriter)]
|
||||
#[derive(OneDWriter, Default)]
|
||||
pub struct Code39Writer;
|
||||
|
||||
impl Default for Code39Writer {
|
||||
fn default() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl OneDimensionalCodeWriter for Code39Writer {
|
||||
fn encode_oned(&self, contents: &str) -> Result<Vec<bool>, Exceptions> {
|
||||
let mut contents = contents.to_owned();
|
||||
let mut length = contents.chars().count();
|
||||
if length > 80 {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Requested contents should be less than 80 digits long, but got {}",
|
||||
length
|
||||
)));
|
||||
))));
|
||||
}
|
||||
|
||||
for i in 0..length {
|
||||
let mut i = 0;
|
||||
while i < length {
|
||||
// for i in 0..length {
|
||||
// for (int i = 0; i < length; i++) {
|
||||
if let None = Code39Reader::ALPHABET_STRING.find(contents.chars().nth(i).unwrap()) {
|
||||
if Code39Reader::ALPHABET_STRING
|
||||
.find(contents.chars().nth(i).unwrap())
|
||||
.is_none()
|
||||
{
|
||||
contents = Self::tryToConvertToExtendedMode(&contents)?;
|
||||
length = contents.chars().count();
|
||||
if length > 80 {
|
||||
return Err(Exceptions::IllegalArgumentException(format!("Requested contents should be less than 80 digits long, but got {} (extended full ASCII mode)",length)));
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!("Requested contents should be less than 80 digits long, but got {} (extended full ASCII mode)",length))));
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -64,6 +63,7 @@ impl OneDimensionalCodeWriter for Code39Writer {
|
||||
// }
|
||||
// break;
|
||||
// }
|
||||
i += 1;
|
||||
}
|
||||
|
||||
let mut widths = [0_usize; 9]; //new int[9];
|
||||
@@ -99,10 +99,10 @@ impl OneDimensionalCodeWriter for Code39Writer {
|
||||
}
|
||||
impl Code39Writer {
|
||||
fn toIntArray(a: u32, toReturn: &mut [usize; 9]) {
|
||||
for i in 0..9 {
|
||||
for (i, val) in toReturn.iter_mut().enumerate().take(9) {
|
||||
// for (int i = 0; i < 9; i++) {
|
||||
let temp = a & (1 << (8 - i));
|
||||
toReturn[i] = if temp == 0 { 1 } else { 2 };
|
||||
*val = if temp == 0 { 1 } else { 2 };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,10 +155,10 @@ impl Code39Writer {
|
||||
extendedContent
|
||||
.push(char::from_u32('P' as u32 + (character as u32 - 123)).unwrap());
|
||||
} else {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Requested content contains a non-encodable character: '{}'",
|
||||
character
|
||||
)));
|
||||
))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ impl OneDReader for Code93Reader {
|
||||
one_d_reader::recordPattern(row, nextStart, &mut theCounters)?;
|
||||
let pattern = Self::toPattern(&theCounters);
|
||||
if pattern < 0 {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
decodedChar = Self::patternToChar(pattern as u32)?;
|
||||
self.decodeRowRXingResult.push(decodedChar);
|
||||
@@ -75,7 +75,7 @@ impl OneDReader for Code93Reader {
|
||||
// Read off white space
|
||||
nextStart = row.getNextSet(nextStart);
|
||||
|
||||
if !(decodedChar != '*') {
|
||||
if decodedChar == '*' {
|
||||
break;
|
||||
}
|
||||
} //while (decodedChar != '*');
|
||||
@@ -92,12 +92,12 @@ impl OneDReader for Code93Reader {
|
||||
|
||||
// Should be at least one more black module
|
||||
if nextStart == end || !row.get(nextStart) {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
if self.decodeRowRXingResult.chars().count() < 2 {
|
||||
// false positive -- need at least 2 checksum digits
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
Self::checkChecksums(&self.decodeRowRXingResult)?;
|
||||
@@ -193,7 +193,7 @@ impl Code93Reader {
|
||||
isWhite = !isWhite;
|
||||
}
|
||||
}
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
|
||||
fn toPattern(counters: &[u32; 6]) -> i32 {
|
||||
@@ -204,11 +204,11 @@ impl Code93Reader {
|
||||
}
|
||||
let mut pattern = 0;
|
||||
let max = counters.len();
|
||||
for i in 0..max {
|
||||
for (i, counter) in counters.iter().enumerate().take(max) {
|
||||
// for (int i = 0; i < max; i++) {
|
||||
let scaled = (counters[i] as f32 * 9.0 / sum as f32).round() as u32;
|
||||
let scaled = (*counter as f32 * 9.0 / sum as f32).round() as u32;
|
||||
// let scaled = Math.round(counters[i] * 9.0 / sum);
|
||||
if scaled < 1 || scaled > 4 {
|
||||
if !(1..=4).contains(&scaled) {
|
||||
return -1;
|
||||
}
|
||||
if (i & 0x01) == 0 {
|
||||
@@ -220,7 +220,7 @@ impl Code93Reader {
|
||||
pattern <<= scaled;
|
||||
}
|
||||
}
|
||||
return pattern;
|
||||
pattern
|
||||
}
|
||||
|
||||
fn patternToChar(pattern: u32) -> Result<char, Exceptions> {
|
||||
@@ -230,7 +230,7 @@ impl Code93Reader {
|
||||
return Ok(Self::ALPHABET[i]);
|
||||
}
|
||||
}
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
|
||||
fn decodeExtended(encoded: &str) -> Result<String, Exceptions> {
|
||||
@@ -241,40 +241,40 @@ impl Code93Reader {
|
||||
// for i in 0..length {
|
||||
// for (int i = 0; i < length; i++) {
|
||||
let c = encoded.chars().nth(i).unwrap();
|
||||
if c >= 'a' && c <= 'd' {
|
||||
if ('a'..='d').contains(&c) {
|
||||
if i >= length - 1 {
|
||||
return Err(Exceptions::FormatException("".to_owned()));
|
||||
return Err(Exceptions::FormatException(None));
|
||||
}
|
||||
let next = encoded.chars().nth(i + 1).unwrap();
|
||||
let mut decodedChar = '\0';
|
||||
match c {
|
||||
'd' => {
|
||||
// +A to +Z map to a to z
|
||||
if next >= 'A' && next <= 'Z' {
|
||||
if ('A'..='Z').contains(&next) {
|
||||
decodedChar = char::from_u32(next as u32 + 32).unwrap();
|
||||
} else {
|
||||
return Err(Exceptions::FormatException("".to_owned()));
|
||||
return Err(Exceptions::FormatException(None));
|
||||
}
|
||||
}
|
||||
'a' => {
|
||||
// $A to $Z map to control codes SH to SB
|
||||
if next >= 'A' && next <= 'Z' {
|
||||
if ('A'..='Z').contains(&next) {
|
||||
decodedChar = char::from_u32(next as u32 - 64).unwrap();
|
||||
} else {
|
||||
return Err(Exceptions::FormatException("".to_owned()));
|
||||
return Err(Exceptions::FormatException(None));
|
||||
}
|
||||
}
|
||||
'b' => {
|
||||
if next >= 'A' && next <= 'E' {
|
||||
if ('A'..='E').contains(&next) {
|
||||
// %A to %E map to control codes ESC to USep
|
||||
decodedChar = char::from_u32(next as u32 - 38).unwrap();
|
||||
} else if next >= 'F' && next <= 'J' {
|
||||
} else if ('F'..='J').contains(&next) {
|
||||
// %F to %J map to ; < = > ?
|
||||
decodedChar = char::from_u32(next as u32 - 11).unwrap();
|
||||
} else if next >= 'K' && next <= 'O' {
|
||||
} else if ('K'..='O').contains(&next) {
|
||||
// %K to %O map to [ \ ] ^ _
|
||||
decodedChar = char::from_u32(next as u32 + 16).unwrap();
|
||||
} else if next >= 'P' && next <= 'T' {
|
||||
} else if ('P'..='T').contains(&next) {
|
||||
// %P to %T map to { | } ~ DEL
|
||||
decodedChar = char::from_u32(next as u32 + 43).unwrap();
|
||||
} else if next == 'U' {
|
||||
@@ -286,21 +286,21 @@ impl Code93Reader {
|
||||
} else if next == 'W' {
|
||||
// %W map to `
|
||||
decodedChar = '`';
|
||||
} else if next >= 'X' && next <= 'Z' {
|
||||
} else if ('X'..='Z').contains(&next) {
|
||||
// %X to %Z all map to DEL (127)
|
||||
decodedChar = 127 as char;
|
||||
} else {
|
||||
return Err(Exceptions::FormatException("".to_owned()));
|
||||
return Err(Exceptions::FormatException(None));
|
||||
}
|
||||
}
|
||||
'c' => {
|
||||
// /A to /O map to ! to , and /Z maps to :
|
||||
if next >= 'A' && next <= 'O' {
|
||||
if ('A'..='O').contains(&next) {
|
||||
decodedChar = char::from_u32(next as u32 - 32).unwrap();
|
||||
} else if next == 'Z' {
|
||||
decodedChar = ':';
|
||||
} else {
|
||||
return Err(Exceptions::FormatException("".to_owned()));
|
||||
return Err(Exceptions::FormatException(None));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
@@ -345,7 +345,7 @@ impl Code93Reader {
|
||||
}
|
||||
}
|
||||
if result.chars().nth(checkPosition).unwrap() != Self::ALPHABET[(total as usize) % 47] {
|
||||
Err(Exceptions::ChecksumException("".to_owned()))
|
||||
Err(Exceptions::ChecksumException(None))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -23,15 +23,9 @@ use super::{Code93Reader, OneDimensionalCodeWriter};
|
||||
/**
|
||||
* This object renders a CODE93 code as a BitMatrix
|
||||
*/
|
||||
#[derive(OneDWriter)]
|
||||
#[derive(OneDWriter, Default)]
|
||||
pub struct Code93Writer;
|
||||
|
||||
impl Default for Code93Writer {
|
||||
fn default() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl OneDimensionalCodeWriter for Code93Writer {
|
||||
/**
|
||||
* @param contents barcode contents to encode. It should not be encoded for extended characters.
|
||||
@@ -41,7 +35,7 @@ impl OneDimensionalCodeWriter for Code93Writer {
|
||||
let mut contents = Self::convertToExtended(contents)?;
|
||||
let length = contents.chars().count();
|
||||
if length > 80 {
|
||||
return Err(Exceptions::IllegalArgumentException(format!("Requested contents should be less than 80 digits long after converting to extended encoding, but got {}" , length)));
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!("Requested contents should be less than 80 digits long after converting to extended encoding, but got {}" , length))));
|
||||
}
|
||||
|
||||
//length of code + 2 start/stop characters + 2 checksums, each of 9 bits, plus a termination bar
|
||||
@@ -202,10 +196,10 @@ impl Code93Writer {
|
||||
extendedContent
|
||||
.push(char::from_u32('P' as u32 + character as u32 - '{' as u32).unwrap());
|
||||
} else {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Requested content contains a non-encodable character: '{}'",
|
||||
character
|
||||
)));
|
||||
))));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ use crate::Exceptions;
|
||||
* @author Sean Owen
|
||||
* @author alasdair@google.com (Alasdair Mackintosh)
|
||||
*/
|
||||
#[derive(OneDReader, EANReader)]
|
||||
#[derive(OneDReader, EANReader, Default)]
|
||||
pub struct EAN13Reader;
|
||||
impl UPCEANReader for EAN13Reader {
|
||||
fn getBarcodeFormat(&self) -> crate::BarcodeFormat {
|
||||
@@ -154,12 +154,6 @@ impl EAN13Reader {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for EAN13Reader {
|
||||
fn default() -> Self {
|
||||
Self {}
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ use super::{OneDimensionalCodeWriter, UPCEANReader, UPCEANWriter};
|
||||
*
|
||||
* @author aripollak@gmail.com (Ari Pollak)
|
||||
*/
|
||||
#[derive(OneDWriter)]
|
||||
#[derive(OneDWriter, Default)]
|
||||
pub struct EAN13Writer;
|
||||
impl UPCEANWriter for EAN13Writer {}
|
||||
|
||||
@@ -40,9 +40,9 @@ impl OneDimensionalCodeWriter for EAN13Writer {
|
||||
match length {
|
||||
12 => {
|
||||
// No check digit present, calculate it and add it
|
||||
let check;
|
||||
|
||||
// try {
|
||||
check = reader.getStandardUPCEANChecksum(&contents)?;
|
||||
let check = reader.getStandardUPCEANChecksum(&contents)?;
|
||||
// } catch (FormatException fe) {
|
||||
// throw new IllegalArgumentException(fe);
|
||||
// }
|
||||
@@ -51,25 +51,25 @@ impl OneDimensionalCodeWriter for EAN13Writer {
|
||||
13 => {
|
||||
//try {
|
||||
if !reader.checkStandardUPCEANChecksum(&contents)? {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"Contents do not pass checksum".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
//} catch (FormatException ignored) {
|
||||
//return Err( Exceptions::IllegalArgumentException("Illegal contents".to_owned()));
|
||||
//}
|
||||
}
|
||||
_ => {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Requested contents should be 12 or 13 digits long, but got {}",
|
||||
length
|
||||
)))
|
||||
))))
|
||||
}
|
||||
}
|
||||
|
||||
EAN13Writer::checkNumeric(&contents)?;
|
||||
|
||||
let firstDigit = contents.chars().nth(0).unwrap().to_digit(10).unwrap() as usize; //, 10);
|
||||
let firstDigit = contents.chars().next().unwrap().to_digit(10).unwrap() as usize; //, 10);
|
||||
let parities = EAN13Reader::FIRST_DIGIT_ENCODINGS[firstDigit];
|
||||
let mut result = [false; CODE_WIDTH];
|
||||
let mut pos = 0;
|
||||
@@ -123,11 +123,6 @@ impl OneDimensionalCodeWriter for EAN13Writer {
|
||||
Self::DEFAULT_MARGIN
|
||||
}
|
||||
}
|
||||
impl Default for EAN13Writer {
|
||||
fn default() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
const CODE_WIDTH: usize = 3 + // start guard
|
||||
(7 * 6) + // left bars
|
||||
|
||||
@@ -26,7 +26,7 @@ use super::UPCEANReader;
|
||||
*
|
||||
* @author Sean Owen
|
||||
*/
|
||||
#[derive(OneDReader, EANReader)]
|
||||
#[derive(OneDReader, EANReader, Default)]
|
||||
pub struct EAN8Reader;
|
||||
|
||||
impl UPCEANReader for EAN8Reader {
|
||||
@@ -83,9 +83,3 @@ impl UPCEANReader for EAN8Reader {
|
||||
Ok(rowOffset)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for EAN8Reader {
|
||||
fn default() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ use super::{upc_ean_reader, OneDimensionalCodeWriter, UPCEANWriter};
|
||||
*
|
||||
* @author aripollak@gmail.com (Ari Pollak)
|
||||
*/
|
||||
#[derive(OneDWriter)]
|
||||
#[derive(OneDWriter, Default)]
|
||||
pub struct EAN8Writer;
|
||||
|
||||
const CODE_WIDTH: usize = 3 + // start guard
|
||||
@@ -37,11 +37,6 @@ const CODE_WIDTH: usize = 3 + // start guard
|
||||
(7 * 4) + // right bars
|
||||
3; // end guard
|
||||
|
||||
impl Default for EAN8Writer {
|
||||
fn default() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
impl UPCEANWriter for EAN8Writer {}
|
||||
impl OneDimensionalCodeWriter for EAN8Writer {
|
||||
/**
|
||||
@@ -66,19 +61,19 @@ impl OneDimensionalCodeWriter for EAN8Writer {
|
||||
// try {
|
||||
{
|
||||
if !EAN8Reader.checkStandardUPCEANChecksum(&contents)? {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"Contents do not pass checksum".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
// } catch (FormatException ignored) {
|
||||
// throw new IllegalArgumentException("Illegal contents");
|
||||
// }},
|
||||
_ => {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Requested contents should be 7 or 8 digits long, but got {}",
|
||||
length
|
||||
)))
|
||||
))))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -151,7 +151,7 @@ impl OneDReader for ITFReader {
|
||||
lengthOK = true;
|
||||
}
|
||||
if !lengthOK {
|
||||
return Err(Exceptions::FormatException("".to_owned()));
|
||||
return Err(Exceptions::FormatException(None));
|
||||
}
|
||||
|
||||
let mut resultObject = RXingResult::new(
|
||||
@@ -277,7 +277,7 @@ impl ITFReader {
|
||||
|
||||
if quietCount != 0 {
|
||||
// Unable to find the necessary number of quiet zone pixels.
|
||||
Err(Exceptions::NotFoundException("".to_owned()))
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
@@ -294,7 +294,7 @@ impl ITFReader {
|
||||
let width = row.getSize();
|
||||
let endStart = row.getNextSet(0);
|
||||
if endStart == width {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
Ok(endStart)
|
||||
@@ -397,7 +397,7 @@ impl ITFReader {
|
||||
isWhite = !isWhite;
|
||||
}
|
||||
}
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -412,9 +412,8 @@ impl ITFReader {
|
||||
let mut bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
|
||||
let mut bestMatch = -1_isize;
|
||||
let max = PATTERNS.len();
|
||||
for i in 0..max {
|
||||
for (i, pattern) in PATTERNS.iter().enumerate().take(max) {
|
||||
// for (int i = 0; i < max; i++) {
|
||||
let pattern = &PATTERNS[i];
|
||||
let variance =
|
||||
one_d_reader::patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE);
|
||||
if variance < bestVariance {
|
||||
@@ -428,7 +427,7 @@ impl ITFReader {
|
||||
if bestMatch >= 0 {
|
||||
Ok(bestMatch as u32 % 10)
|
||||
} else {
|
||||
Err(Exceptions::NotFoundException("".to_owned()))
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,28 +25,22 @@ use super::OneDimensionalCodeWriter;
|
||||
*
|
||||
* @author erik.barbara@gmail.com (Erik Barbara)
|
||||
*/
|
||||
#[derive(OneDWriter)]
|
||||
#[derive(OneDWriter, Default)]
|
||||
pub struct ITFWriter;
|
||||
|
||||
impl Default for ITFWriter {
|
||||
fn default() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl OneDimensionalCodeWriter for ITFWriter {
|
||||
fn encode_oned(&self, contents: &str) -> Result<Vec<bool>, Exceptions> {
|
||||
let length = contents.chars().count();
|
||||
if length % 2 != 0 {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"The length of the input should be even".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
if length > 80 {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Requested contents should be less than 80 digits long, but got {}",
|
||||
length
|
||||
)));
|
||||
))));
|
||||
}
|
||||
|
||||
Self::checkNumeric(contents)?;
|
||||
|
||||
@@ -51,7 +51,7 @@ impl OneDReader for MultiFormatOneDReader {
|
||||
// }
|
||||
}
|
||||
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
}
|
||||
impl MultiFormatOneDReader {
|
||||
@@ -111,12 +111,10 @@ impl MultiFormatOneDReader {
|
||||
}
|
||||
}
|
||||
|
||||
use crate::result_point::ResultPoint;
|
||||
use crate::DecodeHintType;
|
||||
use crate::DecodingHintDictionary;
|
||||
use crate::RXingResultMetadataType;
|
||||
use crate::RXingResultMetadataValue;
|
||||
use crate::RXingResultPoint;
|
||||
use crate::Reader;
|
||||
use std::collections::HashMap;
|
||||
|
||||
@@ -167,18 +165,21 @@ impl Reader for MultiFormatOneDReader {
|
||||
// for point in result.getRXingResultPointsMut().iter_mut() {
|
||||
let total_points = result.getRXingResultPoints().len();
|
||||
let points = result.getRXingResultPointsMut();
|
||||
for i in 0..total_points {
|
||||
for point in points.iter_mut().take(total_points) {
|
||||
// for i in 0..total_points {
|
||||
// for (int i = 0; i < points.length; i++) {
|
||||
points[i] = RXingResultPoint::new(
|
||||
height as f32 - points[i].getY() - 1.0,
|
||||
points[i].getX(),
|
||||
);
|
||||
std::mem::swap(&mut point.x, &mut point.y);
|
||||
point.x = height as f32 - point.x - 1.0;
|
||||
// points[i] = RXingResultPoint::new(
|
||||
// height as f32 - points[i].getY() - 1.0,
|
||||
// points[i].getX(),
|
||||
// );
|
||||
}
|
||||
// }
|
||||
|
||||
Ok(result)
|
||||
} else {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ impl MultiFormatUPCEANReader {
|
||||
//
|
||||
// But, don't return UPC-A if UPC-A was not a requested format!
|
||||
let ean13MayBeUPCA = result.getBarcodeFormat() == &BarcodeFormat::EAN_13
|
||||
&& result.getText().chars().nth(0).unwrap() == '0';
|
||||
&& result.getText().starts_with('0');
|
||||
|
||||
let canReturnUPCA = if let Some(DecodeHintValue::PossibleFormats(possibleFormats)) =
|
||||
hints.get(&DecodeHintType::POSSIBLE_FORMATS)
|
||||
@@ -134,16 +134,14 @@ impl OneDReader for MultiFormatUPCEANReader {
|
||||
}
|
||||
}
|
||||
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
}
|
||||
|
||||
use crate::result_point::ResultPoint;
|
||||
use crate::DecodeHintType;
|
||||
use crate::DecodingHintDictionary;
|
||||
use crate::RXingResultMetadataType;
|
||||
use crate::RXingResultMetadataValue;
|
||||
use crate::RXingResultPoint;
|
||||
use std::collections::HashMap;
|
||||
|
||||
impl Reader for MultiFormatUPCEANReader {
|
||||
@@ -193,18 +191,21 @@ impl Reader for MultiFormatUPCEANReader {
|
||||
// for point in result.getRXingResultPointsMut().iter_mut() {
|
||||
let total_points = result.getRXingResultPoints().len();
|
||||
let points = result.getRXingResultPointsMut();
|
||||
for i in 0..total_points {
|
||||
for point in points.iter_mut().take(total_points) {
|
||||
// for i in 0..total_points {
|
||||
// for (int i = 0; i < points.length; i++) {
|
||||
points[i] = RXingResultPoint::new(
|
||||
height as f32 - points[i].getY() - 1.0,
|
||||
points[i].getX(),
|
||||
);
|
||||
std::mem::swap(&mut point.x, &mut point.y);
|
||||
point.x = height as f32 - point.x - 1.0;
|
||||
// points[i] = RXingResultPoint::new(
|
||||
// height as f32 - points[i].getY() - 1.0,
|
||||
// points[i].getX(),
|
||||
// );
|
||||
}
|
||||
// }
|
||||
|
||||
Ok(result)
|
||||
} else {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,9 +103,9 @@ pub trait OneDimensionalCodeWriter: Writer {
|
||||
*/
|
||||
fn checkNumeric(contents: &str) -> Result<(), Exceptions> {
|
||||
if !NUMERIC.is_match(contents) {
|
||||
Err(Exceptions::IllegalArgumentException(
|
||||
Err(Exceptions::IllegalArgumentException(Some(
|
||||
"Input should only contain digits 0-9".to_owned(),
|
||||
))
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
@@ -168,23 +168,23 @@ impl Writer for L {
|
||||
hints: &crate::EncodingHintDictionary,
|
||||
) -> Result<crate::common::BitMatrix, crate::Exceptions> {
|
||||
if contents.is_empty() {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"Found empty contents".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
if width < 0 || height < 0 {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Negative size is not allowed. Input: {}x{}",
|
||||
width, height
|
||||
)));
|
||||
))));
|
||||
}
|
||||
if let Some(supportedFormats) = self.getSupportedWriteFormats() {
|
||||
if !supportedFormats.contains(format) {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Can only encode {:?}, but got {:?}",
|
||||
supportedFormats, format
|
||||
)));
|
||||
))));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -54,12 +54,11 @@ pub trait OneDReader: Reader {
|
||||
|
||||
let tryHarder = hints.contains_key(&DecodeHintType::TRY_HARDER);
|
||||
let rowStep = 1.max(height >> (if tryHarder { 8 } else { 5 }));
|
||||
let maxLines;
|
||||
if tryHarder {
|
||||
maxLines = height; // Look at the whole image, not just the center
|
||||
let maxLines = if tryHarder {
|
||||
height // Look at the whole image, not just the center
|
||||
} else {
|
||||
maxLines = 15; // 15 rows spaced 1/32 apart is roughly the middle half of the image
|
||||
}
|
||||
15 // 15 rows spaced 1/32 apart is roughly the middle half of the image
|
||||
};
|
||||
|
||||
let middle = height / 2;
|
||||
for x in 0..maxLines {
|
||||
@@ -143,7 +142,7 @@ pub trait OneDReader: Reader {
|
||||
}
|
||||
}
|
||||
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -193,7 +192,7 @@ pub fn patternMatchVariance(counters: &[u32], pattern: &[u32], maxIndividualVari
|
||||
}
|
||||
|
||||
let unitBarWidth = total / patternLength as f32;
|
||||
maxIndividualVariance *= unitBarWidth as f32;
|
||||
maxIndividualVariance *= unitBarWidth;
|
||||
|
||||
let mut totalVariance = 0.0;
|
||||
for x in 0..numCounters {
|
||||
@@ -210,7 +209,7 @@ pub fn patternMatchVariance(counters: &[u32], pattern: &[u32], maxIndividualVari
|
||||
}
|
||||
totalVariance += variance;
|
||||
}
|
||||
return totalVariance / total;
|
||||
totalVariance / total
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -232,7 +231,7 @@ pub fn recordPattern(row: &BitArray, start: usize, counters: &mut [u32]) -> Resu
|
||||
counters.fill(0);
|
||||
let end = row.getSize();
|
||||
if start >= end {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
let mut isWhite = !row.get(start);
|
||||
let mut counterPosition = 0;
|
||||
@@ -254,7 +253,7 @@ pub fn recordPattern(row: &BitArray, start: usize, counters: &mut [u32]) -> Resu
|
||||
// If we read fully the last section of pixels and filled up our counters -- or filled
|
||||
// the last counter but ran off the side of the image, OK. Otherwise, a problem.
|
||||
if !(counterPosition == numCounters || (counterPosition == numCounters - 1 && i == end)) {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -276,7 +275,7 @@ pub fn recordPatternInReverse(
|
||||
}
|
||||
}
|
||||
if numTransitionsLeft >= 0 {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
recordPattern(row, start + 1, counters)?;
|
||||
|
||||
|
||||
@@ -47,18 +47,16 @@ pub trait AbstractRSSReaderTrait: OneDReader {
|
||||
// }
|
||||
|
||||
fn parseFinderValue(counters: &[u32], finderPatterns: &[[u32; 4]]) -> Result<u32, Exceptions> {
|
||||
for value in 0..finderPatterns.len() {
|
||||
for (value, pattern) in finderPatterns.iter().enumerate() {
|
||||
// for value in 0..finderPatterns.len() {
|
||||
// for (int value = 0; value < finderPatterns.length; value++) {
|
||||
if one_d_reader::patternMatchVariance(
|
||||
counters,
|
||||
&finderPatterns[value],
|
||||
Self::MAX_INDIVIDUAL_VARIANCE,
|
||||
) < Self::MAX_AVG_VARIANCE
|
||||
if one_d_reader::patternMatchVariance(counters, pattern, Self::MAX_INDIVIDUAL_VARIANCE)
|
||||
< Self::MAX_AVG_VARIANCE
|
||||
{
|
||||
return Ok(value as u32);
|
||||
}
|
||||
}
|
||||
Err(Exceptions::NotFoundException("".to_owned()))
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,10 +72,10 @@ pub trait AbstractRSSReaderTrait: OneDReader {
|
||||
fn increment(array: &mut [u32], errors: &[f32]) {
|
||||
let mut index = 0;
|
||||
let mut biggestError = errors[0];
|
||||
for i in 1..array.len() {
|
||||
for (i, error) in errors.iter().enumerate().take(array.len()).skip(1) {
|
||||
// for (int i = 1; i < array.length; i++) {
|
||||
if errors[i] > biggestError {
|
||||
biggestError = errors[i];
|
||||
if *error > biggestError {
|
||||
biggestError = *error;
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
@@ -87,10 +85,11 @@ pub trait AbstractRSSReaderTrait: OneDReader {
|
||||
fn decrement(array: &mut [u32], errors: &[f32]) {
|
||||
let mut index = 0;
|
||||
let mut biggestError = errors[0];
|
||||
for i in 1..array.len() {
|
||||
for (i, error) in errors.iter().enumerate().take(array.len()).skip(1) {
|
||||
// for i in 1..array.len() {
|
||||
// for (int i = 1; i < array.length; i++) {
|
||||
if errors[i] < biggestError {
|
||||
biggestError = errors[i];
|
||||
if *error < biggestError {
|
||||
biggestError = *error;
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
@@ -116,6 +115,6 @@ pub trait AbstractRSSReaderTrait: OneDReader {
|
||||
}
|
||||
return maxCounter < 10 * minCounter;
|
||||
}
|
||||
return false;
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,9 +53,9 @@ pub fn buildBitArrayFromString(data: &str) -> Result<BitArray, Exceptions> {
|
||||
if i % 9 == 0 {
|
||||
// spaces
|
||||
if dotsAndXs.chars().nth(i).unwrap() != ' ' {
|
||||
return Err(Exceptions::IllegalStateException(
|
||||
return Err(Exceptions::IllegalStateException(Some(
|
||||
"space expected".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ use super::ExpandedPair;
|
||||
|
||||
pub fn buildBitArray(pairs: &Vec<ExpandedPair>) -> BitArray {
|
||||
let mut charNumber = (pairs.len() * 2) - 1;
|
||||
if pairs.get(pairs.len() - 1).unwrap().getRightChar().is_none() {
|
||||
if pairs.last().unwrap().getRightChar().is_none() {
|
||||
charNumber -= 1;
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ pub fn buildBitArray(pairs: &Vec<ExpandedPair>) -> BitArray {
|
||||
}
|
||||
}
|
||||
}
|
||||
return binary;
|
||||
binary
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,9 +118,8 @@ mod BitArrayBuilderTest {
|
||||
|
||||
fn buildBitArray(pairValues: &Vec<Vec<u32>>) -> BitArray {
|
||||
let mut pairs = Vec::new(); //new ArrayList<>();
|
||||
for i in 0..pairValues.len() {
|
||||
for (i, pair) in pairValues.iter().enumerate() {
|
||||
// for (int i = 0; i < pairValues.length; ++i) {
|
||||
let pair = &pairValues[i];
|
||||
|
||||
let leftChar = if i == 0 {
|
||||
None
|
||||
|
||||
@@ -149,8 +149,8 @@ pub fn createDecoder<'a>(
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Err(Exceptions::IllegalStateException(format!(
|
||||
Err(Exceptions::IllegalStateException(Some(format!(
|
||||
"unknown decoder: {}",
|
||||
information
|
||||
)))
|
||||
))))
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ impl AI01decoder for AI01392xDecoder<'_> {}
|
||||
impl AbstractExpandedDecoder for AI01392xDecoder<'_> {
|
||||
fn parseInformation(&mut self) -> Result<String, crate::Exceptions> {
|
||||
if self.information.getSize() < Self::HEADER_SIZE + Self::GTIN_SIZE as usize {
|
||||
return Err(crate::Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(crate::Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
let mut buf = String::new();
|
||||
@@ -58,7 +58,7 @@ impl AbstractExpandedDecoder for AI01392xDecoder<'_> {
|
||||
Self::HEADER_SIZE + Self::GTIN_SIZE as usize + Self::LAST_DIGIT_SIZE,
|
||||
"",
|
||||
)?;
|
||||
buf.push_str(&decodedInformation.getNewString());
|
||||
buf.push_str(decodedInformation.getNewString());
|
||||
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ impl AI01decoder for AI01393xDecoder<'_> {}
|
||||
impl AbstractExpandedDecoder for AI01393xDecoder<'_> {
|
||||
fn parseInformation(&mut self) -> Result<String, crate::Exceptions> {
|
||||
if self.information.getSize() < Self::HEADER_SIZE + Self::GTIN_SIZE as usize {
|
||||
return Err(crate::Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(crate::Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
let mut buf = String::new();
|
||||
@@ -74,7 +74,7 @@ impl AbstractExpandedDecoder for AI01393xDecoder<'_> {
|
||||
+ Self::FIRST_THREE_DIGITS_SIZE,
|
||||
"",
|
||||
)?;
|
||||
buf.push_str(&generalInformation.getNewString());
|
||||
buf.push_str(generalInformation.getNewString());
|
||||
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ impl AbstractExpandedDecoder for AI013x0x1xDecoder<'_> {
|
||||
if self.information.getSize()
|
||||
!= Self::HEADER_SIZE + Self::GTIN_SIZE as usize + Self::WEIGHT_SIZE + Self::DATE_SIZE
|
||||
{
|
||||
return Err(crate::Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(crate::Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
let mut buf = String::new();
|
||||
|
||||
@@ -53,7 +53,7 @@ impl AbstractExpandedDecoder for AI013x0xDecoder<'_> {
|
||||
if self.information.getSize()
|
||||
!= Self::HEADER_SIZE + Self::GTIN_SIZE as usize + Self::WEIGHT_SIZE
|
||||
{
|
||||
return Err(crate::Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(crate::Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
let mut buf = String::new();
|
||||
|
||||
@@ -33,7 +33,7 @@ use super::{AI01decoder, AbstractExpandedDecoder, GeneralAppIdDecoder};
|
||||
* @author Eduardo Castillejo, University of Deusto (eduardo.castillejo@deusto.es)
|
||||
*/
|
||||
pub struct AI01AndOtherAIs<'a>(&'a BitArray, GeneralAppIdDecoder<'a>);
|
||||
impl<'a> AI01decoder for AI01AndOtherAIs<'_> {}
|
||||
impl AI01decoder for AI01AndOtherAIs<'_> {}
|
||||
impl AbstractExpandedDecoder for AI01AndOtherAIs<'_> {
|
||||
fn parseInformation(&mut self) -> Result<String, crate::Exceptions> {
|
||||
let mut buff = String::new(); //new StringBuilder();
|
||||
|
||||
@@ -34,6 +34,13 @@ pub struct BlockParsedRXingResult {
|
||||
decodedInformation: Option<DecodedInformation>,
|
||||
finished: bool,
|
||||
}
|
||||
|
||||
impl Default for BlockParsedRXingResult {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl BlockParsedRXingResult {
|
||||
pub fn new() -> Self {
|
||||
Self::with_information(None, false)
|
||||
|
||||
@@ -25,9 +25,9 @@
|
||||
*/
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum State {
|
||||
NUMERIC,
|
||||
ALPHA,
|
||||
ISO_IEC_646,
|
||||
Numeric,
|
||||
Alpha,
|
||||
IsoIec646,
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,7 +42,7 @@ impl CurrentParsingState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
position: 0,
|
||||
encoding: State::NUMERIC,
|
||||
encoding: State::Numeric,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,26 +59,32 @@ impl CurrentParsingState {
|
||||
}
|
||||
|
||||
pub fn isAlpha(&self) -> bool {
|
||||
self.encoding == State::ALPHA
|
||||
self.encoding == State::Alpha
|
||||
}
|
||||
|
||||
pub fn isNumeric(&self) -> bool {
|
||||
self.encoding == State::NUMERIC
|
||||
self.encoding == State::Numeric
|
||||
}
|
||||
|
||||
pub fn isIsoIec646(&self) -> bool {
|
||||
self.encoding == State::ISO_IEC_646
|
||||
self.encoding == State::IsoIec646
|
||||
}
|
||||
|
||||
pub fn setNumeric(&mut self) {
|
||||
self.encoding = State::NUMERIC;
|
||||
self.encoding = State::Numeric;
|
||||
}
|
||||
|
||||
pub fn setAlpha(&mut self) {
|
||||
self.encoding = State::ALPHA;
|
||||
self.encoding = State::Alpha;
|
||||
}
|
||||
|
||||
pub fn setIsoIec646(&mut self) {
|
||||
self.encoding = State::ISO_IEC_646;
|
||||
self.encoding = State::IsoIec646;
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for CurrentParsingState {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,9 +51,7 @@ impl DecodedNumeric {
|
||||
if
|
||||
/*firstDigit < 0 ||*/
|
||||
firstDigit > 10 || /*secondDigit < 0 ||*/ secondDigit > 10 {
|
||||
return Err(Exceptions::FormatException(
|
||||
".getFormatInstance();".to_owned(),
|
||||
));
|
||||
return Err(Exceptions::FormatException(None));
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
|
||||
@@ -149,7 +149,7 @@ pub fn parseFieldsInGeneralPurpose(rawInformation: &str) -> Result<String, Excep
|
||||
// Processing 2-digit AIs
|
||||
|
||||
if rawInformation.chars().count() < 2 {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
let lookup: String = rawInformation.chars().take(2).collect();
|
||||
@@ -162,7 +162,7 @@ pub fn parseFieldsInGeneralPurpose(rawInformation: &str) -> Result<String, Excep
|
||||
}
|
||||
|
||||
if rawInformation.chars().count() < 3 {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
let firstThreeDigits: String = rawInformation.chars().take(3).collect(); //rawInformation.substring(0, 3);
|
||||
@@ -175,7 +175,7 @@ pub fn parseFieldsInGeneralPurpose(rawInformation: &str) -> Result<String, Excep
|
||||
}
|
||||
|
||||
if rawInformation.chars().count() < 4 {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
let threeDigitPlusDigitDataLength = THREE_DIGIT_PLUS_DIGIT_DATA_LENGTH.get(&firstThreeDigits);
|
||||
@@ -195,7 +195,7 @@ pub fn parseFieldsInGeneralPurpose(rawInformation: &str) -> Result<String, Excep
|
||||
return processFixedAI(4, ffdl.length, rawInformation);
|
||||
}
|
||||
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
|
||||
fn processFixedAI(
|
||||
@@ -204,13 +204,13 @@ fn processFixedAI(
|
||||
rawInformation: &str,
|
||||
) -> Result<String, Exceptions> {
|
||||
if rawInformation.chars().count() < aiSize {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
let ai: String = rawInformation.chars().take(aiSize).collect();
|
||||
|
||||
if rawInformation.chars().count() < aiSize + fieldSize {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
let field: String = rawInformation
|
||||
@@ -234,16 +234,12 @@ fn processVariableAI(
|
||||
variableFieldSize: usize,
|
||||
rawInformation: &str,
|
||||
) -> Result<String, Exceptions> {
|
||||
let ai: String = rawInformation.chars().take(aiSize as usize).collect(); //rawInformation.substring(0, aiSize);
|
||||
let ai: String = rawInformation.chars().take(aiSize).collect(); //rawInformation.substring(0, aiSize);
|
||||
let maxSize = rawInformation
|
||||
.chars()
|
||||
.count()
|
||||
.min(aiSize + variableFieldSize);
|
||||
let field: String = rawInformation
|
||||
.chars()
|
||||
.skip(aiSize as usize)
|
||||
.take(maxSize)
|
||||
.collect(); // (aiSize, maxSize);
|
||||
let field: String = rawInformation.chars().skip(aiSize).take(maxSize).collect(); // (aiSize, maxSize);
|
||||
let remaining: String = rawInformation.chars().skip(maxSize).collect();
|
||||
let result = format!("({}){}", ai, field); //'(' + ai + ')' + field;
|
||||
let parsedAI = parseFieldsInGeneralPurpose(&remaining)?;
|
||||
@@ -287,7 +283,7 @@ impl DataLength {
|
||||
mod FieldParserTest {
|
||||
|
||||
fn checkFields(expected: &str) {
|
||||
let field = expected.replace("(", "").replace(")", "");
|
||||
let field = expected.replace(['(', ')'], "");
|
||||
let actual = super::parseFieldsInGeneralPurpose(&field).expect("parse");
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
}
|
||||
|
||||
pub fn extractNumericValueFromBitArray(&self, pos: usize, bits: u32) -> u32 {
|
||||
Self::extractNumericValueFromBitArrayWithInformation(&self.information, pos, bits)
|
||||
Self::extractNumericValueFromBitArrayWithInformation(self.information, pos, bits)
|
||||
}
|
||||
|
||||
pub fn extractNumericValueFromBitArrayWithInformation(
|
||||
@@ -192,7 +192,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
break;
|
||||
}
|
||||
|
||||
if !(!isFinished) {
|
||||
if isFinished {
|
||||
break;
|
||||
}
|
||||
} //while (!isFinished);
|
||||
@@ -200,7 +200,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
if result.getDecodedInformation().is_some() {
|
||||
Ok(result.getDecodedInformation().as_ref().unwrap().clone())
|
||||
} else {
|
||||
Err(Exceptions::NotFoundException("".to_owned()))
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -315,7 +315,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
}
|
||||
|
||||
let fiveBitValue = self.extractNumericValueFromBitArray(pos, 5);
|
||||
if fiveBitValue >= 5 && fiveBitValue < 16 {
|
||||
if (5..16).contains(&fiveBitValue) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -324,7 +324,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
}
|
||||
|
||||
let sevenBitValue = self.extractNumericValueFromBitArray(pos, 7);
|
||||
if sevenBitValue >= 64 && sevenBitValue < 116 {
|
||||
if (64..116).contains(&sevenBitValue) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -334,7 +334,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
|
||||
let eightBitValue = self.extractNumericValueFromBitArray(pos, 8);
|
||||
|
||||
eightBitValue >= 232 && eightBitValue < 253
|
||||
(232..253).contains(&eightBitValue)
|
||||
}
|
||||
|
||||
fn decodeIsoIec646(&self, pos: usize) -> Result<DecodedChar, Exceptions> {
|
||||
@@ -343,7 +343,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
return Ok(DecodedChar::new(pos + 5, DecodedChar::FNC1));
|
||||
}
|
||||
|
||||
if fiveBitValue >= 5 && fiveBitValue < 15 {
|
||||
if (5..15).contains(&fiveBitValue) {
|
||||
return Ok(DecodedChar::new(
|
||||
pos + 5,
|
||||
char::from_u32('0' as u32 + fiveBitValue - 5).unwrap(),
|
||||
@@ -352,14 +352,14 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
|
||||
let sevenBitValue = self.extractNumericValueFromBitArray(pos, 7);
|
||||
|
||||
if sevenBitValue >= 64 && sevenBitValue < 90 {
|
||||
if (64..90).contains(&sevenBitValue) {
|
||||
return Ok(DecodedChar::new(
|
||||
pos + 7,
|
||||
char::from_u32(sevenBitValue + 1).unwrap(),
|
||||
));
|
||||
}
|
||||
|
||||
if sevenBitValue >= 90 && sevenBitValue < 116 {
|
||||
if (90..116).contains(&sevenBitValue) {
|
||||
return Ok(DecodedChar::new(
|
||||
pos + 7,
|
||||
char::from_u32(sevenBitValue + 7).unwrap(),
|
||||
@@ -389,7 +389,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
250 => '?',
|
||||
251 => '_',
|
||||
252 => ' ',
|
||||
_ => return Err(Exceptions::FormatException("".to_owned())),
|
||||
_ => return Err(Exceptions::FormatException(None)),
|
||||
};
|
||||
|
||||
Ok(DecodedChar::new(pos + 8, c))
|
||||
@@ -402,7 +402,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
|
||||
// We now check if it's a valid 5-bit value (0..9 and FNC1)
|
||||
let fiveBitValue = self.extractNumericValueFromBitArray(pos, 5);
|
||||
if fiveBitValue >= 5 && fiveBitValue < 16 {
|
||||
if (5..16).contains(&fiveBitValue) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -412,7 +412,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
|
||||
let sixBitValue = self.extractNumericValueFromBitArray(pos, 6);
|
||||
|
||||
sixBitValue >= 16 && sixBitValue < 63 // 63 not included
|
||||
(16..63).contains(&sixBitValue) // 63 not included
|
||||
}
|
||||
|
||||
fn decodeAlphanumeric(&self, pos: usize) -> Result<DecodedChar, Exceptions> {
|
||||
@@ -421,7 +421,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
return Ok(DecodedChar::new(pos + 5, DecodedChar::FNC1));
|
||||
}
|
||||
|
||||
if fiveBitValue >= 5 && fiveBitValue < 15 {
|
||||
if (5..15).contains(&fiveBitValue) {
|
||||
return Ok(DecodedChar::new(
|
||||
pos + 5,
|
||||
char::from_u32('0' as u32 + fiveBitValue - 5).unwrap(),
|
||||
@@ -430,7 +430,7 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
|
||||
let sixBitValue = self.extractNumericValueFromBitArray(pos, 6);
|
||||
|
||||
if sixBitValue >= 32 && sixBitValue < 58 {
|
||||
if (32..58).contains(&sixBitValue) {
|
||||
return Ok(DecodedChar::new(
|
||||
pos + 6,
|
||||
char::from_u32(sixBitValue + 33).unwrap(),
|
||||
@@ -444,10 +444,10 @@ impl<'a> GeneralAppIdDecoder<'_> {
|
||||
61 => '.',
|
||||
62 => '/',
|
||||
_ => {
|
||||
return Err(Exceptions::IllegalStateException(format!(
|
||||
return Err(Exceptions::IllegalStateException(Some(format!(
|
||||
"Decoding invalid alphanumeric value: {}",
|
||||
sixBitValue
|
||||
)))
|
||||
))))
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -14,14 +14,14 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use std::fmt::Display;
|
||||
use std::{fmt::Display, hash::Hash};
|
||||
|
||||
use super::ExpandedPair;
|
||||
|
||||
/**
|
||||
* One row of an RSS Expanded Stacked symbol, consisting of 1+ expanded pairs.
|
||||
*/
|
||||
#[derive(Hash, Clone)]
|
||||
#[derive(Clone)]
|
||||
pub struct ExpandedRow {
|
||||
pairs: Vec<ExpandedPair>,
|
||||
rowNumber: u32,
|
||||
@@ -58,6 +58,12 @@ impl PartialEq for ExpandedRow {
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for ExpandedRow {
|
||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||
self.pairs.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for ExpandedRow {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{{ ")?;
|
||||
|
||||
@@ -74,7 +74,7 @@ fn assertCorrectImage2result(fileName: &str, expected: ExpandedProductParsedRXin
|
||||
let mut binaryMap = BinaryBitmap::new(Rc::new(RefCell::new(GlobalHistogramBinarizer::new(
|
||||
Box::new(BufferedImageLuminanceSource::new(image)),
|
||||
))));
|
||||
let rowNumber = binaryMap.getHeight() as usize / 2;
|
||||
let rowNumber = binaryMap.getHeight() / 2;
|
||||
let row = binaryMap.getBlackRow(rowNumber).expect("get row");
|
||||
|
||||
let mut rssExpandedReader = RSSExpandedReader::new();
|
||||
|
||||
@@ -37,7 +37,7 @@ use crate::{
|
||||
OneDReader,
|
||||
},
|
||||
BarcodeFormat, DecodeHintType, DecodingHintDictionary, Exceptions, RXingResult,
|
||||
RXingResultMetadataType, RXingResultMetadataValue, RXingResultPoint, Reader, ResultPoint,
|
||||
RXingResultMetadataType, RXingResultMetadataValue, Reader,
|
||||
};
|
||||
|
||||
use super::{bit_array_builder, decoders::abstract_expanded_decoder, ExpandedPair, ExpandedRow};
|
||||
@@ -132,6 +132,7 @@ lazy_static! {
|
||||
* @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es)
|
||||
* @author Eduardo Castillejo, University of Deusto (eduardo.castillejo@deusto.es)
|
||||
*/
|
||||
#[derive(Default)]
|
||||
pub struct RSSExpandedReader {
|
||||
_possibleLeftPairs: Vec<Pair>,
|
||||
_possibleRightPairs: Vec<Pair>,
|
||||
@@ -225,18 +226,21 @@ impl Reader for RSSExpandedReader {
|
||||
// for point in result.getRXingResultPointsMut().iter_mut() {
|
||||
let total_points = result.getRXingResultPoints().len();
|
||||
let points = result.getRXingResultPointsMut();
|
||||
for i in 0..total_points {
|
||||
for point in points.iter_mut().take(total_points) {
|
||||
// for i in 0..total_points {
|
||||
// for (int i = 0; i < points.length; i++) {
|
||||
points[i] = RXingResultPoint::new(
|
||||
height as f32 - points[i].getY() - 1.0,
|
||||
points[i].getX(),
|
||||
);
|
||||
std::mem::swap(&mut point.x, &mut point.y);
|
||||
point.x = height as f32 - point.x - 1.0;
|
||||
// points[i] = RXingResultPoint::new(
|
||||
// height as f32 - points[i].getY() - 1.0,
|
||||
// points[i].getX(),
|
||||
// );
|
||||
}
|
||||
// }
|
||||
|
||||
Ok(result)
|
||||
} else {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -335,16 +339,16 @@ impl RSSExpandedReader {
|
||||
// When the image is 180-rotated, then rows are sorted in wrong direction.
|
||||
// Try twice with both the directions.
|
||||
let ps = self.checkRows(false);
|
||||
if ps.is_some() {
|
||||
return Ok(ps.unwrap());
|
||||
if let Some(ps) = ps {
|
||||
return Ok(ps);
|
||||
}
|
||||
let ps = self.checkRows(true);
|
||||
if ps.is_some() {
|
||||
return Ok(ps.unwrap());
|
||||
if let Some(ps) = ps {
|
||||
return Ok(ps);
|
||||
}
|
||||
}
|
||||
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
|
||||
fn checkRows(&mut self, reverse: bool) -> Option<Vec<ExpandedPair>> {
|
||||
@@ -415,7 +419,7 @@ impl RSSExpandedReader {
|
||||
}
|
||||
}
|
||||
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
|
||||
// Whether the pairs form a valid find pattern sequence,
|
||||
@@ -427,7 +431,7 @@ impl RSSExpandedReader {
|
||||
// for (int[] sequence : FINDER_PATTERN_SEQUENCES) {
|
||||
if pairs.len() <= sequence.len() {
|
||||
let mut stop = true;
|
||||
for j in 0..pairs.len() {
|
||||
for (j, seq) in sequence.iter().enumerate().take(pairs.len()) {
|
||||
// for (int j = 0; j < pairs.size(); j++) {
|
||||
if pairs
|
||||
.get(j)
|
||||
@@ -436,7 +440,7 @@ impl RSSExpandedReader {
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.getValue()
|
||||
!= sequence[j]
|
||||
!= *seq
|
||||
{
|
||||
stop = false;
|
||||
break;
|
||||
@@ -448,7 +452,7 @@ impl RSSExpandedReader {
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
false
|
||||
}
|
||||
|
||||
fn storeRow(&mut self, rowNumber: u32) {
|
||||
@@ -535,7 +539,7 @@ impl RSSExpandedReader {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
false
|
||||
}
|
||||
|
||||
// Only used for unit testing
|
||||
@@ -564,7 +568,7 @@ impl RSSExpandedReader {
|
||||
.unwrap()
|
||||
.getRXingResultPoints();
|
||||
let lastPoints = pairs
|
||||
.get(pairs.len() - 1)
|
||||
.last()
|
||||
.unwrap()
|
||||
.getFinderPattern()
|
||||
.as_ref()
|
||||
@@ -663,13 +667,8 @@ impl RSSExpandedReader {
|
||||
let leftChar =
|
||||
self.decodeDataCharacter(row, pattern.as_ref().unwrap(), isOddPattern, true)?;
|
||||
|
||||
if !previousPairs.is_empty()
|
||||
&& previousPairs
|
||||
.get(previousPairs.len() - 1)
|
||||
.unwrap()
|
||||
.mustBeLast()
|
||||
{
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
if !previousPairs.is_empty() && previousPairs.last().unwrap().mustBeLast() {
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
let rightChar = if let Ok(ch) =
|
||||
@@ -708,7 +707,7 @@ impl RSSExpandedReader {
|
||||
} else if previousPairs.is_empty() {
|
||||
rowOffset = 0;
|
||||
} else {
|
||||
let lastPair = previousPairs.get(previousPairs.len() - 1).unwrap();
|
||||
let lastPair = previousPairs.last().unwrap();
|
||||
rowOffset = lastPair.getFinderPattern().as_ref().unwrap().getStartEnd()[1] as i32;
|
||||
}
|
||||
let mut searchingEvenPair = previousPairs.len() % 2 != 0;
|
||||
@@ -760,16 +759,14 @@ impl RSSExpandedReader {
|
||||
isWhite = !isWhite;
|
||||
}
|
||||
}
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
|
||||
fn reverseCounters(counters: &mut [u32]) {
|
||||
let length = counters.len();
|
||||
for i in 0..length / 2 {
|
||||
// for (int i = 0; i < length / 2; ++i) {
|
||||
let tmp = counters[i];
|
||||
counters[i] = counters[length - i - 1];
|
||||
counters[length - i - 1] = tmp;
|
||||
counters.swap(i, length - i - 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -861,7 +858,7 @@ impl RSSExpandedReader {
|
||||
let expectedElementWidth: f32 =
|
||||
(pattern.getStartEnd()[1] - pattern.getStartEnd()[0]) as f32 / 15.0;
|
||||
if (elementWidth - expectedElementWidth).abs() / expectedElementWidth > 0.3 {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
// let oddCounts = &mut self.oddCounts;
|
||||
@@ -869,18 +866,18 @@ impl RSSExpandedReader {
|
||||
// let oddRoundingErrors = &mut self.oddRoundingErrors;
|
||||
// let evenRoundingErrors = &mut self.evenRoundingErrors;
|
||||
|
||||
for i in 0..counters.len() {
|
||||
for (i, counter) in counters.iter().enumerate() {
|
||||
// for (int i = 0; i < counters.length; i++) {
|
||||
let value: f32 = 1.0 * counters[i] as f32 / elementWidth;
|
||||
let value: f32 = 1.0 * (*counter as f32) / elementWidth;
|
||||
let mut count = (value + 0.5) as i32; // Round
|
||||
if count < 1 {
|
||||
if value < 0.3 {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
count = 1;
|
||||
} else if count > 8 {
|
||||
if value > 8.7 {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
count = 8;
|
||||
}
|
||||
@@ -898,7 +895,7 @@ impl RSSExpandedReader {
|
||||
|
||||
let weightRowNumber = (4 * pattern.getValue() as isize
|
||||
+ (if isOddPattern { 0 } else { 2 })
|
||||
+ (if leftChar { 0 } else { 1 })
|
||||
+ isize::from(!leftChar)//(if leftChar { 0 } else { 1 })
|
||||
- 1) as usize;
|
||||
|
||||
let mut oddSum = 0;
|
||||
@@ -921,8 +918,8 @@ impl RSSExpandedReader {
|
||||
}
|
||||
let checksumPortion = oddChecksumPortion + evenChecksumPortion;
|
||||
|
||||
if (oddSum & 0x01) != 0 || oddSum > 13 || oddSum < 4 {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
if (oddSum & 0x01) != 0 || !(4..=13).contains(&oddSum) {
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
let group = ((13 - oddSum) / 2) as usize;
|
||||
@@ -971,12 +968,12 @@ impl RSSExpandedReader {
|
||||
1 => {
|
||||
if oddParityBad {
|
||||
if evenParityBad {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
decrementOdd = true;
|
||||
} else {
|
||||
if !evenParityBad {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
decrementEven = true;
|
||||
}
|
||||
@@ -984,12 +981,12 @@ impl RSSExpandedReader {
|
||||
-1 => {
|
||||
if oddParityBad {
|
||||
if evenParityBad {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
incrementOdd = true;
|
||||
} else {
|
||||
if !evenParityBad {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
incrementEven = true;
|
||||
}
|
||||
@@ -997,7 +994,7 @@ impl RSSExpandedReader {
|
||||
0 => {
|
||||
if oddParityBad {
|
||||
if !evenParityBad {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
// Both bad
|
||||
if oddSum < evenSum {
|
||||
@@ -1007,20 +1004,17 @@ impl RSSExpandedReader {
|
||||
decrementOdd = true;
|
||||
incrementEven = true;
|
||||
}
|
||||
} else {
|
||||
if evenParityBad {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
}
|
||||
// Nothing to do!
|
||||
} else if evenParityBad {
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
}
|
||||
|
||||
_ => return Err(Exceptions::NotFoundException("".to_owned())),
|
||||
_ => return Err(Exceptions::NotFoundException(None)),
|
||||
}
|
||||
|
||||
if incrementOdd {
|
||||
if decrementOdd {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
Self::increment(&mut self.oddCounts, &self.oddRoundingErrors);
|
||||
}
|
||||
@@ -1029,7 +1023,7 @@ impl RSSExpandedReader {
|
||||
}
|
||||
if incrementEven {
|
||||
if decrementEven {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
Self::increment(&mut self.evenCounts, &self.oddRoundingErrors);
|
||||
}
|
||||
@@ -1040,22 +1034,3 @@ impl RSSExpandedReader {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for RSSExpandedReader {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
_possibleLeftPairs: Default::default(),
|
||||
_possibleRightPairs: Default::default(),
|
||||
decodeFinderCounters: Default::default(),
|
||||
dataCharacterCounters: Default::default(),
|
||||
oddRoundingErrors: Default::default(),
|
||||
evenRoundingErrors: Default::default(),
|
||||
oddCounts: Default::default(),
|
||||
evenCounts: Default::default(),
|
||||
pairs: Default::default(),
|
||||
rows: Default::default(),
|
||||
startEnd: Default::default(),
|
||||
startFromEven: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,16 +33,13 @@ use crate::{common::GlobalHistogramBinarizer, BinaryBitmap, BufferedImageLuminan
|
||||
fn getBufferedImage(fileName: &str) -> DynamicImage {
|
||||
let path = format!("test_resources/blackbox/rssexpandedstacked-2/{}", fileName);
|
||||
|
||||
let image = image::open(path).expect("load image");
|
||||
|
||||
image
|
||||
image::open(path).expect("load image")
|
||||
}
|
||||
|
||||
pub(crate) fn getBinaryBitmap(fileName: &str) -> BinaryBitmap {
|
||||
let bufferedImage = getBufferedImage(fileName);
|
||||
let binaryMap = BinaryBitmap::new(Rc::new(RefCell::new(GlobalHistogramBinarizer::new(
|
||||
Box::new(BufferedImageLuminanceSource::new(bufferedImage)),
|
||||
))));
|
||||
|
||||
binaryMap
|
||||
BinaryBitmap::new(Rc::new(RefCell::new(GlobalHistogramBinarizer::new(
|
||||
Box::new(BufferedImageLuminanceSource::new(bufferedImage)),
|
||||
))))
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ use crate::{
|
||||
common::BitArray,
|
||||
oned::{one_d_reader, OneDReader},
|
||||
BarcodeFormat, DecodeHintType, DecodingHintDictionary, Exceptions, RXingResult,
|
||||
RXingResultMetadataType, RXingResultMetadataValue, RXingResultPoint, Reader, ResultPoint,
|
||||
RXingResultMetadataType, RXingResultMetadataValue, Reader,
|
||||
};
|
||||
|
||||
use super::{
|
||||
@@ -63,13 +63,13 @@ impl OneDReader for RSS14Reader {
|
||||
if left.getCount() > 1 {
|
||||
for right in &self.possibleRightPairs {
|
||||
// for (Pair right : possibleRightPairs) {
|
||||
if right.getCount() > 1 && self.checkChecksum(&left, &right) {
|
||||
return Ok(self.constructRXingResult(&left, &right));
|
||||
if right.getCount() > 1 && self.checkChecksum(left, right) {
|
||||
return Ok(self.constructRXingResult(left, right));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
}
|
||||
impl Reader for RSS14Reader {
|
||||
@@ -119,18 +119,21 @@ impl Reader for RSS14Reader {
|
||||
// for point in result.getRXingResultPointsMut().iter_mut() {
|
||||
let total_points = result.getRXingResultPoints().len();
|
||||
let points = result.getRXingResultPointsMut();
|
||||
for i in 0..total_points {
|
||||
// for i in 0..total_points {
|
||||
for point in points.iter_mut().take(total_points) {
|
||||
// for (int i = 0; i < points.length; i++) {
|
||||
points[i] = RXingResultPoint::new(
|
||||
height as f32 - points[i].getY() - 1.0,
|
||||
points[i].getX(),
|
||||
);
|
||||
std::mem::swap(&mut point.x, &mut point.y);
|
||||
point.x = height as f32 - point.x - 1.0
|
||||
// points[i] = RXingResultPoint::new(
|
||||
// height as f32 - points[i].getY() - 1.0,
|
||||
// points[i].getX(),
|
||||
// );
|
||||
}
|
||||
// }
|
||||
|
||||
Ok(result)
|
||||
} else {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -284,10 +287,10 @@ impl RSS14Reader {
|
||||
pattern,
|
||||
))
|
||||
}();
|
||||
if pos_pair.is_err() {
|
||||
None
|
||||
if let Ok(ppair) = pos_pair {
|
||||
Some(ppair)
|
||||
} else {
|
||||
Some(pos_pair.unwrap())
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
@@ -328,15 +331,16 @@ impl RSS14Reader {
|
||||
// let oddRoundingErrors = //&mut self.oddRoundingErrors;//[0f32; 4]; // self.getOddRoundingErrors();
|
||||
// let evenRoundingErrors = &mut self.evenRoundingErrors;//[0f32; 4]; // self.getEvenRoundingErrors();
|
||||
|
||||
for i in 0..counters.len() {
|
||||
for (i, counter) in counters.iter().enumerate() {
|
||||
// for (int i = 0; i < counters.length; i++) {
|
||||
let value: f32 = counters[i] as f32 / elementWidth;
|
||||
let mut count = (value + 0.5) as u32; // Round
|
||||
if count < 1 {
|
||||
count = 1;
|
||||
} else if count > 8 {
|
||||
count = 8;
|
||||
}
|
||||
let value: f32 = *counter as f32 / elementWidth;
|
||||
// let mut count = (value + 0.5) as u32; // Round
|
||||
let count = ((value + 0.5) as u32).clamp(1, 8);
|
||||
// if count < 1 {
|
||||
// count = 1;
|
||||
// } else if count > 8 {
|
||||
// count = 8;
|
||||
// }
|
||||
let offset = i / 2;
|
||||
if (i & 0x01) == 0 {
|
||||
self.oddCounts[offset] = count;
|
||||
@@ -368,8 +372,8 @@ impl RSS14Reader {
|
||||
let checksumPortion = oddChecksumPortion + 3 * evenChecksumPortion;
|
||||
|
||||
if outsideChar {
|
||||
if (oddSum & 0x01) != 0 || oddSum > 12 || oddSum < 4 {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
if (oddSum & 0x01) != 0 || !(4..=12).contains(&oddSum) {
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
let group = ((12 - oddSum) / 2) as usize;
|
||||
let oddWidest = Self::OUTSIDE_ODD_WIDEST[group];
|
||||
@@ -378,13 +382,13 @@ impl RSS14Reader {
|
||||
let vEven = rss_utils::getRSSvalue(&self.evenCounts, evenWidest, true);
|
||||
let tEven = Self::OUTSIDE_EVEN_TOTAL_SUBSET[group];
|
||||
let gSum = Self::OUTSIDE_GSUM[group];
|
||||
return Ok(DataCharacter::new(
|
||||
Ok(DataCharacter::new(
|
||||
vOdd * tEven + vEven + gSum,
|
||||
checksumPortion,
|
||||
));
|
||||
))
|
||||
} else {
|
||||
if (evenSum & 0x01) != 0 || evenSum > 10 || evenSum < 4 {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
if (evenSum & 0x01) != 0 || !(4..=10).contains(&evenSum) {
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
let group = ((10 - evenSum) / 2) as usize;
|
||||
let oddWidest = Self::INSIDE_ODD_WIDEST[group];
|
||||
@@ -393,10 +397,10 @@ impl RSS14Reader {
|
||||
let vEven = rss_utils::getRSSvalue(&self.evenCounts, evenWidest, false);
|
||||
let tOdd = Self::INSIDE_ODD_TOTAL_SUBSET[group];
|
||||
let gSum = Self::INSIDE_GSUM[group];
|
||||
return Ok(DataCharacter::new(
|
||||
Ok(DataCharacter::new(
|
||||
vEven * tOdd + vOdd + gSum,
|
||||
checksumPortion,
|
||||
));
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -449,7 +453,7 @@ impl RSS14Reader {
|
||||
isWhite = !isWhite;
|
||||
}
|
||||
}
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
|
||||
fn parseFoundFinderPattern(
|
||||
@@ -534,7 +538,7 @@ impl RSS14Reader {
|
||||
}
|
||||
|
||||
let mismatch = oddSum as i32 + evenSum as i32 - numModules as i32;
|
||||
let oddParityBad = (oddSum & 0x01) == (if outsideChar { 1 } else { 0 });
|
||||
let oddParityBad = (oddSum & 0x01) == u32::from(outsideChar); //(if outsideChar { 1 } else { 0 });
|
||||
let evenParityBad = (evenSum & 0x01) == 1;
|
||||
/*if (mismatch == 2) {
|
||||
if (!(oddParityBad && evenParityBad)) {
|
||||
@@ -553,12 +557,12 @@ impl RSS14Reader {
|
||||
1 => {
|
||||
if oddParityBad {
|
||||
if evenParityBad {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
decrementOdd = true;
|
||||
} else {
|
||||
if !evenParityBad {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
decrementEven = true;
|
||||
}
|
||||
@@ -566,12 +570,12 @@ impl RSS14Reader {
|
||||
-1 => {
|
||||
if oddParityBad {
|
||||
if evenParityBad {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
incrementOdd = true;
|
||||
} else {
|
||||
if !evenParityBad {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
incrementEven = true;
|
||||
}
|
||||
@@ -579,7 +583,7 @@ impl RSS14Reader {
|
||||
0 => {
|
||||
if oddParityBad {
|
||||
if !evenParityBad {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
// Both bad
|
||||
if oddSum < evenSum {
|
||||
@@ -589,19 +593,16 @@ impl RSS14Reader {
|
||||
decrementOdd = true;
|
||||
incrementEven = true;
|
||||
}
|
||||
} else {
|
||||
if evenParityBad {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
}
|
||||
// Nothing to do!
|
||||
} else if evenParityBad {
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
}
|
||||
_ => return Err(Exceptions::NotFoundException("".to_owned())),
|
||||
_ => return Err(Exceptions::NotFoundException(None)),
|
||||
}
|
||||
|
||||
if incrementOdd {
|
||||
if decrementOdd {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
Self::increment(&mut self.oddCounts, &self.oddRoundingErrors);
|
||||
}
|
||||
@@ -610,7 +611,7 @@ impl RSS14Reader {
|
||||
}
|
||||
if incrementEven {
|
||||
if decrementEven {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
Self::increment(&mut self.evenCounts, &self.evenRoundingErrors);
|
||||
}
|
||||
|
||||
@@ -64,13 +64,15 @@ pub fn getRSSvalue(widths: &[u32], maxWidth: u32, noNarrow: bool) -> u32 {
|
||||
}
|
||||
n -= elmWidth;
|
||||
}
|
||||
return val;
|
||||
val
|
||||
}
|
||||
|
||||
fn combins(n: u32, r: u32) -> u32 {
|
||||
let maxDenom;
|
||||
let minDenom;
|
||||
if n - r > r {
|
||||
|
||||
// if n - r > r {
|
||||
if n.checked_sub(r).is_none() {
|
||||
minDenom = r;
|
||||
maxDenom = n - r;
|
||||
} else {
|
||||
|
||||
@@ -24,6 +24,7 @@ use super::{EAN13Reader, OneDReader, UPCEANReader};
|
||||
* @author dswitkin@google.com (Daniel Switkin)
|
||||
* @author Sean Owen
|
||||
*/
|
||||
#[derive(Default)]
|
||||
pub struct UPCAReader(EAN13Reader);
|
||||
|
||||
impl Reader for UPCAReader {
|
||||
@@ -87,20 +88,15 @@ impl UPCEANReader for UPCAReader {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for UPCAReader {
|
||||
fn default() -> Self {
|
||||
Self(Default::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl UPCAReader {
|
||||
// private final UPCEANReader ean13Reader = new EAN13Reader();
|
||||
|
||||
fn maybeReturnRXingResult(result: RXingResult) -> Result<RXingResult, Exceptions> {
|
||||
let text = result.getText();
|
||||
if text.chars().nth(0).unwrap() == '0' {
|
||||
if let Some(stripped_text) = text.strip_prefix('0') {
|
||||
// if text.starts_with('0') {
|
||||
let mut upcaRXingResult = RXingResult::new(
|
||||
&text[1..],
|
||||
stripped_text,
|
||||
Vec::new(),
|
||||
result.getRXingResultPoints().to_vec(),
|
||||
BarcodeFormat::UPC_A,
|
||||
@@ -110,7 +106,7 @@ impl UPCAReader {
|
||||
// }
|
||||
Ok(upcaRXingResult)
|
||||
} else {
|
||||
Err(Exceptions::NotFoundException("".to_owned()))
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,14 +25,9 @@ use super::EAN13Writer;
|
||||
*
|
||||
* @author qwandor@google.com (Andrew Walbran)
|
||||
*/
|
||||
#[derive(Default)]
|
||||
pub struct UPCAWriter(EAN13Writer);
|
||||
|
||||
impl Default for UPCAWriter {
|
||||
fn default() -> Self {
|
||||
Self(Default::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl Writer for UPCAWriter {
|
||||
fn encode(
|
||||
&self,
|
||||
@@ -53,10 +48,10 @@ impl Writer for UPCAWriter {
|
||||
hints: &crate::EncodingHintDictionary,
|
||||
) -> Result<crate::common::BitMatrix, crate::Exceptions> {
|
||||
if format != &BarcodeFormat::UPC_A {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Can only encode UPC-A, but got {:?}",
|
||||
format
|
||||
)));
|
||||
))));
|
||||
}
|
||||
// Transform a UPC-A code into the equivalent EAN-13 code and write it that way
|
||||
self.0.encode_with_hints(
|
||||
@@ -64,7 +59,7 @@ impl Writer for UPCAWriter {
|
||||
&BarcodeFormat::EAN_13,
|
||||
width,
|
||||
height,
|
||||
&hints,
|
||||
hints,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ use rxing_one_d_proc_derive::{EANReader, OneDReader};
|
||||
*
|
||||
* @author Sean Owen
|
||||
*/
|
||||
#[derive(OneDReader, EANReader)]
|
||||
#[derive(OneDReader, EANReader, Default)]
|
||||
pub struct UPCEReader;
|
||||
|
||||
impl UPCEANReader for UPCEReader {
|
||||
@@ -84,12 +84,6 @@ impl UPCEANReader for UPCEReader {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for UPCEReader {
|
||||
fn default() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl UPCEReader {
|
||||
/**
|
||||
* The pattern that marks the middle, and end, of a UPC-E pattern.
|
||||
@@ -146,7 +140,7 @@ impl UPCEReader {
|
||||
}
|
||||
}
|
||||
}
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,7 +154,7 @@ pub fn convertUPCEtoUPCA(upce: &str) -> String {
|
||||
let upceChars = &upce[1..7]; //['0';6];//new char[6];
|
||||
//upce.getChars(1, 7, upceChars, 0);
|
||||
let mut result = String::with_capacity(12); //new StringBuilder(12);
|
||||
result.push(upce.chars().nth(0).unwrap());
|
||||
result.push(upce.chars().next().unwrap());
|
||||
let lastChar = upceChars.chars().nth(5).unwrap();
|
||||
match lastChar {
|
||||
'0' | '1' | '2' => {
|
||||
|
||||
@@ -31,15 +31,11 @@ const CODE_WIDTH: usize = 3 + // start guard
|
||||
*
|
||||
* @author 0979097955s@gmail.com (RX)
|
||||
*/
|
||||
#[derive(OneDWriter)]
|
||||
#[derive(OneDWriter, Default)]
|
||||
pub struct UPCEWriter;
|
||||
|
||||
impl UPCEANWriter for UPCEWriter {}
|
||||
impl Default for UPCEWriter {
|
||||
fn default() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl OneDimensionalCodeWriter for UPCEWriter {
|
||||
fn encode_oned(&self, contents: &str) -> Result<Vec<bool>, Exceptions> {
|
||||
let length = contents.chars().count();
|
||||
@@ -63,29 +59,29 @@ impl OneDimensionalCodeWriter for UPCEWriter {
|
||||
if !reader
|
||||
.checkStandardUPCEANChecksum(&upc_e_reader::convertUPCEtoUPCA(&contents))?
|
||||
{
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"Contents do not pass checksum".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
}
|
||||
// } catch (FormatException ignored) {
|
||||
// throw new IllegalArgumentException("Illegal contents");
|
||||
// }},
|
||||
_ => {
|
||||
return Err(Exceptions::IllegalArgumentException(format!(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Requested contents should be 7 or 8 digits long, but got {}",
|
||||
length
|
||||
)))
|
||||
))))
|
||||
}
|
||||
}
|
||||
|
||||
Self::checkNumeric(&contents)?;
|
||||
|
||||
let firstDigit = contents.chars().nth(0).unwrap().to_digit(10).unwrap() as usize; //Character.digit(contents.charAt(0), 10);
|
||||
let firstDigit = contents.chars().next().unwrap().to_digit(10).unwrap() as usize; //Character.digit(contents.charAt(0), 10);
|
||||
if firstDigit != 0 && firstDigit != 1 {
|
||||
return Err(Exceptions::IllegalArgumentException(
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"Number system must be 0 or 1".to_owned(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
let checkDigit = contents.chars().nth(7).unwrap().to_digit(10).unwrap() as usize; //Character.digit(contents.charAt(7), 10);
|
||||
@@ -109,7 +105,7 @@ impl OneDimensionalCodeWriter for UPCEWriter {
|
||||
) as usize;
|
||||
}
|
||||
|
||||
Self::appendPattern(&mut result, pos, &upc_ean_reader::END_PATTERN, false) as usize;
|
||||
Self::appendPattern(&mut result, pos, &upc_ean_reader::END_PATTERN, false);
|
||||
|
||||
Ok(result.to_vec())
|
||||
}
|
||||
|
||||
@@ -26,16 +26,10 @@ use super::{upc_ean_reader, UPCEANReader, STAND_IN};
|
||||
/**
|
||||
* @see UPCEANExtension5Support
|
||||
*/
|
||||
#[derive(Default)]
|
||||
pub struct UPCEANExtension2Support {
|
||||
decodeMiddleCounters: [u32; 4],
|
||||
}
|
||||
impl Default for UPCEANExtension2Support {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
decodeMiddleCounters: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl UPCEANExtension2Support {
|
||||
pub fn decodeRow(
|
||||
@@ -114,12 +108,12 @@ impl UPCEANExtension2Support {
|
||||
}
|
||||
|
||||
if resultString.chars().count() != 2 {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
if resultString.parse::<u32>().unwrap() % 4 != checkParity {
|
||||
// if (Integer.parseInt(resultString.toString()) % 4 != checkParity) {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
Ok(rowOffset as u32)
|
||||
|
||||
@@ -26,17 +26,12 @@ use super::{upc_ean_reader, UPCEANReader, STAND_IN};
|
||||
/**
|
||||
* @see UPCEANExtension2Support
|
||||
*/
|
||||
#[derive(Default)]
|
||||
pub struct UPCEANExtension5Support {
|
||||
//decodeMiddleCounters : [u32;4],
|
||||
// decodeRowStringBuffer : String,
|
||||
}
|
||||
|
||||
impl Default for UPCEANExtension5Support {
|
||||
fn default() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl UPCEANExtension5Support {
|
||||
const CHECK_DIGIT_ENCODINGS: [usize; 10] =
|
||||
[0x18, 0x14, 0x12, 0x11, 0x0C, 0x06, 0x03, 0x0A, 0x09, 0x05];
|
||||
@@ -83,7 +78,7 @@ impl UPCEANExtension5Support {
|
||||
// counters[2] = 0;
|
||||
// counters[3] = 0;
|
||||
let end = row.getSize();
|
||||
let mut rowOffset = startRange[1] as usize;
|
||||
let mut rowOffset = startRange[1];
|
||||
|
||||
let mut lgPatternFound = 0;
|
||||
|
||||
@@ -115,12 +110,12 @@ impl UPCEANExtension5Support {
|
||||
}
|
||||
|
||||
if resultString.chars().count() != 5 {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
let checkDigit = Self::determineCheckDigit(lgPatternFound)?;
|
||||
if Self::extensionChecksum(resultString) != checkDigit as u32 {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
Ok(rowOffset as u32)
|
||||
@@ -146,7 +141,7 @@ impl UPCEANExtension5Support {
|
||||
i -= 2;
|
||||
}
|
||||
sum *= 3;
|
||||
return sum % 10;
|
||||
sum % 10
|
||||
}
|
||||
|
||||
fn determineCheckDigit(lgPatternFound: usize) -> Result<usize, Exceptions> {
|
||||
@@ -156,7 +151,7 @@ impl UPCEANExtension5Support {
|
||||
return Ok(d);
|
||||
}
|
||||
}
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -184,7 +179,7 @@ impl UPCEANExtension5Support {
|
||||
}
|
||||
|
||||
fn parseExtension5String(raw: &str) -> Option<String> {
|
||||
let currency = match raw.chars().nth(0).unwrap() {
|
||||
let currency = match raw.chars().next().unwrap() {
|
||||
'0' => "£",
|
||||
'5' => "$",
|
||||
'9' => {
|
||||
|
||||
@@ -18,19 +18,12 @@ use crate::{common::BitArray, Exceptions, RXingResult};
|
||||
|
||||
use super::{UPCEANExtension2Support, UPCEANExtension5Support, UPCEANReader, STAND_IN};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct UPCEANExtensionSupport {
|
||||
twoSupport: UPCEANExtension2Support,
|
||||
fiveSupport: UPCEANExtension5Support,
|
||||
}
|
||||
|
||||
impl Default for UPCEANExtensionSupport {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
twoSupport: Default::default(),
|
||||
fiveSupport: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl UPCEANExtensionSupport {
|
||||
const EXTENSION_START_PATTERN: [u32; 3] = [1, 1, 2];
|
||||
|
||||
|
||||
@@ -203,16 +203,16 @@ pub trait UPCEANReader: OneDReader {
|
||||
let end = endRange[1];
|
||||
let quietEnd = end + (end - endRange[0]);
|
||||
if quietEnd >= row.getSize() || !row.isRange(end, quietEnd, false)? {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
|
||||
let resultString = result;
|
||||
// UPC/EAN should never be less than 8 chars anyway
|
||||
if resultString.chars().count() < 8 {
|
||||
return Err(Exceptions::FormatException("".to_owned()));
|
||||
return Err(Exceptions::FormatException(None));
|
||||
}
|
||||
if !self.checkChecksum(&resultString)? {
|
||||
return Err(Exceptions::ChecksumException("".to_owned()));
|
||||
return Err(Exceptions::ChecksumException(None));
|
||||
}
|
||||
|
||||
let left = (startGuardRange[1] + startGuardRange[0]) as f32 / 2.0;
|
||||
@@ -273,7 +273,7 @@ pub trait UPCEANReader: OneDReader {
|
||||
}
|
||||
}
|
||||
if !valid {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
}
|
||||
}
|
||||
// let allowedExtensions =
|
||||
@@ -287,7 +287,7 @@ pub trait UPCEANReader: OneDReader {
|
||||
// }
|
||||
// }
|
||||
// if (!valid) {
|
||||
// return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
// return Err(Exceptions::NotFoundException(None));
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -335,7 +335,7 @@ pub trait UPCEANReader: OneDReader {
|
||||
return Ok(false);
|
||||
}
|
||||
let char_in_question = s.chars().nth(length - 1).unwrap();
|
||||
let check = char_in_question.is_digit(10);
|
||||
let check = char_in_question.is_ascii_digit();
|
||||
// let check = Character.digit(s.charAt(length - 1), 10);
|
||||
|
||||
let check_against = &s[..length - 1]; //s.subSequence(0, length - 1);
|
||||
@@ -356,8 +356,8 @@ pub trait UPCEANReader: OneDReader {
|
||||
while i >= 0 {
|
||||
// for (int i = length - 1; i >= 0; i -= 2) {
|
||||
let digit = (s.chars().nth(i as usize).unwrap() as i32) - ('0' as i32);
|
||||
if digit < 0 || digit > 9 {
|
||||
return Err(Exceptions::FormatException("".to_owned()));
|
||||
if !(0..=9).contains(&digit) {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
}
|
||||
sum += digit;
|
||||
|
||||
@@ -368,8 +368,8 @@ pub trait UPCEANReader: OneDReader {
|
||||
while i >= 0 {
|
||||
// for (int i = length - 2; i >= 0; i -= 2) {
|
||||
let digit = (s.chars().nth(i as usize).unwrap() as i32) - ('0' as i32);
|
||||
if digit < 0 || digit > 9 {
|
||||
return Err(Exceptions::FormatException("".to_owned()));
|
||||
if !(0..=9).contains(&digit) {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
}
|
||||
sum += digit;
|
||||
|
||||
@@ -456,7 +456,7 @@ pub trait UPCEANReader: OneDReader {
|
||||
}
|
||||
}
|
||||
|
||||
Err(Exceptions::NotFoundException("".to_owned()))
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -482,9 +482,8 @@ pub trait UPCEANReader: OneDReader {
|
||||
let mut bestVariance = MAX_AVG_VARIANCE; // worst variance we'll accept
|
||||
let mut bestMatch = -1_isize;
|
||||
let max = patterns.len();
|
||||
for i in 0..max {
|
||||
for (i, pattern) in patterns.iter().enumerate().take(max) {
|
||||
// for (int i = 0; i < max; i++) {
|
||||
let pattern = &patterns[i];
|
||||
let variance: f32 =
|
||||
one_d_reader::patternMatchVariance(counters, pattern, MAX_INDIVIDUAL_VARIANCE);
|
||||
if variance < bestVariance {
|
||||
@@ -495,7 +494,7 @@ pub trait UPCEANReader: OneDReader {
|
||||
if bestMatch >= 0 {
|
||||
Ok(bestMatch as usize)
|
||||
} else {
|
||||
Err(Exceptions::NotFoundException("".to_owned()))
|
||||
Err(Exceptions::NotFoundException(None))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user