mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 04:12:34 +00:00
refactor to use common result type
This commit is contained in:
@@ -16,7 +16,10 @@
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::{common::BitMatrix, Exceptions, RXingResultPoint, ResultPoint};
|
||||
use crate::{
|
||||
common::{BitMatrix, Result},
|
||||
Exceptions, RXingResultPoint, ResultPoint,
|
||||
};
|
||||
|
||||
/**
|
||||
* @author Guenther Grau
|
||||
@@ -40,7 +43,7 @@ impl BoundingBox {
|
||||
bottomLeft: Option<RXingResultPoint>,
|
||||
topRight: Option<RXingResultPoint>,
|
||||
bottomRight: Option<RXingResultPoint>,
|
||||
) -> Result<BoundingBox, Exceptions> {
|
||||
) -> Result<BoundingBox> {
|
||||
let leftUnspecified = topLeft.is_none() || bottomLeft.is_none();
|
||||
let rightUnspecified = topRight.is_none() || bottomRight.is_none();
|
||||
if leftUnspecified && rightUnspecified {
|
||||
@@ -100,7 +103,7 @@ impl BoundingBox {
|
||||
pub fn merge(
|
||||
leftBox: Option<BoundingBox>,
|
||||
rightBox: Option<BoundingBox>,
|
||||
) -> Result<BoundingBox, Exceptions> {
|
||||
) -> Result<BoundingBox> {
|
||||
if leftBox.is_none() {
|
||||
return Ok(rightBox
|
||||
.as_ref()
|
||||
@@ -130,7 +133,7 @@ impl BoundingBox {
|
||||
missingStartRows: u32,
|
||||
missingEndRows: u32,
|
||||
isLeft: bool,
|
||||
) -> Result<BoundingBox, Exceptions> {
|
||||
) -> Result<BoundingBox> {
|
||||
let mut newTopLeft = self.topLeft;
|
||||
let mut newBottomLeft = self.bottomLeft;
|
||||
let mut newTopRight = self.topRight;
|
||||
|
||||
@@ -19,7 +19,7 @@ use num::{self, bigint::ToBigUint, BigUint};
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::{
|
||||
common::{DecoderRXingResult, ECIStringBuilder},
|
||||
common::{DecoderRXingResult, ECIStringBuilder, Result},
|
||||
pdf417::PDF417RXingResultMetadata,
|
||||
Exceptions,
|
||||
};
|
||||
@@ -106,7 +106,7 @@ static EXP900: Lazy<Vec<BigUint>> = Lazy::new(|| {
|
||||
|
||||
const NUMBER_OF_SEQUENCE_CODEWORDS: usize = 2;
|
||||
|
||||
pub fn decode(codewords: &[u32], ecLevel: &str) -> Result<DecoderRXingResult, Exceptions> {
|
||||
pub fn decode(codewords: &[u32], ecLevel: &str) -> Result<DecoderRXingResult> {
|
||||
let mut result = ECIStringBuilder::with_capacity(codewords.len() * 2);
|
||||
let mut codeIndex = textCompaction(codewords, 1, &mut result)?;
|
||||
let mut resultMetadata = PDF417RXingResultMetadata::default();
|
||||
@@ -182,7 +182,7 @@ pub fn decodeMacroBlock(
|
||||
codewords: &[u32],
|
||||
codeIndex: usize,
|
||||
resultMetadata: &mut PDF417RXingResultMetadata,
|
||||
) -> Result<usize, Exceptions> {
|
||||
) -> Result<usize> {
|
||||
let mut codeIndex = codeIndex;
|
||||
if codeIndex + NUMBER_OF_SEQUENCE_CODEWORDS > codewords[0] as usize {
|
||||
// we must have at least two bytes left for the segment index
|
||||
@@ -332,7 +332,7 @@ fn textCompaction(
|
||||
codewords: &[u32],
|
||||
codeIndex: usize,
|
||||
result: &mut ECIStringBuilder,
|
||||
) -> Result<usize, Exceptions> {
|
||||
) -> Result<usize> {
|
||||
let mut codeIndex = codeIndex;
|
||||
// 2 character per codeword
|
||||
let mut textCompactionData = vec![0; (codewords[0] as usize - codeIndex) * 2];
|
||||
@@ -610,7 +610,7 @@ fn byteCompaction(
|
||||
codewords: &[u32],
|
||||
codeIndex: usize,
|
||||
result: &mut ECIStringBuilder,
|
||||
) -> Result<usize, Exceptions> {
|
||||
) -> Result<usize> {
|
||||
let mut end = false;
|
||||
let mut codeIndex = codeIndex;
|
||||
|
||||
@@ -681,7 +681,7 @@ fn numericCompaction(
|
||||
codewords: &[u32],
|
||||
codeIndex: usize,
|
||||
result: &mut ECIStringBuilder,
|
||||
) -> Result<usize, Exceptions> {
|
||||
) -> Result<usize> {
|
||||
let mut count = 0;
|
||||
let mut end = false;
|
||||
let mut codeIndex = codeIndex;
|
||||
@@ -769,7 +769,7 @@ fn numericCompaction(
|
||||
|
||||
Remove leading 1 => RXingResult is 000213298174000
|
||||
*/
|
||||
fn decodeBase900toBase10(codewords: &[u32], count: usize) -> Result<String, Exceptions> {
|
||||
fn decodeBase900toBase10(codewords: &[u32], count: usize) -> Result<String> {
|
||||
let mut result = 0
|
||||
.to_biguint()
|
||||
.ok_or(Exceptions::ArithmeticException(None))?;
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::{
|
||||
common::Result,
|
||||
pdf417::{decoder::ec::ModulusGF, pdf_417_common::NUMBER_OF_CODEWORDS},
|
||||
Exceptions,
|
||||
};
|
||||
@@ -45,11 +46,7 @@ static FLD_INTERIOR: Lazy<ModulusGF> = Lazy::new(|| ModulusGF::new(NUMBER_OF_COD
|
||||
* @return number of errors
|
||||
* @throws ChecksumException if errors cannot be corrected, maybe because of too many errors
|
||||
*/
|
||||
pub fn decode(
|
||||
received: &mut [u32],
|
||||
numECCodewords: u32,
|
||||
erasures: &mut [u32],
|
||||
) -> Result<usize, Exceptions> {
|
||||
pub fn decode(received: &mut [u32], numECCodewords: u32, erasures: &mut [u32]) -> Result<usize> {
|
||||
let field: &'static ModulusGF = &FLD_INTERIOR;
|
||||
let poly = ModulusPoly::new(field, received.to_vec())?;
|
||||
let mut S = vec![0u32; numECCodewords as usize];
|
||||
@@ -117,7 +114,7 @@ fn runEuclideanAlgorithm(
|
||||
b: Rc<ModulusPoly>,
|
||||
R: u32,
|
||||
field: &'static ModulusGF,
|
||||
) -> Result<[Rc<ModulusPoly>; 2], Exceptions> {
|
||||
) -> Result<[Rc<ModulusPoly>; 2]> {
|
||||
// Assume a's degree is >= b's
|
||||
let mut a = a;
|
||||
let mut b = b;
|
||||
@@ -172,10 +169,7 @@ fn runEuclideanAlgorithm(
|
||||
Ok([sigma, omega])
|
||||
}
|
||||
|
||||
fn findErrorLocations(
|
||||
errorLocator: Rc<ModulusPoly>,
|
||||
field: &ModulusGF,
|
||||
) -> Result<Vec<u32>, Exceptions> {
|
||||
fn findErrorLocations(errorLocator: Rc<ModulusPoly>, field: &ModulusGF) -> Result<Vec<u32>> {
|
||||
// This is a direct application of Chien's search
|
||||
let numErrors = errorLocator.getDegree();
|
||||
let mut result = vec![0u32; numErrors as usize];
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
use rand::Rng;
|
||||
|
||||
use crate::Exceptions;
|
||||
use crate::common::Result;
|
||||
|
||||
use super::{
|
||||
abstract_error_correction_test_case::{corrupt, getRandom},
|
||||
@@ -99,11 +99,11 @@ fn testTooManyErrors() {
|
||||
// }
|
||||
}
|
||||
|
||||
fn checkDecode(received: &mut [u32]) -> Result<(), Exceptions> {
|
||||
fn checkDecode(received: &mut [u32]) -> Result<()> {
|
||||
checkDecodeErasures(received, &mut [0_u32; 0])
|
||||
}
|
||||
|
||||
fn checkDecodeErasures(received: &mut [u32], erasures: &mut [u32]) -> Result<(), Exceptions> {
|
||||
fn checkDecodeErasures(received: &mut [u32], erasures: &mut [u32]) -> Result<()> {
|
||||
decode(received, ECC_BYTES as u32, erasures)?;
|
||||
// ec.decode(received, ECC_BYTES, erasures);
|
||||
for i in 0..PDF417_TEST.len() {
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
//public static final ModulusGF PDF417_GF = new ModulusGF(PDF417Common.NUMBER_OF_CODEWORDS, 3);
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
/**
|
||||
@@ -75,7 +76,7 @@ impl ModulusGF {
|
||||
self.expTable[a as usize]
|
||||
}
|
||||
|
||||
pub fn log(&self, a: u32) -> Result<u32, Exceptions> {
|
||||
pub fn log(&self, a: u32) -> Result<u32> {
|
||||
if a == 0 {
|
||||
Err(Exceptions::ArithmeticException(None))
|
||||
} else {
|
||||
@@ -83,7 +84,7 @@ impl ModulusGF {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn inverse(&self, a: u32) -> Result<u32, Exceptions> {
|
||||
pub fn inverse(&self, a: u32) -> Result<u32> {
|
||||
if a == 0 {
|
||||
Err(Exceptions::ArithmeticException(None))
|
||||
} else {
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
use super::ModulusGF;
|
||||
@@ -31,10 +32,7 @@ pub struct ModulusPoly {
|
||||
// one: Option<Rc<ModulusPoly>>,
|
||||
}
|
||||
impl ModulusPoly {
|
||||
pub fn new(
|
||||
field: &'static ModulusGF,
|
||||
coefficients: Vec<u32>,
|
||||
) -> Result<ModulusPoly, Exceptions> {
|
||||
pub fn new(field: &'static ModulusGF, coefficients: Vec<u32>) -> Result<ModulusPoly> {
|
||||
if coefficients.is_empty() {
|
||||
return Err(Exceptions::IllegalArgumentException(None));
|
||||
}
|
||||
@@ -124,7 +122,7 @@ impl ModulusPoly {
|
||||
result
|
||||
}
|
||||
|
||||
pub fn add(&self, other: Rc<ModulusPoly>) -> Result<Rc<ModulusPoly>, Exceptions> {
|
||||
pub fn add(&self, other: Rc<ModulusPoly>) -> Result<Rc<ModulusPoly>> {
|
||||
if self.field != other.field {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"ModulusPolys do not have same ModulusGF field".to_owned(),
|
||||
@@ -158,7 +156,7 @@ impl ModulusPoly {
|
||||
Ok(Rc::new(ModulusPoly::new(self.field, sumDiff)?))
|
||||
}
|
||||
|
||||
pub fn subtract(&self, other: Rc<ModulusPoly>) -> Result<Rc<ModulusPoly>, Exceptions> {
|
||||
pub fn subtract(&self, other: Rc<ModulusPoly>) -> Result<Rc<ModulusPoly>> {
|
||||
if self.field != other.field {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"ModulusPolys do not have same ModulusGF field".to_owned(),
|
||||
@@ -170,7 +168,7 @@ impl ModulusPoly {
|
||||
self.add(other.negative())
|
||||
}
|
||||
|
||||
pub fn multiply(&self, other: Rc<ModulusPoly>) -> Result<Rc<ModulusPoly>, Exceptions> {
|
||||
pub fn multiply(&self, other: Rc<ModulusPoly>) -> Result<Rc<ModulusPoly>> {
|
||||
if !(self.field == other.field) {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"ModulusPolys do not have same ModulusGF field".to_owned(),
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::{
|
||||
common::{BitMatrix, DecoderRXingResult},
|
||||
common::{BitMatrix, DecoderRXingResult, Result},
|
||||
pdf417::pdf_417_common,
|
||||
Exceptions, RXingResultPoint, ResultPoint,
|
||||
};
|
||||
@@ -50,7 +50,7 @@ pub fn decode(
|
||||
imageBottomRight: Option<RXingResultPoint>,
|
||||
minCodewordWidth: u32,
|
||||
maxCodewordWidth: u32,
|
||||
) -> Result<DecoderRXingResult, Exceptions> {
|
||||
) -> Result<DecoderRXingResult> {
|
||||
let mut minCodewordWidth = minCodewordWidth;
|
||||
let mut maxCodewordWidth = maxCodewordWidth;
|
||||
let mut boundingBox = Rc::new(BoundingBox::new(
|
||||
@@ -180,7 +180,7 @@ pub fn decode(
|
||||
fn merge<'a, T: DetectionRXingResultRowIndicatorColumn>(
|
||||
leftRowIndicatorColumn: &'a mut Option<T>,
|
||||
rightRowIndicatorColumn: &'a mut Option<T>,
|
||||
) -> Result<Option<DetectionRXingResult>, Exceptions> {
|
||||
) -> Result<Option<DetectionRXingResult>> {
|
||||
if leftRowIndicatorColumn.is_none() && rightRowIndicatorColumn.is_none() {
|
||||
return Ok(None);
|
||||
}
|
||||
@@ -201,7 +201,7 @@ fn merge<'a, T: DetectionRXingResultRowIndicatorColumn>(
|
||||
|
||||
fn adjustBoundingBox<T: DetectionRXingResultRowIndicatorColumn>(
|
||||
rowIndicatorColumn: &mut Option<T>,
|
||||
) -> Result<Option<BoundingBox>, Exceptions> {
|
||||
) -> Result<Option<BoundingBox>> {
|
||||
if rowIndicatorColumn.is_none() {
|
||||
return Ok(None);
|
||||
}
|
||||
@@ -403,7 +403,7 @@ fn getRowIndicatorColumn<'a>(
|
||||
fn adjustCodewordCount(
|
||||
detectionRXingResult: &DetectionRXingResult,
|
||||
barcodeMatrix: &mut [Vec<BarcodeValue>],
|
||||
) -> Result<(), Exceptions> {
|
||||
) -> Result<()> {
|
||||
let barcodeMatrix01 = &mut barcodeMatrix[0][1];
|
||||
let numberOfCodewords = barcodeMatrix01.getValue();
|
||||
let calculatedNumberOfCodewords = (detectionRXingResult.getBarcodeColumnCount() as isize
|
||||
@@ -426,7 +426,7 @@ fn adjustCodewordCount(
|
||||
|
||||
fn createDecoderRXingResult(
|
||||
detectionRXingResult: &mut DetectionRXingResult,
|
||||
) -> Result<DecoderRXingResult, Exceptions> {
|
||||
) -> Result<DecoderRXingResult> {
|
||||
let mut barcodeMatrix = createBarcodeMatrix(detectionRXingResult);
|
||||
adjustCodewordCount(detectionRXingResult, &mut barcodeMatrix)?;
|
||||
let mut erasures = Vec::new();
|
||||
@@ -488,7 +488,7 @@ fn createDecoderRXingResultFromAmbiguousValues(
|
||||
erasureArray: &mut [u32],
|
||||
ambiguousIndexes: &mut [u32],
|
||||
ambiguousIndexValues: &[Vec<u32>],
|
||||
) -> Result<DecoderRXingResult, Exceptions> {
|
||||
) -> Result<DecoderRXingResult> {
|
||||
let mut ambiguousIndexCount = vec![0; ambiguousIndexes.len()];
|
||||
|
||||
let mut tries = 100;
|
||||
@@ -843,7 +843,7 @@ fn decodeCodewords(
|
||||
codewords: &mut [u32],
|
||||
ecLevel: u32,
|
||||
erasures: &mut [u32],
|
||||
) -> Result<DecoderRXingResult, Exceptions> {
|
||||
) -> Result<DecoderRXingResult> {
|
||||
if codewords.is_empty() {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
}
|
||||
@@ -874,7 +874,7 @@ fn correctErrors(
|
||||
codewords: &mut [u32],
|
||||
erasures: &mut [u32],
|
||||
numECCodewords: u32,
|
||||
) -> Result<usize, Exceptions> {
|
||||
) -> Result<usize> {
|
||||
if !erasures.is_empty() && erasures.len() as u32 > numECCodewords / 2 + MAX_ERRORS
|
||||
/*|| numECCodewords < 0*/
|
||||
|| numECCodewords > MAX_EC_CODEWORDS
|
||||
@@ -888,7 +888,7 @@ fn correctErrors(
|
||||
/**
|
||||
* Verify that all is OK with the codeword array.
|
||||
*/
|
||||
fn verifyCodewordCount(codewords: &mut [u32], numECCodewords: u32) -> Result<(), Exceptions> {
|
||||
fn verifyCodewordCount(codewords: &mut [u32], numECCodewords: u32) -> Result<()> {
|
||||
if codewords.len() < 4 {
|
||||
// Codeword array size should be at least 4 allowing for
|
||||
// Count CW, At least one Data CW, Error Correction CW, Error Correction CW
|
||||
|
||||
@@ -15,8 +15,8 @@
|
||||
*/
|
||||
|
||||
use crate::{
|
||||
common::BitMatrix, BinaryBitmap, DecodingHintDictionary, Exceptions, RXingResultPoint,
|
||||
ResultPoint,
|
||||
common::{BitMatrix, Result},
|
||||
BinaryBitmap, DecodingHintDictionary, Exceptions, RXingResultPoint, ResultPoint,
|
||||
};
|
||||
|
||||
use std::borrow::Cow;
|
||||
@@ -68,7 +68,7 @@ pub fn detect_with_hints(
|
||||
image: &mut BinaryBitmap,
|
||||
_hints: &DecodingHintDictionary,
|
||||
multiple: bool,
|
||||
) -> Result<PDF417DetectorRXingResult, Exceptions> {
|
||||
) -> Result<PDF417DetectorRXingResult> {
|
||||
// TODO detection improvement, tryHarder could try several different luminance thresholds/blackpoints or even
|
||||
// different binarizers
|
||||
//boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
|
||||
@@ -101,7 +101,7 @@ pub fn detect_with_hints(
|
||||
* @param rotation the degrees of rotation to apply
|
||||
* @return BitMatrix with applied rotation
|
||||
*/
|
||||
fn applyRotation(matrix: &BitMatrix, rotation: u32) -> Result<Cow<BitMatrix>, Exceptions> {
|
||||
fn applyRotation(matrix: &BitMatrix, rotation: u32) -> Result<Cow<BitMatrix>> {
|
||||
if rotation % 360 == 0 {
|
||||
Ok(Cow::Borrowed(matrix))
|
||||
} else {
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
use encoding::EncodingRef;
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
use super::{
|
||||
@@ -133,7 +134,7 @@ impl PDF417 {
|
||||
r: u32,
|
||||
errorCorrectionLevel: u32,
|
||||
logic: &mut BarcodeMatrix,
|
||||
) -> Result<(), Exceptions> {
|
||||
) -> Result<()> {
|
||||
let mut idx = 0;
|
||||
for y in 0..r {
|
||||
let cluster = y as usize % 3;
|
||||
@@ -184,11 +185,7 @@ impl PDF417 {
|
||||
* @param errorCorrectionLevel PDF417 error correction level to use
|
||||
* @throws WriterException if the contents cannot be encoded in this format
|
||||
*/
|
||||
pub fn generateBarcodeLogic(
|
||||
&mut self,
|
||||
msg: &str,
|
||||
errorCorrectionLevel: u32,
|
||||
) -> Result<(), Exceptions> {
|
||||
pub fn generateBarcodeLogic(&mut self, msg: &str, errorCorrectionLevel: u32) -> Result<()> {
|
||||
self.generateBarcodeLogicWithAutoECI(msg, errorCorrectionLevel, false)
|
||||
}
|
||||
|
||||
@@ -203,7 +200,7 @@ impl PDF417 {
|
||||
msg: &str,
|
||||
errorCorrectionLevel: u32,
|
||||
autoECI: bool,
|
||||
) -> Result<(), Exceptions> {
|
||||
) -> Result<()> {
|
||||
//1. step: High-level encoding
|
||||
let errorCorrectionCodeWords =
|
||||
pdf_417_error_correction::getErrorCorrectionCodewordCount(errorCorrectionLevel)?;
|
||||
@@ -272,7 +269,7 @@ impl PDF417 {
|
||||
&self,
|
||||
sourceCodeWords: u32,
|
||||
errorCorrectionCodeWords: u32,
|
||||
) -> Result<[u32; 2], Exceptions> {
|
||||
) -> Result<[u32; 2]> {
|
||||
let mut ratio = 0.0;
|
||||
let mut dimension = None;
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
*/
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
/**
|
||||
@@ -117,7 +118,7 @@ static EC_COEFFICIENTS: Lazy<[Vec<u32>; 9]> = Lazy::new(|| {
|
||||
* @param errorCorrectionLevel the error correction level (0-8)
|
||||
* @return the number of codewords generated for error correction
|
||||
*/
|
||||
pub fn getErrorCorrectionCodewordCount(errorCorrectionLevel: u32) -> Result<u32, Exceptions> {
|
||||
pub fn getErrorCorrectionCodewordCount(errorCorrectionLevel: u32) -> Result<u32> {
|
||||
if errorCorrectionLevel > 8 {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"Error correction level must be between 0 and 8!".to_owned(),
|
||||
@@ -133,7 +134,7 @@ pub fn getErrorCorrectionCodewordCount(errorCorrectionLevel: u32) -> Result<u32,
|
||||
* @param n the number of data codewords
|
||||
* @return the recommended minimum error correction level
|
||||
*/
|
||||
pub fn getRecommendedMinimumErrorCorrectionLevel(n: u32) -> Result<u32, Exceptions> {
|
||||
pub fn getRecommendedMinimumErrorCorrectionLevel(n: u32) -> Result<u32> {
|
||||
if n == 0 {
|
||||
Err(Exceptions::IllegalArgumentException(Some(
|
||||
"n must be > 0".to_owned(),
|
||||
@@ -160,10 +161,7 @@ pub fn getRecommendedMinimumErrorCorrectionLevel(n: u32) -> Result<u32, Exceptio
|
||||
* @param errorCorrectionLevel the error correction level (0-8)
|
||||
* @return the String representing the error correction codewords
|
||||
*/
|
||||
pub fn generateErrorCorrection(
|
||||
dataCodewords: &str,
|
||||
errorCorrectionLevel: u32,
|
||||
) -> Result<String, Exceptions> {
|
||||
pub fn generateErrorCorrection(dataCodewords: &str, errorCorrectionLevel: u32) -> Result<String> {
|
||||
let k = getErrorCorrectionCodewordCount(errorCorrectionLevel)?;
|
||||
let mut e = vec![0 as char; k as usize]; //new char[k];
|
||||
let sld = dataCodewords.chars().count();
|
||||
|
||||
@@ -23,7 +23,7 @@ use std::{any::TypeId, fmt::Display, str::FromStr};
|
||||
use encoding::EncodingRef;
|
||||
|
||||
use crate::{
|
||||
common::{CharacterSetECI, ECIInput, MinimalECIInput},
|
||||
common::{CharacterSetECI, ECIInput, MinimalECIInput, Result},
|
||||
Exceptions,
|
||||
};
|
||||
|
||||
@@ -176,7 +176,7 @@ pub fn encodeHighLevel(
|
||||
compaction: Compaction,
|
||||
encoding: Option<EncodingRef>,
|
||||
autoECI: bool,
|
||||
) -> Result<String, Exceptions> {
|
||||
) -> Result<String> {
|
||||
let mut encoding = encoding;
|
||||
if msg.is_empty() {
|
||||
return Err(Exceptions::WriterException(Some(
|
||||
@@ -368,7 +368,7 @@ fn encodeText<T: ECIInput + ?Sized>(
|
||||
count: u32,
|
||||
sb: &mut String,
|
||||
initialSubmode: u32,
|
||||
) -> Result<u32, Exceptions> {
|
||||
) -> Result<u32> {
|
||||
let mut tmp = String::with_capacity(count as usize);
|
||||
let mut submode = initialSubmode;
|
||||
let mut idx = 0;
|
||||
@@ -531,7 +531,7 @@ fn encodeMultiECIBinary<T: ECIInput + ?Sized>(
|
||||
count: u32,
|
||||
startmode: u32,
|
||||
sb: &mut String,
|
||||
) -> Result<(), Exceptions> {
|
||||
) -> Result<()> {
|
||||
let end = (startpos + count).min(input.length() as u32);
|
||||
let mut localStart = startpos;
|
||||
loop {
|
||||
@@ -570,11 +570,7 @@ fn encodeMultiECIBinary<T: ECIInput + ?Sized>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn subBytes<T: ECIInput + ?Sized>(
|
||||
input: &Box<T>,
|
||||
start: u32,
|
||||
end: u32,
|
||||
) -> Result<Vec<u8>, Exceptions> {
|
||||
pub fn subBytes<T: ECIInput + ?Sized>(input: &Box<T>, start: u32, end: u32) -> Result<Vec<u8>> {
|
||||
let count = (end - start) as usize;
|
||||
let mut result = vec![0_u8; count];
|
||||
for i in start as usize..end as usize {
|
||||
@@ -600,7 +596,7 @@ fn encodeBinary(
|
||||
count: u32,
|
||||
startmode: u32,
|
||||
sb: &mut String,
|
||||
) -> Result<(), Exceptions> {
|
||||
) -> Result<()> {
|
||||
if count == 1 && startmode == TEXT_COMPACTION {
|
||||
sb.push(char::from_u32(SHIFT_TO_BYTE).ok_or(Exceptions::ParseException(None))?);
|
||||
} else if (count % 6) == 0 {
|
||||
@@ -641,7 +637,7 @@ fn encodeNumeric<T: ECIInput + ?Sized>(
|
||||
startpos: u32,
|
||||
count: u32,
|
||||
sb: &mut String,
|
||||
) -> Result<(), Exceptions> {
|
||||
) -> Result<()> {
|
||||
let mut idx = 0;
|
||||
let mut tmp = String::with_capacity(count as usize / 3 + 1);
|
||||
let NUM900: num::BigUint = num::BigUint::from(900_u16); //.ok_or(Exceptions::ParseException(None))?;
|
||||
@@ -723,7 +719,7 @@ fn isText(ch: char) -> bool {
|
||||
fn determineConsecutiveDigitCount<T: ECIInput + ?Sized>(
|
||||
input: &Box<T>,
|
||||
startpos: u32,
|
||||
) -> Result<u32, Exceptions> {
|
||||
) -> Result<u32> {
|
||||
let mut count = 0;
|
||||
let len = input.length();
|
||||
let mut idx = startpos as usize;
|
||||
@@ -747,7 +743,7 @@ fn determineConsecutiveDigitCount<T: ECIInput + ?Sized>(
|
||||
fn determineConsecutiveTextCount<T: ECIInput + ?Sized>(
|
||||
input: &Box<T>,
|
||||
startpos: u32,
|
||||
) -> Result<u32, Exceptions> {
|
||||
) -> Result<u32> {
|
||||
let len = input.length();
|
||||
let mut idx = startpos as usize;
|
||||
while idx < len {
|
||||
@@ -789,7 +785,7 @@ fn determineConsecutiveBinaryCount<T: ECIInput + ?Sized + 'static>(
|
||||
input: &Box<T>,
|
||||
startpos: u32,
|
||||
encoding: Option<EncodingRef>,
|
||||
) -> Result<u32, Exceptions> {
|
||||
) -> Result<u32> {
|
||||
let len = input.length();
|
||||
let mut idx = startpos as usize;
|
||||
while idx < len {
|
||||
@@ -834,7 +830,7 @@ fn determineConsecutiveBinaryCount<T: ECIInput + ?Sized + 'static>(
|
||||
Ok(idx as u32 - startpos)
|
||||
}
|
||||
|
||||
fn encodingECI(eci: i32, sb: &mut String) -> Result<(), Exceptions> {
|
||||
fn encodingECI(eci: i32, sb: &mut String) -> Result<()> {
|
||||
if (0..900).contains(&eci) {
|
||||
sb.push(char::from_u32(ECI_CHARSET).ok_or(Exceptions::ParseException(None))?);
|
||||
sb.push(char::from_u32(eci as u32).ok_or(Exceptions::ParseException(None))?);
|
||||
@@ -859,27 +855,27 @@ impl ECIInput for NoECIInput {
|
||||
self.0.chars().count()
|
||||
}
|
||||
|
||||
fn charAt(&self, index: usize) -> Result<char, Exceptions> {
|
||||
fn charAt(&self, index: usize) -> Result<char> {
|
||||
self.0
|
||||
.chars()
|
||||
.nth(index)
|
||||
.ok_or(Exceptions::IndexOutOfBoundsException(None))
|
||||
}
|
||||
|
||||
fn subSequence(&self, start: usize, end: usize) -> Result<Vec<char>, Exceptions> {
|
||||
fn subSequence(&self, start: usize, end: usize) -> Result<Vec<char>> {
|
||||
let res: Vec<char> = self.0.chars().skip(start).take(end - start).collect();
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
fn isECI(&self, _index: u32) -> Result<bool, Exceptions> {
|
||||
fn isECI(&self, _index: u32) -> Result<bool> {
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
fn getECIValue(&self, _index: usize) -> Result<i32, Exceptions> {
|
||||
fn getECIValue(&self, _index: usize) -> Result<i32> {
|
||||
Ok(-1)
|
||||
}
|
||||
|
||||
fn haveNCharacters(&self, index: usize, n: usize) -> Result<bool, Exceptions> {
|
||||
fn haveNCharacters(&self, index: usize, n: usize) -> Result<bool> {
|
||||
Ok(index + n <= self.0.len())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
use encoding::EncodingRef;
|
||||
|
||||
use crate::Exceptions;
|
||||
use crate::common::Result;
|
||||
|
||||
use super::{pdf_417_high_level_encoder, Compaction};
|
||||
|
||||
@@ -29,6 +29,6 @@ pub fn encodeHighLevel(
|
||||
compaction: Compaction,
|
||||
encoding: Option<EncodingRef>,
|
||||
autoECI: bool,
|
||||
) -> Result<String, Exceptions> {
|
||||
) -> Result<String> {
|
||||
pdf_417_high_level_encoder::encodeHighLevel(msg, compaction, encoding, autoECI)
|
||||
}
|
||||
|
||||
@@ -17,9 +17,9 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{
|
||||
multi::MultipleBarcodeReader, BarcodeFormat, BinaryBitmap, DecodingHintDictionary, Exceptions,
|
||||
RXingResult, RXingResultMetadataType, RXingResultMetadataValue, RXingResultPoint, Reader,
|
||||
ResultPoint,
|
||||
common::Result, multi::MultipleBarcodeReader, BarcodeFormat, BinaryBitmap,
|
||||
DecodingHintDictionary, Exceptions, RXingResult, RXingResultMetadataType,
|
||||
RXingResultMetadataValue, RXingResultPoint, Reader, ResultPoint,
|
||||
};
|
||||
|
||||
use super::{
|
||||
@@ -43,10 +43,7 @@ impl Reader for PDF417Reader {
|
||||
* @throws NotFoundException if a PDF417 code cannot be found,
|
||||
* @throws FormatException if a PDF417 cannot be decoded
|
||||
*/
|
||||
fn decode(
|
||||
&mut self,
|
||||
image: &mut crate::BinaryBitmap,
|
||||
) -> Result<crate::RXingResult, crate::Exceptions> {
|
||||
fn decode(&mut self, image: &mut crate::BinaryBitmap) -> Result<crate::RXingResult> {
|
||||
self.decode_with_hints(image, &HashMap::new())
|
||||
}
|
||||
|
||||
@@ -54,7 +51,7 @@ impl Reader for PDF417Reader {
|
||||
&mut self,
|
||||
image: &mut crate::BinaryBitmap,
|
||||
hints: &crate::DecodingHintDictionary,
|
||||
) -> Result<crate::RXingResult, crate::Exceptions> {
|
||||
) -> Result<crate::RXingResult> {
|
||||
let result = Self::decode(image, hints, false)?;
|
||||
if result.is_empty() {
|
||||
return Err(Exceptions::NotFoundException(None));
|
||||
@@ -67,7 +64,7 @@ impl MultipleBarcodeReader for PDF417Reader {
|
||||
fn decode_multiple(
|
||||
&mut self,
|
||||
image: &mut crate::BinaryBitmap,
|
||||
) -> Result<Vec<crate::RXingResult>, crate::Exceptions> {
|
||||
) -> Result<Vec<crate::RXingResult>> {
|
||||
self.decode_multiple_with_hints(image, &HashMap::new())
|
||||
}
|
||||
|
||||
@@ -75,7 +72,7 @@ impl MultipleBarcodeReader for PDF417Reader {
|
||||
&mut self,
|
||||
image: &mut crate::BinaryBitmap,
|
||||
hints: &crate::DecodingHintDictionary,
|
||||
) -> Result<Vec<crate::RXingResult>, crate::Exceptions> {
|
||||
) -> Result<Vec<crate::RXingResult>> {
|
||||
Self::decode(image, hints, true)
|
||||
}
|
||||
}
|
||||
@@ -89,7 +86,7 @@ impl PDF417Reader {
|
||||
image: &mut BinaryBitmap,
|
||||
hints: &DecodingHintDictionary,
|
||||
multiple: bool,
|
||||
) -> Result<Vec<RXingResult>, Exceptions> {
|
||||
) -> Result<Vec<RXingResult>> {
|
||||
let mut results = Vec::new();
|
||||
let detectorRXingResult = pdf_417_detector::detect_with_hints(image, hints, multiple)?;
|
||||
|
||||
|
||||
@@ -17,7 +17,8 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{
|
||||
common::BitMatrix, BarcodeFormat, EncodeHintType, EncodeHintValue, Exceptions, Writer,
|
||||
common::{BitMatrix, Result},
|
||||
BarcodeFormat, EncodeHintType, EncodeHintValue, Exceptions, Writer,
|
||||
};
|
||||
|
||||
use super::encoder::PDF417;
|
||||
@@ -46,7 +47,7 @@ impl Writer for PDF417Writer {
|
||||
format: &crate::BarcodeFormat,
|
||||
width: i32,
|
||||
height: i32,
|
||||
) -> Result<crate::common::BitMatrix, crate::Exceptions> {
|
||||
) -> Result<crate::common::BitMatrix> {
|
||||
self.encode_with_hints(contents, format, width, height, &HashMap::new())
|
||||
}
|
||||
|
||||
@@ -57,7 +58,7 @@ impl Writer for PDF417Writer {
|
||||
width: i32,
|
||||
height: i32,
|
||||
hints: &crate::EncodingHintDictionary,
|
||||
) -> Result<crate::common::BitMatrix, crate::Exceptions> {
|
||||
) -> Result<crate::common::BitMatrix> {
|
||||
if format != &BarcodeFormat::PDF_417 {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
"Can only encode PDF_417, but got {format}"
|
||||
@@ -142,7 +143,7 @@ impl PDF417Writer {
|
||||
height: u32,
|
||||
margin: u32,
|
||||
autoECI: bool,
|
||||
) -> Result<BitMatrix, Exceptions> {
|
||||
) -> Result<BitMatrix> {
|
||||
encoder.generateBarcodeLogicWithAutoECI(contents, errorCorrectionLevel, autoECI)?;
|
||||
|
||||
let aspectRatio = 4;
|
||||
|
||||
Reference in New Issue
Block a user