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:
@@ -17,7 +17,7 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{
|
||||
common::{BitMatrix, DecoderRXingResult, DetectorRXingResult},
|
||||
common::{BitMatrix, DecoderRXingResult, DetectorRXingResult, Result},
|
||||
BarcodeFormat, DecodeHintType, DecodeHintValue, Exceptions, RXingResult,
|
||||
RXingResultMetadataType, RXingResultMetadataValue, Reader,
|
||||
};
|
||||
@@ -52,10 +52,7 @@ impl Reader for DataMatrixReader {
|
||||
* @throws FormatException if a Data Matrix code cannot be decoded
|
||||
* @throws ChecksumException if error correction fails
|
||||
*/
|
||||
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())
|
||||
}
|
||||
|
||||
@@ -71,7 +68,7 @@ impl Reader for DataMatrixReader {
|
||||
&mut self,
|
||||
image: &mut crate::BinaryBitmap,
|
||||
hints: &crate::DecodingHintDictionary,
|
||||
) -> Result<crate::RXingResult, crate::Exceptions> {
|
||||
) -> Result<crate::RXingResult> {
|
||||
let try_harder = matches!(
|
||||
hints.get(&DecodeHintType::TRY_HARDER),
|
||||
Some(DecodeHintValue::TryHarder(true))
|
||||
@@ -84,7 +81,7 @@ impl Reader for DataMatrixReader {
|
||||
points.clear();
|
||||
} else {
|
||||
//Result<DatamatrixDetectorResult, Exceptions>
|
||||
decoderRXingResult = if let Ok(fnd) = || -> Result<DecoderRXingResult, Exceptions> {
|
||||
decoderRXingResult = if let Ok(fnd) = || -> Result<DecoderRXingResult> {
|
||||
let detectorRXingResult =
|
||||
zxing_cpp_detector::detect(image.getBlackMatrix(), try_harder, true)?;
|
||||
let decoded = DECODER.decode(detectorRXingResult.getBits())?;
|
||||
@@ -93,7 +90,7 @@ impl Reader for DataMatrixReader {
|
||||
}() {
|
||||
fnd
|
||||
} else if try_harder {
|
||||
if let Ok(fnd) = || -> Result<DecoderRXingResult, Exceptions> {
|
||||
if let Ok(fnd) = || -> Result<DecoderRXingResult> {
|
||||
let detectorRXingResult = Detector::new(image.getBlackMatrix())?.detect()?;
|
||||
let decoded = DECODER.decode(detectorRXingResult.getBits())?;
|
||||
points = detectorRXingResult.getPoints().to_vec();
|
||||
@@ -179,7 +176,7 @@ impl DataMatrixReader {
|
||||
* around it. This is a specialized method that works exceptionally fast in this special
|
||||
* case.
|
||||
*/
|
||||
fn extractPureBits(&self, image: &BitMatrix) -> Result<BitMatrix, Exceptions> {
|
||||
fn extractPureBits(&self, image: &BitMatrix) -> Result<BitMatrix> {
|
||||
let Some(leftTopBlack) = image.getTopLeftOnBit() else {
|
||||
return Err(Exceptions::NotFoundException(None))
|
||||
};
|
||||
@@ -226,7 +223,7 @@ impl DataMatrixReader {
|
||||
Ok(bits)
|
||||
}
|
||||
|
||||
fn moduleSize(leftTopBlack: &[u32], image: &BitMatrix) -> Result<u32, Exceptions> {
|
||||
fn moduleSize(leftTopBlack: &[u32], image: &BitMatrix) -> Result<u32> {
|
||||
let width = image.getWidth();
|
||||
let mut x = leftTopBlack[0];
|
||||
let y = leftTopBlack[1];
|
||||
|
||||
@@ -20,8 +20,9 @@ use std::collections::HashMap;
|
||||
use encoding::EncodingRef;
|
||||
|
||||
use crate::{
|
||||
common::BitMatrix, qrcode::encoder::ByteMatrix, BarcodeFormat, EncodeHintType, EncodeHintValue,
|
||||
Exceptions, Writer,
|
||||
common::{BitMatrix, Result},
|
||||
qrcode::encoder::ByteMatrix,
|
||||
BarcodeFormat, EncodeHintType, EncodeHintValue, Exceptions, Writer,
|
||||
};
|
||||
|
||||
use super::encoder::{
|
||||
@@ -47,7 +48,7 @@ impl Writer for DataMatrixWriter {
|
||||
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())
|
||||
}
|
||||
|
||||
@@ -58,7 +59,7 @@ impl Writer for DataMatrixWriter {
|
||||
width: i32,
|
||||
height: i32,
|
||||
hints: &crate::EncodingHintDictionary,
|
||||
) -> Result<crate::common::BitMatrix, crate::Exceptions> {
|
||||
) -> Result<crate::common::BitMatrix> {
|
||||
if contents.is_empty() {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"Found empty contents".to_owned(),
|
||||
@@ -189,7 +190,7 @@ impl DataMatrixWriter {
|
||||
symbolInfo: &SymbolInfo,
|
||||
width: u32,
|
||||
height: u32,
|
||||
) -> Result<BitMatrix, Exceptions> {
|
||||
) -> Result<BitMatrix> {
|
||||
let symbolWidth = symbolInfo.getSymbolDataWidth()?;
|
||||
let symbolHeight = symbolInfo.getSymbolDataHeight()?;
|
||||
|
||||
@@ -255,7 +256,7 @@ impl DataMatrixWriter {
|
||||
matrix: &ByteMatrix,
|
||||
reqWidth: u32,
|
||||
reqHeight: u32,
|
||||
) -> Result<BitMatrix, Exceptions> {
|
||||
) -> Result<BitMatrix> {
|
||||
let matrixWidth = matrix.getWidth();
|
||||
let matrixHeight = matrix.getHeight();
|
||||
let outputWidth = reqWidth.max(matrixWidth);
|
||||
|
||||
@@ -14,7 +14,10 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use crate::{common::BitMatrix, Exceptions};
|
||||
use crate::{
|
||||
common::{BitMatrix, Result},
|
||||
Exceptions,
|
||||
};
|
||||
|
||||
use super::{Version, VersionRef};
|
||||
|
||||
@@ -31,7 +34,7 @@ impl BitMatrixParser {
|
||||
* @param bitMatrix {@link BitMatrix} to parse
|
||||
* @throws FormatException if dimension is < 8 or > 144 or not 0 mod 2
|
||||
*/
|
||||
pub fn new(bitMatrix: &BitMatrix) -> Result<Self, Exceptions> {
|
||||
pub fn new(bitMatrix: &BitMatrix) -> Result<Self> {
|
||||
let dimension = bitMatrix.getHeight();
|
||||
if !(8..=144).contains(&dimension) || (dimension & 0x01) != 0 {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
@@ -64,7 +67,7 @@ impl BitMatrixParser {
|
||||
* @throws FormatException if the dimensions of the mapping matrix are not valid
|
||||
* Data Matrix dimensions.
|
||||
*/
|
||||
fn readVersion(bitMatrix: &BitMatrix) -> Result<VersionRef, Exceptions> {
|
||||
fn readVersion(bitMatrix: &BitMatrix) -> Result<VersionRef> {
|
||||
let numRows = bitMatrix.getHeight();
|
||||
let numColumns = bitMatrix.getWidth();
|
||||
Version::getVersionForDimensions(numRows, numColumns)
|
||||
@@ -78,7 +81,7 @@ impl BitMatrixParser {
|
||||
* @return bytes encoded within the Data Matrix Code
|
||||
* @throws FormatException if the exact number of bytes expected is not read
|
||||
*/
|
||||
pub fn readCodewords(&mut self) -> Result<Vec<u8>, Exceptions> {
|
||||
pub fn readCodewords(&mut self) -> Result<Vec<u8>> {
|
||||
let mut result = vec![0u8; self.version.getTotalCodewords() as usize];
|
||||
let mut resultOffset = 0;
|
||||
|
||||
@@ -447,10 +450,7 @@ impl BitMatrixParser {
|
||||
* @param bitMatrix Original {@link BitMatrix} with alignment patterns
|
||||
* @return BitMatrix that has the alignment patterns removed
|
||||
*/
|
||||
fn extractDataRegion(
|
||||
bitMatrix: &BitMatrix,
|
||||
version: VersionRef,
|
||||
) -> Result<BitMatrix, Exceptions> {
|
||||
fn extractDataRegion(bitMatrix: &BitMatrix, version: VersionRef) -> Result<BitMatrix> {
|
||||
// dbg!(bitMatrix.to_string());
|
||||
let symbolSizeRows = version.getSymbolSizeRows();
|
||||
let symbolSizeColumns = version.getSymbolSizeColumns();
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
use super::Version;
|
||||
@@ -52,7 +53,7 @@ impl DataBlock {
|
||||
rawCodewords: &[u8],
|
||||
version: &Version,
|
||||
fix259: bool,
|
||||
) -> Result<Vec<DataBlock>, Exceptions> {
|
||||
) -> Result<Vec<DataBlock>> {
|
||||
// Figure out the number and size of data blocks used by this version
|
||||
let ecBlocks = version.getECBlocks();
|
||||
|
||||
|
||||
@@ -14,12 +14,9 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use crate::{
|
||||
common::{
|
||||
reedsolomon::{get_predefined_genericgf, PredefinedGenericGF, ReedSolomonDecoder},
|
||||
BitMatrix, DecoderRXingResult,
|
||||
},
|
||||
Exceptions,
|
||||
use crate::common::{
|
||||
reedsolomon::{get_predefined_genericgf, PredefinedGenericGF, ReedSolomonDecoder},
|
||||
BitMatrix, DecoderRXingResult, Result,
|
||||
};
|
||||
|
||||
use super::{decoded_bit_stream_parser, BitMatrixParser, DataBlock};
|
||||
@@ -49,7 +46,7 @@ impl Decoder {
|
||||
* @throws FormatException if the Data Matrix Code cannot be decoded
|
||||
* @throws ChecksumException if error correction fails
|
||||
*/
|
||||
pub fn decode(&self, bits: &BitMatrix) -> Result<DecoderRXingResult, Exceptions> {
|
||||
pub fn decode(&self, bits: &BitMatrix) -> Result<DecoderRXingResult> {
|
||||
let decoded = self.perform_decode(bits, false, false);
|
||||
if decoded.is_ok() {
|
||||
return decoded;
|
||||
@@ -58,7 +55,7 @@ impl Decoder {
|
||||
self.perform_decode(&Self::flip_bitmatrix(bits)?, false, true)
|
||||
}
|
||||
|
||||
fn flip_bitmatrix(bits: &BitMatrix) -> Result<BitMatrix, Exceptions> {
|
||||
fn flip_bitmatrix(bits: &BitMatrix) -> Result<BitMatrix> {
|
||||
let mut res = BitMatrix::new(bits.getHeight(), bits.getWidth())?;
|
||||
for y in 0..res.getHeight() {
|
||||
for x in 0..res.getWidth() {
|
||||
@@ -84,7 +81,7 @@ impl Decoder {
|
||||
* @throws FormatException if the Data Matrix Code cannot be decoded
|
||||
* @throws ChecksumException if error correction fails
|
||||
*/
|
||||
pub fn decode_bools(&self, image: &Vec<Vec<bool>>) -> Result<DecoderRXingResult, Exceptions> {
|
||||
pub fn decode_bools(&self, image: &Vec<Vec<bool>>) -> Result<DecoderRXingResult> {
|
||||
self.perform_decode(&BitMatrix::parse_bools(image), false, false)
|
||||
}
|
||||
|
||||
@@ -102,7 +99,7 @@ impl Decoder {
|
||||
bits: &BitMatrix,
|
||||
fix259: bool,
|
||||
is_flipped: bool,
|
||||
) -> Result<DecoderRXingResult, Exceptions> {
|
||||
) -> Result<DecoderRXingResult> {
|
||||
// Construct a parser and read version, error-correction level
|
||||
let mut parser = BitMatrixParser::new(bits)?;
|
||||
|
||||
@@ -153,11 +150,7 @@ impl Decoder {
|
||||
* @param numDataCodewords number of codewords that are data bytes
|
||||
* @throws ChecksumException if error correction fails
|
||||
*/
|
||||
fn correctErrors(
|
||||
&self,
|
||||
codewordBytes: &mut [u8],
|
||||
numDataCodewords: u32,
|
||||
) -> Result<(), Exceptions> {
|
||||
fn correctErrors(&self, codewordBytes: &mut [u8], numDataCodewords: u32) -> Result<()> {
|
||||
let _numCodewords = codewordBytes.len();
|
||||
// First read into an array of ints
|
||||
// let codewordsInts = vec![0i32;numCodewords];
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
use encoding::Encoding;
|
||||
|
||||
use crate::{
|
||||
common::{BitSource, DecoderRXingResult, ECIStringBuilder},
|
||||
common::{BitSource, DecoderRXingResult, ECIStringBuilder, Result},
|
||||
Exceptions,
|
||||
};
|
||||
|
||||
@@ -110,7 +110,7 @@ const INSERT_STRING_CONST: &str = "\u{001E}\u{0004}";
|
||||
const VALUE_236: &str = "[)>\u{001E}05\u{001D}";
|
||||
const VALUE_237: &str = "[)>\u{001E}06\u{001D}";
|
||||
|
||||
pub fn decode(bytes: &[u8], is_flipped: bool) -> Result<DecoderRXingResult, Exceptions> {
|
||||
pub fn decode(bytes: &[u8], is_flipped: bool) -> Result<DecoderRXingResult> {
|
||||
let mut bits = BitSource::new(bytes.to_vec());
|
||||
let mut result = ECIStringBuilder::with_capacity(100);
|
||||
let mut resultTrailer = String::new();
|
||||
@@ -217,7 +217,7 @@ fn decodeAsciiSegment(
|
||||
resultTrailer: &mut String,
|
||||
fnc1positions: &mut Vec<usize>,
|
||||
is_gs1: &mut bool,
|
||||
) -> Result<Mode, Exceptions> {
|
||||
) -> Result<Mode> {
|
||||
let mut upperShift = false;
|
||||
let mut firstFNC1Position = 1;
|
||||
let mut firstCodeword = true;
|
||||
@@ -353,7 +353,7 @@ fn decodeC40Segment(
|
||||
bits: &mut BitSource,
|
||||
result: &mut ECIStringBuilder,
|
||||
fnc1positions: &mut Vec<usize>,
|
||||
) -> Result<(), Exceptions> {
|
||||
) -> Result<()> {
|
||||
// Three C40 values are encoded in a 16-bit value as
|
||||
// (1600 * C1) + (40 * C2) + C3 + 1
|
||||
// TODO(bbrown): The Upper Shift with C40 doesn't work in the 4 value scenario all the time
|
||||
@@ -472,7 +472,7 @@ fn decodeTextSegment(
|
||||
bits: &mut BitSource,
|
||||
result: &mut ECIStringBuilder,
|
||||
fnc1positions: &mut Vec<usize>,
|
||||
) -> Result<(), Exceptions> {
|
||||
) -> Result<()> {
|
||||
// Three Text values are encoded in a 16-bit value as
|
||||
// (1600 * C1) + (40 * C2) + C3 + 1
|
||||
// TODO(bbrown): The Upper Shift with Text doesn't work in the 4 value scenario all the time
|
||||
@@ -592,10 +592,7 @@ fn decodeTextSegment(
|
||||
/**
|
||||
* See ISO 16022:2006, 5.2.7
|
||||
*/
|
||||
fn decodeAnsiX12Segment(
|
||||
bits: &mut BitSource,
|
||||
result: &mut ECIStringBuilder,
|
||||
) -> Result<(), Exceptions> {
|
||||
fn decodeAnsiX12Segment(bits: &mut BitSource, result: &mut ECIStringBuilder) -> Result<()> {
|
||||
// Three ANSI X12 values are encoded in a 16-bit value as
|
||||
// (1600 * C1) + (40 * C2) + C3 + 1
|
||||
|
||||
@@ -679,10 +676,7 @@ fn parseTwoBytes(firstByte: u32, secondByte: u32, result: &mut [u32]) {
|
||||
/**
|
||||
* See ISO 16022:2006, 5.2.8 and Annex C Table C.3
|
||||
*/
|
||||
fn decodeEdifactSegment(
|
||||
bits: &mut BitSource,
|
||||
result: &mut ECIStringBuilder,
|
||||
) -> Result<(), Exceptions> {
|
||||
fn decodeEdifactSegment(bits: &mut BitSource, result: &mut ECIStringBuilder) -> Result<()> {
|
||||
loop {
|
||||
// If there is only two or less bytes left then it will be encoded as ASCII
|
||||
if bits.available() <= 16 {
|
||||
@@ -727,7 +721,7 @@ fn decodeBase256Segment(
|
||||
bits: &mut BitSource,
|
||||
result: &mut ECIStringBuilder,
|
||||
byteSegments: &mut Vec<Vec<u8>>,
|
||||
) -> Result<(), Exceptions> {
|
||||
) -> Result<()> {
|
||||
// Figure out how long the Base 256 Segment is.
|
||||
let mut codewordPosition = 1 + bits.getByteOffset(); // position is 1-indexed
|
||||
let d1 = unrandomize255State(bits.readBits(8)?, codewordPosition);
|
||||
@@ -772,10 +766,7 @@ fn decodeBase256Segment(
|
||||
/**
|
||||
* See ISO 16022:2007, 5.4.1
|
||||
*/
|
||||
fn decodeECISegment(
|
||||
bits: &mut BitSource,
|
||||
result: &mut ECIStringBuilder,
|
||||
) -> Result<bool, Exceptions> {
|
||||
fn decodeECISegment(bits: &mut BitSource, result: &mut ECIStringBuilder) -> Result<bool> {
|
||||
let firstByte = bits.readBits(8)?;
|
||||
if firstByte <= 127 {
|
||||
result.appendECI(firstByte - 1)?;
|
||||
@@ -797,10 +788,7 @@ fn decodeECISegment(
|
||||
/**
|
||||
* See ISO 16022:2006, 5.6
|
||||
*/
|
||||
fn parse_structured_append(
|
||||
bits: &mut BitSource,
|
||||
sai: &mut StructuredAppendInfo,
|
||||
) -> Result<(), Exceptions> {
|
||||
fn parse_structured_append(bits: &mut BitSource, sai: &mut StructuredAppendInfo) -> Result<()> {
|
||||
// 5.6.2 Table 8
|
||||
let symbolSequenceIndicator = bits.readBits(8)?;
|
||||
sai.index = (symbolSequenceIndicator >> 4) as i32;
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
use core::fmt;
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
static VERSIONS: Lazy<Vec<Version>> = Lazy::new(Version::buildVersions);
|
||||
@@ -100,10 +101,7 @@ impl Version {
|
||||
* @return Version for a Data Matrix Code of those dimensions
|
||||
* @throws FormatException if dimensions do correspond to a valid Data Matrix size
|
||||
*/
|
||||
pub fn getVersionForDimensions(
|
||||
numRows: u32,
|
||||
numColumns: u32,
|
||||
) -> Result<&'static Version, Exceptions> {
|
||||
pub fn getVersionForDimensions(numRows: u32, numColumns: u32) -> Result<&'static Version> {
|
||||
if (numRows & 0x01) != 0 || (numColumns & 0x01) != 0 {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
}
|
||||
|
||||
@@ -15,7 +15,9 @@
|
||||
*/
|
||||
|
||||
use crate::{
|
||||
common::{detector::WhiteRectangleDetector, BitMatrix, DefaultGridSampler, GridSampler},
|
||||
common::{
|
||||
detector::WhiteRectangleDetector, BitMatrix, DefaultGridSampler, GridSampler, Result,
|
||||
},
|
||||
Exceptions, RXingResultPoint, ResultPoint,
|
||||
};
|
||||
|
||||
@@ -32,7 +34,7 @@ pub struct Detector<'a> {
|
||||
rectangleDetector: WhiteRectangleDetector<'a>,
|
||||
}
|
||||
impl<'a> Detector<'_> {
|
||||
pub fn new(image: &'a BitMatrix) -> Result<Detector<'a>, Exceptions> {
|
||||
pub fn new(image: &'a BitMatrix) -> Result<Detector<'a>> {
|
||||
Ok(Detector {
|
||||
rectangleDetector: WhiteRectangleDetector::new_from_image(image)?,
|
||||
image,
|
||||
@@ -45,7 +47,7 @@ impl<'a> Detector<'_> {
|
||||
* @return {@link DetectorRXingResult} encapsulating results of detecting a Data Matrix Code
|
||||
* @throws NotFoundException if no Data Matrix Code can be found
|
||||
*/
|
||||
pub fn detect(&self) -> Result<DatamatrixDetectorResult, Exceptions> {
|
||||
pub fn detect(&self) -> Result<DatamatrixDetectorResult> {
|
||||
let cornerPoints = self.rectangleDetector.detect()?;
|
||||
|
||||
let mut points = self.detectSolid1(cornerPoints);
|
||||
@@ -331,7 +333,7 @@ impl<'a> Detector<'_> {
|
||||
topRight: &RXingResultPoint,
|
||||
dimensionX: u32,
|
||||
dimensionY: u32,
|
||||
) -> Result<BitMatrix, Exceptions> {
|
||||
) -> Result<BitMatrix> {
|
||||
let sampler = DefaultGridSampler::default();
|
||||
|
||||
sampler.sample_grid_detailed(
|
||||
|
||||
@@ -14,7 +14,7 @@ macro_rules! CHECK {
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
use crate::{
|
||||
common::{BitMatrix, DefaultGridSampler, GridSampler},
|
||||
common::{BitMatrix, DefaultGridSampler, GridSampler, Result},
|
||||
datamatrix::detector::{
|
||||
zxing_cpp_detector::{util::intersect, BitMatrixCursor, Quadrilateral, RegressionLine},
|
||||
DatamatrixDetectorResult,
|
||||
@@ -37,7 +37,7 @@ use super::{DMRegressionLine, EdgeTracer};
|
||||
fn Scan(
|
||||
startTracer: &mut EdgeTracer,
|
||||
lines: &mut [DMRegressionLine; 4],
|
||||
) -> Result<DatamatrixDetectorResult, Exceptions> {
|
||||
) -> Result<DatamatrixDetectorResult> {
|
||||
while startTracer.step(None) {
|
||||
//log(startTracer.p);
|
||||
|
||||
@@ -261,7 +261,7 @@ pub fn detect(
|
||||
image: &BitMatrix,
|
||||
tryHarder: bool,
|
||||
tryRotate: bool,
|
||||
) -> Result<DatamatrixDetectorResult, Exceptions> {
|
||||
) -> Result<DatamatrixDetectorResult> {
|
||||
// #ifdef PRINT_DEBUG
|
||||
// LogMatrixWriter lmw(log, image, 1, "dm-log.pnm");
|
||||
// // tryRotate = tryHarder = false;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::common::Result;
|
||||
use crate::{Exceptions, RXingResultPoint};
|
||||
|
||||
use super::{
|
||||
@@ -74,7 +75,7 @@ impl RegressionLine for DMRegressionLine {
|
||||
self.c = f32::NAN;
|
||||
}
|
||||
|
||||
fn add(&mut self, p: &RXingResultPoint) -> Result<(), Exceptions> {
|
||||
fn add(&mut self, p: &RXingResultPoint) -> Result<()> {
|
||||
if self.direction_inward == RXingResultPoint::default() {
|
||||
return Err(Exceptions::IllegalStateException(None));
|
||||
}
|
||||
@@ -235,11 +236,7 @@ impl DMRegressionLine {
|
||||
self.points.reverse();
|
||||
}
|
||||
|
||||
pub fn modules(
|
||||
&mut self,
|
||||
beg: &RXingResultPoint,
|
||||
end: &RXingResultPoint,
|
||||
) -> Result<f64, Exceptions> {
|
||||
pub fn modules(&mut self, beg: &RXingResultPoint, end: &RXingResultPoint) -> Result<f64> {
|
||||
if self.points.len() <= 3 {
|
||||
return Err(Exceptions::IllegalStateException(None));
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
use crate::{common::BitMatrix, qrcode::encoder::ByteMatrix, Exceptions, RXingResultPoint};
|
||||
use crate::{
|
||||
common::{BitMatrix, Result},
|
||||
qrcode::encoder::ByteMatrix,
|
||||
Exceptions, RXingResultPoint,
|
||||
};
|
||||
|
||||
use super::{BitMatrixCursor, Direction, RegressionLine, StepResult, Value};
|
||||
|
||||
@@ -170,7 +174,7 @@ impl<'a> EdgeTracer<'_> {
|
||||
dEdge: &RXingResultPoint,
|
||||
maxStepSize: i32,
|
||||
goodDirection: bool,
|
||||
) -> Result<StepResult, Exceptions> {
|
||||
) -> Result<StepResult> {
|
||||
let dEdge = RXingResultPoint::mainDirection(*dEdge);
|
||||
for breadth in 1..=(if maxStepSize == 1 {
|
||||
2
|
||||
@@ -262,7 +266,7 @@ impl<'a> EdgeTracer<'_> {
|
||||
&mut self,
|
||||
dEdge: &RXingResultPoint,
|
||||
line: &mut T,
|
||||
) -> Result<bool, Exceptions> {
|
||||
) -> Result<bool> {
|
||||
line.setDirectionInward(dEdge);
|
||||
loop {
|
||||
// log(self.p);
|
||||
@@ -295,7 +299,7 @@ impl<'a> EdgeTracer<'_> {
|
||||
line: &mut T,
|
||||
maxStepSize: i32,
|
||||
finishLine: &mut T,
|
||||
) -> Result<bool, Exceptions> {
|
||||
) -> Result<bool> {
|
||||
let mut maxStepSize = maxStepSize;
|
||||
line.setDirectionInward(dEdge);
|
||||
let mut gaps = 0;
|
||||
@@ -437,7 +441,7 @@ impl<'a> EdgeTracer<'_> {
|
||||
&mut self,
|
||||
dir: &mut RXingResultPoint,
|
||||
corner: &mut RXingResultPoint,
|
||||
) -> Result<bool, Exceptions> {
|
||||
) -> Result<bool> {
|
||||
self.step(None);
|
||||
// log(p);
|
||||
*corner = self.p;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::{Exceptions, RXingResultPoint};
|
||||
use crate::common::Result;
|
||||
use crate::RXingResultPoint;
|
||||
|
||||
pub trait RegressionLine {
|
||||
// points: Vec<RXingResultPoint>,
|
||||
@@ -76,12 +77,12 @@ pub trait RegressionLine {
|
||||
// a = b = c = NAN;
|
||||
// }
|
||||
|
||||
fn add(&mut self, p: &RXingResultPoint) -> Result<(), Exceptions>; //{
|
||||
// assert(_directionInward != PointF());
|
||||
// _points.push_back(p);
|
||||
// if (_points.size() == 1)
|
||||
// c = dot(normal(), p);
|
||||
// }
|
||||
fn add(&mut self, p: &RXingResultPoint) -> Result<()>; //{
|
||||
// assert(_directionInward != PointF());
|
||||
// _points.push_back(p);
|
||||
// if (_points.size() == 1)
|
||||
// c = dot(normal(), p);
|
||||
// }
|
||||
|
||||
fn pop_back(&mut self); // { _points.pop_back(); }
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::common::Result;
|
||||
use crate::{Exceptions, RXingResultPoint};
|
||||
|
||||
use super::{DMRegressionLine, Direction, RegressionLine};
|
||||
@@ -21,10 +22,7 @@ pub fn float_max<T: PartialOrd>(a: T, b: T) -> T {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn intersect(
|
||||
l1: &DMRegressionLine,
|
||||
l2: &DMRegressionLine,
|
||||
) -> Result<RXingResultPoint, Exceptions> {
|
||||
pub fn intersect(l1: &DMRegressionLine, l2: &DMRegressionLine) -> Result<RXingResultPoint> {
|
||||
if !(l1.isValid() && l2.isValid()) {
|
||||
return Err(Exceptions::IllegalStateException(None));
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
use super::{high_level_encoder, Encoder};
|
||||
@@ -21,7 +22,7 @@ use super::{high_level_encoder, Encoder};
|
||||
pub struct ASCIIEncoder;
|
||||
|
||||
impl Encoder for ASCIIEncoder {
|
||||
fn encode(&self, context: &mut super::EncoderContext) -> Result<(), Exceptions> {
|
||||
fn encode(&self, context: &mut super::EncoderContext) -> Result<()> {
|
||||
//step B
|
||||
let n =
|
||||
high_level_encoder::determineConsecutiveDigitCount(context.getMessage(), context.pos);
|
||||
@@ -99,7 +100,7 @@ impl ASCIIEncoder {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
fn encodeASCIIDigits(digit1: char, digit2: char) -> Result<char, Exceptions> {
|
||||
fn encodeASCIIDigits(digit1: char, digit2: char) -> Result<char> {
|
||||
if high_level_encoder::isDigit(digit1) && high_level_encoder::isDigit(digit2) {
|
||||
let num = (digit1 as u8 - 48) * 10 + (digit2 as u8 - 48);
|
||||
Ok((num + 130) as char)
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
use super::{
|
||||
@@ -27,7 +28,7 @@ impl Encoder for Base256Encoder {
|
||||
BASE256_ENCODATION
|
||||
}
|
||||
|
||||
fn encode(&self, context: &mut super::EncoderContext) -> Result<(), crate::Exceptions> {
|
||||
fn encode(&self, context: &mut super::EncoderContext) -> Result<()> {
|
||||
let mut buffer = String::new();
|
||||
buffer.push('\0'); //Initialize length field
|
||||
while context.hasMoreCharacters() {
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
use super::high_level_encoder::{
|
||||
@@ -25,7 +26,7 @@ use super::{Encoder, EncoderContext};
|
||||
pub struct C40Encoder;
|
||||
|
||||
impl Encoder for C40Encoder {
|
||||
fn encode(&self, context: &mut super::EncoderContext) -> Result<(), Exceptions> {
|
||||
fn encode(&self, context: &mut super::EncoderContext) -> Result<()> {
|
||||
self.encode_with_encode_char_fn(
|
||||
context,
|
||||
&Self::encodeChar_c40,
|
||||
@@ -48,9 +49,9 @@ impl C40Encoder {
|
||||
&self,
|
||||
context: &mut super::EncoderContext,
|
||||
encodeChar: &dyn Fn(char, &mut String) -> u32,
|
||||
handleEOD: &dyn Fn(&mut EncoderContext, &mut String) -> Result<(), Exceptions>,
|
||||
handleEOD: &dyn Fn(&mut EncoderContext, &mut String) -> Result<()>,
|
||||
getEncodingMode: &dyn Fn() -> usize,
|
||||
) -> Result<(), Exceptions> {
|
||||
) -> Result<()> {
|
||||
//step C
|
||||
let mut buffer = String::new();
|
||||
while context.hasMoreCharacters() {
|
||||
@@ -110,7 +111,7 @@ impl C40Encoder {
|
||||
handleEOD(context, &mut buffer)
|
||||
}
|
||||
|
||||
pub fn encodeMaximalC40(&self, context: &mut EncoderContext) -> Result<(), Exceptions> {
|
||||
pub fn encodeMaximalC40(&self, context: &mut EncoderContext) -> Result<()> {
|
||||
self.encodeMaximal(context, &Self::encodeChar_c40, &Self::handleEOD_c40)
|
||||
}
|
||||
|
||||
@@ -118,8 +119,8 @@ impl C40Encoder {
|
||||
&self,
|
||||
context: &mut EncoderContext,
|
||||
encodeChar: &dyn Fn(char, &mut String) -> u32,
|
||||
handleEOD: &dyn Fn(&mut EncoderContext, &mut String) -> Result<(), Exceptions>,
|
||||
) -> Result<(), Exceptions> {
|
||||
handleEOD: &dyn Fn(&mut EncoderContext, &mut String) -> Result<()>,
|
||||
) -> Result<()> {
|
||||
let mut buffer = String::new();
|
||||
let mut lastCharSize = 0;
|
||||
let mut backtrackStartPosition = context.pos;
|
||||
@@ -181,7 +182,7 @@ impl C40Encoder {
|
||||
pub(super) fn writeNextTriplet(
|
||||
context: &mut EncoderContext,
|
||||
buffer: &mut String,
|
||||
) -> Result<(), Exceptions> {
|
||||
) -> Result<()> {
|
||||
context.writeCodewords(
|
||||
&Self::encodeToCodewords(buffer).ok_or(Exceptions::FormatException(None))?,
|
||||
);
|
||||
@@ -196,10 +197,7 @@ impl C40Encoder {
|
||||
* @param context the encoder context
|
||||
* @param buffer the buffer with the remaining encoded characters
|
||||
*/
|
||||
pub fn handleEOD_c40(
|
||||
context: &mut EncoderContext,
|
||||
buffer: &mut String,
|
||||
) -> Result<(), Exceptions> {
|
||||
pub fn handleEOD_c40(context: &mut EncoderContext, buffer: &mut String) -> Result<()> {
|
||||
let unwritten = (buffer.chars().count() / 3) * 2;
|
||||
let rest = buffer.chars().count() % 3;
|
||||
|
||||
|
||||
@@ -14,12 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use crate::Exceptions;
|
||||
use crate::common::Result;
|
||||
|
||||
use super::EncoderContext;
|
||||
|
||||
pub trait Encoder {
|
||||
fn getEncodingMode(&self) -> usize;
|
||||
|
||||
fn encode(&self, context: &mut EncoderContext) -> Result<(), Exceptions>;
|
||||
fn encode(&self, context: &mut EncoderContext) -> Result<()>;
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
const EMPTY_BIT_VAL: u8 = 13;
|
||||
@@ -73,7 +74,7 @@ impl DefaultPlacement {
|
||||
self.bits[row * self.numcols + col] == EMPTY_BIT_VAL
|
||||
}
|
||||
|
||||
pub fn place(&mut self) -> Result<(), Exceptions> {
|
||||
pub fn place(&mut self) -> Result<()> {
|
||||
let mut pos = 0;
|
||||
let mut row = 4_isize;
|
||||
let mut col = 0_isize;
|
||||
@@ -147,7 +148,7 @@ impl DefaultPlacement {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn module(&mut self, row: isize, col: isize, pos: usize, bit: u32) -> Result<(), Exceptions> {
|
||||
fn module(&mut self, row: isize, col: isize, pos: usize, bit: u32) -> Result<()> {
|
||||
let mut row = row;
|
||||
let mut col = col;
|
||||
|
||||
@@ -178,7 +179,7 @@ impl DefaultPlacement {
|
||||
* @param col the column
|
||||
* @param pos character position
|
||||
*/
|
||||
fn utah(&mut self, row: isize, col: isize, pos: usize) -> Result<(), Exceptions> {
|
||||
fn utah(&mut self, row: isize, col: isize, pos: usize) -> Result<()> {
|
||||
self.module(row - 2, col - 2, pos, 1)?;
|
||||
self.module(row - 2, col - 1, pos, 2)?;
|
||||
self.module(row - 1, col - 2, pos, 3)?;
|
||||
@@ -190,7 +191,7 @@ impl DefaultPlacement {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn corner1(&mut self, pos: usize) -> Result<(), Exceptions> {
|
||||
fn corner1(&mut self, pos: usize) -> Result<()> {
|
||||
self.module(self.numrows as isize - 1, 0, pos, 1)?;
|
||||
self.module(self.numrows as isize - 1, 1, pos, 2)?;
|
||||
self.module(self.numrows as isize - 1, 2, pos, 3)?;
|
||||
@@ -202,7 +203,7 @@ impl DefaultPlacement {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn corner2(&mut self, pos: usize) -> Result<(), Exceptions> {
|
||||
fn corner2(&mut self, pos: usize) -> Result<()> {
|
||||
self.module(self.numrows as isize - 3, 0, pos, 1)?;
|
||||
self.module(self.numrows as isize - 2, 0, pos, 2)?;
|
||||
self.module(self.numrows as isize - 1, 0, pos, 3)?;
|
||||
@@ -214,7 +215,7 @@ impl DefaultPlacement {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn corner3(&mut self, pos: usize) -> Result<(), Exceptions> {
|
||||
fn corner3(&mut self, pos: usize) -> Result<()> {
|
||||
self.module(self.numrows as isize - 3, 0, pos, 1)?;
|
||||
self.module(self.numrows as isize - 2, 0, pos, 2)?;
|
||||
self.module(self.numrows as isize - 1, 0, pos, 3)?;
|
||||
@@ -226,7 +227,7 @@ impl DefaultPlacement {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn corner4(&mut self, pos: usize) -> Result<(), Exceptions> {
|
||||
fn corner4(&mut self, pos: usize) -> Result<()> {
|
||||
self.module(self.numrows as isize - 1, 0, pos, 1)?;
|
||||
self.module(self.numrows as isize - 1, self.numcols as isize - 1, pos, 2)?;
|
||||
self.module(0, self.numcols as isize - 3, pos, 3)?;
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
use super::{high_level_encoder, Encoder, EncoderContext};
|
||||
@@ -24,7 +25,7 @@ impl Encoder for EdifactEncoder {
|
||||
high_level_encoder::EDIFACT_ENCODATION
|
||||
}
|
||||
|
||||
fn encode(&self, context: &mut super::EncoderContext) -> Result<(), crate::Exceptions> {
|
||||
fn encode(&self, context: &mut super::EncoderContext) -> Result<()> {
|
||||
//step F
|
||||
let mut buffer = String::new();
|
||||
while context.hasMoreCharacters() {
|
||||
@@ -65,8 +66,8 @@ impl EdifactEncoder {
|
||||
* @param context the encoder context
|
||||
* @param buffer the buffer with the remaining encoded characters
|
||||
*/
|
||||
fn handleEOD(context: &mut EncoderContext, buffer: &mut str) -> Result<(), Exceptions> {
|
||||
let mut runner = || -> Result<(), Exceptions> {
|
||||
fn handleEOD(context: &mut EncoderContext, buffer: &mut str) -> Result<()> {
|
||||
let mut runner = || -> Result<()> {
|
||||
let count = buffer.chars().count();
|
||||
if count == 0 {
|
||||
return Ok(()); //Already finished
|
||||
@@ -135,7 +136,7 @@ impl EdifactEncoder {
|
||||
res
|
||||
}
|
||||
|
||||
fn encodeChar(c: char, sb: &mut String) -> Result<(), Exceptions> {
|
||||
fn encodeChar(c: char, sb: &mut String) -> Result<()> {
|
||||
if (' '..='?').contains(&c) {
|
||||
sb.push(c);
|
||||
} else if ('@'..='^').contains(&c) {
|
||||
@@ -146,7 +147,7 @@ impl EdifactEncoder {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn encodeToCodewords(sb: &str) -> Result<String, Exceptions> {
|
||||
fn encodeToCodewords(sb: &str) -> Result<String> {
|
||||
let len = sb.chars().count();
|
||||
if len == 0 {
|
||||
return Err(Exceptions::IllegalStateException(Some(
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::{Dimension, Exceptions};
|
||||
|
||||
use super::{SymbolInfo, SymbolInfoLookup, SymbolShapeHint};
|
||||
@@ -40,13 +41,13 @@ impl<'a> EncoderContext<'_> {
|
||||
pub fn with_symbol_info_lookup(
|
||||
msg: &str,
|
||||
symbol_lookup: Rc<SymbolInfoLookup<'a>>,
|
||||
) -> Result<EncoderContext<'a>, Exceptions> {
|
||||
) -> Result<EncoderContext<'a>> {
|
||||
let mut new_self = EncoderContext::new(msg)?;
|
||||
new_self.symbol_lookup = symbol_lookup.clone();
|
||||
Ok(new_self)
|
||||
}
|
||||
|
||||
pub fn new(msg: &str) -> Result<Self, Exceptions> {
|
||||
pub fn new(msg: &str) -> Result<Self> {
|
||||
//From this point on Strings are not Unicode anymore!
|
||||
// let msgBinary = ISO_8859_1_ENCODER.encode(msg, encoding::EncoderTrap::Strict).expect("encode to bytes");//msg.getBytes(StandardCharsets.ISO_8859_1);
|
||||
// let sb = String::with_capacity(msgBinary.len());
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
use super::SymbolInfo;
|
||||
@@ -152,7 +153,7 @@ const ALOG: [u32; 255] = {
|
||||
* @param symbolInfo information about the symbol to be encoded
|
||||
* @return the codewords with interleaved error correction.
|
||||
*/
|
||||
pub fn encodeECC200(codewords: &str, symbolInfo: &SymbolInfo) -> Result<String, Exceptions> {
|
||||
pub fn encodeECC200(codewords: &str, symbolInfo: &SymbolInfo) -> Result<String> {
|
||||
if codewords.chars().count() != symbolInfo.getDataCapacity() as usize {
|
||||
return Err(Exceptions::IllegalArgumentException(Some(
|
||||
"The number of codewords does not match the selected symbol".to_owned(),
|
||||
@@ -217,7 +218,7 @@ pub fn encodeECC200(codewords: &str, symbolInfo: &SymbolInfo) -> Result<String,
|
||||
Ok(sb)
|
||||
}
|
||||
|
||||
fn createECCBlock(codewords: &str, numECWords: usize) -> Result<String, Exceptions> {
|
||||
fn createECCBlock(codewords: &str, numECWords: usize) -> Result<String> {
|
||||
let mut table = -1_isize;
|
||||
for (i, set) in FACTOR_SETS.iter().enumerate() {
|
||||
// for i in 0..FACTOR_SETS.len() {
|
||||
|
||||
@@ -18,6 +18,7 @@ use std::rc::Rc;
|
||||
|
||||
use encoding::{self, EncodingRef};
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::{Dimension, Exceptions};
|
||||
|
||||
use super::{
|
||||
@@ -134,7 +135,7 @@ fn randomize253State(codewordPosition: u32) -> String {
|
||||
* @param msg the message
|
||||
* @return the encoded message (the char values range from 0 to 255)
|
||||
*/
|
||||
pub fn encodeHighLevel(msg: &str) -> Result<String, Exceptions> {
|
||||
pub fn encodeHighLevel(msg: &str) -> Result<String> {
|
||||
encodeHighLevelWithDimensionForceC40(msg, SymbolShapeHint::FORCE_NONE, None, None, false)
|
||||
}
|
||||
|
||||
@@ -148,7 +149,7 @@ pub fn encodeHighLevel(msg: &str) -> Result<String, Exceptions> {
|
||||
pub fn encodeHighLevelSIL(
|
||||
msg: &str,
|
||||
symbol_lookup: Option<Rc<SymbolInfoLookup>>,
|
||||
) -> Result<String, Exceptions> {
|
||||
) -> Result<String> {
|
||||
encodeHighLevelWithDimensionForceC40WithSymbolInfoLookup(
|
||||
msg,
|
||||
SymbolShapeHint::FORCE_NONE,
|
||||
@@ -175,7 +176,7 @@ pub fn encodeHighLevelWithDimension(
|
||||
shape: SymbolShapeHint,
|
||||
minSize: Option<Dimension>,
|
||||
maxSize: Option<Dimension>,
|
||||
) -> Result<String, Exceptions> {
|
||||
) -> Result<String> {
|
||||
encodeHighLevelWithDimensionForceC40(msg, shape, minSize, maxSize, false)
|
||||
}
|
||||
|
||||
@@ -186,7 +187,7 @@ pub fn encodeHighLevelWithDimensionForceC40WithSymbolInfoLookup(
|
||||
maxSize: Option<Dimension>,
|
||||
forceC40: bool,
|
||||
symbol_lookup: Option<Rc<SymbolInfoLookup>>,
|
||||
) -> Result<String, Exceptions> {
|
||||
) -> Result<String> {
|
||||
//the codewords 0..255 are encoded as Unicode characters
|
||||
let c40Encoder = Rc::new(C40Encoder::new());
|
||||
let encoders: [Rc<dyn Encoder>; 6] = [
|
||||
@@ -284,7 +285,7 @@ pub fn encodeHighLevelWithDimensionForceC40(
|
||||
minSize: Option<Dimension>,
|
||||
maxSize: Option<Dimension>,
|
||||
forceC40: bool,
|
||||
) -> Result<String, Exceptions> {
|
||||
) -> Result<String> {
|
||||
encodeHighLevelWithDimensionForceC40WithSymbolInfoLookup(
|
||||
msg, shape, minSize, maxSize, forceC40, None,
|
||||
)
|
||||
@@ -608,7 +609,7 @@ pub fn determineConsecutiveDigitCount(msg: &str, startpos: u32) -> u32 {
|
||||
idx - startpos
|
||||
}
|
||||
|
||||
pub fn illegalCharacter(c: char) -> Result<(), Exceptions> {
|
||||
pub fn illegalCharacter(c: char) -> Result<()> {
|
||||
// let hex = Integer.toHexString(c);
|
||||
// hex = "0000".substring(0, 4 - hex.length()) + hex;
|
||||
Err(Exceptions::IllegalArgumentException(Some(format!(
|
||||
|
||||
@@ -19,7 +19,7 @@ use std::{fmt, rc::Rc};
|
||||
use encoding::{self, EncodingRef};
|
||||
|
||||
use crate::{
|
||||
common::{ECIInput, MinimalECIInput},
|
||||
common::{ECIInput, MinimalECIInput, Result},
|
||||
Exceptions,
|
||||
};
|
||||
|
||||
@@ -134,7 +134,7 @@ fn isInTextShift2Set(ch: char, fnc1: Option<char>) -> bool {
|
||||
* @param msg the message
|
||||
* @return the encoded message (the char values range from 0 to 255)
|
||||
*/
|
||||
pub fn encodeHighLevel(msg: &str) -> Result<String, Exceptions> {
|
||||
pub fn encodeHighLevel(msg: &str) -> Result<String> {
|
||||
encodeHighLevelWithDetails(msg, None, None, SymbolShapeHint::FORCE_NONE)
|
||||
}
|
||||
|
||||
@@ -156,7 +156,7 @@ pub fn encodeHighLevelWithDetails(
|
||||
priorityCharset: Option<EncodingRef>,
|
||||
fnc1: Option<char>,
|
||||
shape: SymbolShapeHint,
|
||||
) -> Result<String, Exceptions> {
|
||||
) -> Result<String> {
|
||||
let mut msg = msg;
|
||||
let mut macroId = 0;
|
||||
if msg.starts_with(high_level_encoder::MACRO_05_HEADER)
|
||||
@@ -201,7 +201,7 @@ fn encode(
|
||||
fnc1: Option<char>,
|
||||
shape: SymbolShapeHint,
|
||||
macroId: i32,
|
||||
) -> Result<Vec<u8>, Exceptions> {
|
||||
) -> Result<Vec<u8>> {
|
||||
Ok(encodeMinimally(Rc::new(Input::new(
|
||||
input,
|
||||
priorityCharset,
|
||||
@@ -213,7 +213,7 @@ fn encode(
|
||||
.to_vec())
|
||||
}
|
||||
|
||||
fn addEdge(edges: &mut [Vec<Option<Rc<Edge>>>], edge: Rc<Edge>) -> Result<(), Exceptions> {
|
||||
fn addEdge(edges: &mut [Vec<Option<Rc<Edge>>>], edge: Rc<Edge>) -> Result<()> {
|
||||
let vertexIndex = (edge.fromPosition + edge.characterLength) as usize;
|
||||
if edges[vertexIndex][edge.getEndMode()?.ordinal()].is_none()
|
||||
|| edges[vertexIndex][edge.getEndMode()?.ordinal()]
|
||||
@@ -239,7 +239,7 @@ fn getNumberOfC40Words(
|
||||
from: u32,
|
||||
c40: bool,
|
||||
characterLength: &mut [u32],
|
||||
) -> Result<u32, Exceptions> {
|
||||
) -> Result<u32> {
|
||||
let mut thirdsCount = 0;
|
||||
for i in (from as usize)..input.length() {
|
||||
// for (int i = from; i < input.length(); i++) {
|
||||
@@ -282,7 +282,7 @@ fn addEdges(
|
||||
edges: &mut [Vec<Option<Rc<Edge>>>],
|
||||
from: u32,
|
||||
previous: Option<Rc<Edge>>,
|
||||
) -> Result<(), Exceptions> {
|
||||
) -> Result<()> {
|
||||
if input.isECI(from)? {
|
||||
addEdge(
|
||||
edges,
|
||||
@@ -409,7 +409,7 @@ fn addEdges(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn encodeMinimally(input: Rc<Input>) -> Result<RXingResult, Exceptions> {
|
||||
fn encodeMinimally(input: Rc<Input>) -> Result<RXingResult> {
|
||||
// @SuppressWarnings("checkstyle:lineLength")
|
||||
/* The minimal encoding is computed by Dijkstra. The acyclic graph is modeled as follows:
|
||||
* A vertex represents a combination of a position in the input and an encoding mode where position 0
|
||||
@@ -667,7 +667,7 @@ impl Edge {
|
||||
fromPosition: u32,
|
||||
characterLength: u32,
|
||||
previous: Option<Rc<Edge>>,
|
||||
) -> Result<Self, Exceptions> {
|
||||
) -> Result<Self> {
|
||||
if fromPosition + characterLength > input.length() as u32 {
|
||||
return Err(Exceptions::FormatException(None));
|
||||
}
|
||||
@@ -804,7 +804,7 @@ impl Edge {
|
||||
// if previous.is_none() { Mode::ASCII} else {previous.as_ref().unwrap().mode}
|
||||
}
|
||||
|
||||
pub fn getPreviousMode(previous: Option<Rc<Edge>>) -> Result<Mode, Exceptions> {
|
||||
pub fn getPreviousMode(previous: Option<Rc<Edge>>) -> Result<Mode> {
|
||||
if let Some(prev) = previous {
|
||||
prev.getEndMode()
|
||||
} else {
|
||||
@@ -819,7 +819,7 @@ impl Edge {
|
||||
* - Mode is C40, TEXT or X12 and the remaining characters can be encoded in at most 1 ASCII byte.
|
||||
* Returns mode in all other cases.
|
||||
* */
|
||||
pub fn getEndMode(&self) -> Result<Mode, Exceptions> {
|
||||
pub fn getEndMode(&self) -> Result<Mode> {
|
||||
let mode = self.mode;
|
||||
if mode == Mode::Edf {
|
||||
if self.characterLength < 4 {
|
||||
@@ -856,7 +856,7 @@ impl Edge {
|
||||
* two consecutive digits and a non extended character or of 4 digits.
|
||||
* Returns 0 in any other case
|
||||
**/
|
||||
pub fn getLastASCII(&self) -> Result<u32, Exceptions> {
|
||||
pub fn getLastASCII(&self) -> Result<u32> {
|
||||
let length = self.input.length() as u32;
|
||||
let from = self.fromPosition + self.characterLength;
|
||||
if length - from > 4 || from >= length {
|
||||
@@ -1000,7 +1000,7 @@ impl Edge {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn getX12Words(&self) -> Result<Vec<u8>, Exceptions> {
|
||||
pub fn getX12Words(&self) -> Result<Vec<u8>> {
|
||||
assert!(self.characterLength % 3 == 0);
|
||||
let mut result = vec![0u8; self.characterLength as usize / 3 * 2];
|
||||
let mut i = 0;
|
||||
@@ -1096,7 +1096,7 @@ impl Edge {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn getC40Words(&self, c40: bool, fnc1: Option<char>) -> Result<Vec<u8>, Exceptions> {
|
||||
pub fn getC40Words(&self, c40: bool, fnc1: Option<char>) -> Result<Vec<u8>> {
|
||||
let mut c40Values: Vec<u8> = Vec::new();
|
||||
let fromPosition = self.fromPosition as usize;
|
||||
for i in 0..self.characterLength as usize {
|
||||
@@ -1156,7 +1156,7 @@ impl Edge {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn getEDFBytes(&self) -> Result<Vec<u8>, Exceptions> {
|
||||
pub fn getEDFBytes(&self) -> Result<Vec<u8>> {
|
||||
let numberOfThirds = (self.characterLength as f32 / 4.0).ceil() as usize;
|
||||
let mut result = vec![0u8; numberOfThirds * 3];
|
||||
let mut pos = self.fromPosition as usize;
|
||||
@@ -1190,7 +1190,7 @@ impl Edge {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn getLatchBytes(&self) -> Result<Vec<u8>, Exceptions> {
|
||||
pub fn getLatchBytes(&self) -> Result<Vec<u8>> {
|
||||
match Self::getPreviousMode(self.previous.clone())? {
|
||||
Mode::Ascii | Mode::B256 =>
|
||||
//after B256 ends (via length) we are back to ASCII
|
||||
@@ -1224,7 +1224,7 @@ impl Edge {
|
||||
}
|
||||
|
||||
// Important: The function does not return the length bytes (one or two) in case of B256 encoding
|
||||
pub fn getDataBytes(&self) -> Result<Vec<u8>, Exceptions> {
|
||||
pub fn getDataBytes(&self) -> Result<Vec<u8>> {
|
||||
match self.mode {
|
||||
Mode::Ascii => {
|
||||
if self.input.isECI(self.fromPosition)? {
|
||||
@@ -1272,7 +1272,7 @@ struct RXingResult {
|
||||
bytes: Vec<u8>,
|
||||
}
|
||||
impl RXingResult {
|
||||
pub fn new(solution: Option<Rc<Edge>>) -> Result<Self, Exceptions> {
|
||||
pub fn new(solution: Option<Rc<Edge>>) -> Result<Self> {
|
||||
let solution = if let Some(edge) = solution {
|
||||
edge
|
||||
} else {
|
||||
@@ -1427,10 +1427,10 @@ impl Input {
|
||||
pub fn length(&self) -> usize {
|
||||
self.internal.length()
|
||||
}
|
||||
pub fn isECI(&self, index: u32) -> Result<bool, Exceptions> {
|
||||
pub fn isECI(&self, index: u32) -> Result<bool> {
|
||||
self.internal.isECI(index)
|
||||
}
|
||||
pub fn charAt(&self, index: usize) -> Result<char, Exceptions> {
|
||||
pub fn charAt(&self, index: usize) -> Result<char> {
|
||||
self.internal.charAt(index)
|
||||
}
|
||||
pub fn getFNC1Character(&self) -> Option<char> {
|
||||
@@ -1440,13 +1440,13 @@ impl Input {
|
||||
Some(self.internal.getFNC1Character() as u8 as char)
|
||||
}
|
||||
}
|
||||
fn haveNCharacters(&self, index: usize, n: usize) -> Result<bool, Exceptions> {
|
||||
fn haveNCharacters(&self, index: usize, n: usize) -> Result<bool> {
|
||||
self.internal.haveNCharacters(index, n)
|
||||
}
|
||||
fn isFNC1(&self, index: usize) -> Result<bool, Exceptions> {
|
||||
fn isFNC1(&self, index: usize) -> Result<bool> {
|
||||
self.internal.isFNC1(index)
|
||||
}
|
||||
fn getECIValue(&self, index: usize) -> Result<i32, Exceptions> {
|
||||
fn getECIValue(&self, index: usize) -> Result<i32> {
|
||||
self.internal.getECIValue(index)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::{Dimension, Exceptions};
|
||||
|
||||
use super::SymbolShapeHint;
|
||||
@@ -122,7 +123,7 @@ impl SymbolInfo {
|
||||
new_symbol
|
||||
}
|
||||
|
||||
fn getHorizontalDataRegions(&self) -> Result<u32, Exceptions> {
|
||||
fn getHorizontalDataRegions(&self) -> Result<u32> {
|
||||
match self.dataRegions {
|
||||
1 => Ok(1),
|
||||
2 | 4 => Ok(2),
|
||||
@@ -134,7 +135,7 @@ impl SymbolInfo {
|
||||
}
|
||||
}
|
||||
|
||||
fn getVerticalDataRegions(&self) -> Result<u32, Exceptions> {
|
||||
fn getVerticalDataRegions(&self) -> Result<u32> {
|
||||
match self.dataRegions {
|
||||
1 | 2 => Ok(1),
|
||||
4 => Ok(2),
|
||||
@@ -146,19 +147,19 @@ impl SymbolInfo {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn getSymbolDataWidth(&self) -> Result<u32, Exceptions> {
|
||||
pub fn getSymbolDataWidth(&self) -> Result<u32> {
|
||||
Ok(self.getHorizontalDataRegions()? * self.matrixWidth)
|
||||
}
|
||||
|
||||
pub fn getSymbolDataHeight(&self) -> Result<u32, Exceptions> {
|
||||
pub fn getSymbolDataHeight(&self) -> Result<u32> {
|
||||
Ok(self.getVerticalDataRegions()? * self.matrixHeight)
|
||||
}
|
||||
|
||||
pub fn getSymbolWidth(&self) -> Result<u32, Exceptions> {
|
||||
pub fn getSymbolWidth(&self) -> Result<u32> {
|
||||
Ok(self.getSymbolDataWidth()? + (self.getHorizontalDataRegions()? * 2))
|
||||
}
|
||||
|
||||
pub fn getSymbolHeight(&self) -> Result<u32, Exceptions> {
|
||||
pub fn getSymbolHeight(&self) -> Result<u32> {
|
||||
Ok(self.getSymbolDataHeight()? + (self.getVerticalDataRegions()? * 2))
|
||||
}
|
||||
|
||||
@@ -236,7 +237,7 @@ impl<'a> SymbolInfoLookup<'a> {
|
||||
self.0 = Some(override_symbols);
|
||||
}
|
||||
|
||||
pub fn lookup(&self, dataCodewords: u32) -> Result<Option<&'a SymbolInfo>, Exceptions> {
|
||||
pub fn lookup(&self, dataCodewords: u32) -> Result<Option<&'a SymbolInfo>> {
|
||||
self.lookup_with_codewords_shape_fail(dataCodewords, SymbolShapeHint::FORCE_NONE, true)
|
||||
}
|
||||
|
||||
@@ -244,7 +245,7 @@ impl<'a> SymbolInfoLookup<'a> {
|
||||
&self,
|
||||
dataCodewords: u32,
|
||||
shape: SymbolShapeHint,
|
||||
) -> Result<Option<&'a SymbolInfo>, Exceptions> {
|
||||
) -> Result<Option<&'a SymbolInfo>> {
|
||||
self.lookup_with_codewords_shape_fail(dataCodewords, shape, true)
|
||||
}
|
||||
|
||||
@@ -253,7 +254,7 @@ impl<'a> SymbolInfoLookup<'a> {
|
||||
dataCodewords: u32,
|
||||
allowRectangular: bool,
|
||||
fail: bool,
|
||||
) -> Result<Option<&'a SymbolInfo>, Exceptions> {
|
||||
) -> Result<Option<&'a SymbolInfo>> {
|
||||
let shape = if allowRectangular {
|
||||
SymbolShapeHint::FORCE_NONE
|
||||
} else {
|
||||
@@ -267,7 +268,7 @@ impl<'a> SymbolInfoLookup<'a> {
|
||||
dataCodewords: u32,
|
||||
shape: SymbolShapeHint,
|
||||
fail: bool,
|
||||
) -> Result<Option<&'a SymbolInfo>, Exceptions> {
|
||||
) -> Result<Option<&'a SymbolInfo>> {
|
||||
self.lookup_with_codewords_shape_size_fail(dataCodewords, shape, &None, &None, fail)
|
||||
}
|
||||
|
||||
@@ -279,7 +280,7 @@ impl<'a> SymbolInfoLookup<'a> {
|
||||
maxSize: &Option<Dimension>,
|
||||
fail: bool,
|
||||
// alternate_symbols_chart: Option<&'a Vec<SymbolInfo>>,
|
||||
) -> Result<Option<&'a SymbolInfo>, Exceptions> {
|
||||
) -> Result<Option<&'a SymbolInfo>> {
|
||||
let symbol_search_chart: &Vec<SymbolInfo> = if self.0.is_none() {
|
||||
&PROD_SYMBOLS
|
||||
} else {
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
|
||||
use super::{high_level_encoder, C40Encoder, Encoder};
|
||||
use crate::common::Result;
|
||||
|
||||
pub struct TextEncoder(C40Encoder);
|
||||
impl Encoder for TextEncoder {
|
||||
@@ -22,7 +23,7 @@ impl Encoder for TextEncoder {
|
||||
high_level_encoder::TEXT_ENCODATION
|
||||
}
|
||||
|
||||
fn encode(&self, context: &mut super::EncoderContext) -> Result<(), crate::Exceptions> {
|
||||
fn encode(&self, context: &mut super::EncoderContext) -> Result<()> {
|
||||
self.0.encode_with_encode_char_fn(
|
||||
context,
|
||||
&Self::encodeChar,
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use crate::common::Result;
|
||||
use crate::Exceptions;
|
||||
|
||||
use super::{high_level_encoder, C40Encoder, Encoder, EncoderContext};
|
||||
@@ -24,7 +25,7 @@ impl Encoder for X12Encoder {
|
||||
high_level_encoder::X12_ENCODATION
|
||||
}
|
||||
|
||||
fn encode(&self, context: &mut super::EncoderContext) -> Result<(), crate::Exceptions> {
|
||||
fn encode(&self, context: &mut super::EncoderContext) -> Result<()> {
|
||||
//step C
|
||||
let mut buffer = String::new();
|
||||
while context.hasMoreCharacters() {
|
||||
@@ -58,7 +59,7 @@ impl X12Encoder {
|
||||
Self(C40Encoder::new())
|
||||
}
|
||||
|
||||
fn encodeChar(c: char, sb: &mut String) -> Result<u32, Exceptions> {
|
||||
fn encodeChar(c: char, sb: &mut String) -> Result<u32> {
|
||||
match c {
|
||||
'\r' => sb.push('\0'),
|
||||
'*' => sb.push('\u{1}'),
|
||||
@@ -77,7 +78,7 @@ impl X12Encoder {
|
||||
Ok(1)
|
||||
}
|
||||
|
||||
fn handleEOD(context: &mut EncoderContext, buffer: &mut str) -> Result<(), Exceptions> {
|
||||
fn handleEOD(context: &mut EncoderContext, buffer: &mut str) -> Result<()> {
|
||||
context.updateSymbolInfo();
|
||||
let available = context
|
||||
.getSymbolInfo()
|
||||
|
||||
Reference in New Issue
Block a user