mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-28 05:12:34 +00:00
incomplete port of decoder
This commit is contained in:
@@ -1,443 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2007 ZXing authors
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.google.zxing.datamatrix.decoder;
|
|
||||||
|
|
||||||
import com.google.zxing.FormatException;
|
|
||||||
import com.google.zxing.common.BitMatrix;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author bbrown@google.com (Brian Brown)
|
|
||||||
*/
|
|
||||||
final class BitMatrixParser {
|
|
||||||
|
|
||||||
private final BitMatrix mappingBitMatrix;
|
|
||||||
private final BitMatrix readMappingMatrix;
|
|
||||||
private final Version version;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param bitMatrix {@link BitMatrix} to parse
|
|
||||||
* @throws FormatException if dimension is < 8 or > 144 or not 0 mod 2
|
|
||||||
*/
|
|
||||||
BitMatrixParser(BitMatrix bitMatrix) throws FormatException {
|
|
||||||
int dimension = bitMatrix.getHeight();
|
|
||||||
if (dimension < 8 || dimension > 144 || (dimension & 0x01) != 0) {
|
|
||||||
throw FormatException.getFormatInstance();
|
|
||||||
}
|
|
||||||
|
|
||||||
version = readVersion(bitMatrix);
|
|
||||||
this.mappingBitMatrix = extractDataRegion(bitMatrix);
|
|
||||||
this.readMappingMatrix = new BitMatrix(this.mappingBitMatrix.getWidth(), this.mappingBitMatrix.getHeight());
|
|
||||||
}
|
|
||||||
|
|
||||||
Version getVersion() {
|
|
||||||
return version;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>Creates the version object based on the dimension of the original bit matrix from
|
|
||||||
* the datamatrix code.</p>
|
|
||||||
*
|
|
||||||
* <p>See ISO 16022:2006 Table 7 - ECC 200 symbol attributes</p>
|
|
||||||
*
|
|
||||||
* @param bitMatrix Original {@link BitMatrix} including alignment patterns
|
|
||||||
* @return {@link Version} encapsulating the Data Matrix Code's "version"
|
|
||||||
* @throws FormatException if the dimensions of the mapping matrix are not valid
|
|
||||||
* Data Matrix dimensions.
|
|
||||||
*/
|
|
||||||
private static Version readVersion(BitMatrix bitMatrix) throws FormatException {
|
|
||||||
int numRows = bitMatrix.getHeight();
|
|
||||||
int numColumns = bitMatrix.getWidth();
|
|
||||||
return Version.getVersionForDimensions(numRows, numColumns);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>Reads the bits in the {@link BitMatrix} representing the mapping matrix (No alignment patterns)
|
|
||||||
* in the correct order in order to reconstitute the codewords bytes contained within the
|
|
||||||
* Data Matrix Code.</p>
|
|
||||||
*
|
|
||||||
* @return bytes encoded within the Data Matrix Code
|
|
||||||
* @throws FormatException if the exact number of bytes expected is not read
|
|
||||||
*/
|
|
||||||
byte[] readCodewords() throws FormatException {
|
|
||||||
|
|
||||||
byte[] result = new byte[version.getTotalCodewords()];
|
|
||||||
int resultOffset = 0;
|
|
||||||
|
|
||||||
int row = 4;
|
|
||||||
int column = 0;
|
|
||||||
|
|
||||||
int numRows = mappingBitMatrix.getHeight();
|
|
||||||
int numColumns = mappingBitMatrix.getWidth();
|
|
||||||
|
|
||||||
boolean corner1Read = false;
|
|
||||||
boolean corner2Read = false;
|
|
||||||
boolean corner3Read = false;
|
|
||||||
boolean corner4Read = false;
|
|
||||||
|
|
||||||
// Read all of the codewords
|
|
||||||
do {
|
|
||||||
// Check the four corner cases
|
|
||||||
if ((row == numRows) && (column == 0) && !corner1Read) {
|
|
||||||
result[resultOffset++] = (byte) readCorner1(numRows, numColumns);
|
|
||||||
row -= 2;
|
|
||||||
column += 2;
|
|
||||||
corner1Read = true;
|
|
||||||
} else if ((row == numRows - 2) && (column == 0) && ((numColumns & 0x03) != 0) && !corner2Read) {
|
|
||||||
result[resultOffset++] = (byte) readCorner2(numRows, numColumns);
|
|
||||||
row -= 2;
|
|
||||||
column += 2;
|
|
||||||
corner2Read = true;
|
|
||||||
} else if ((row == numRows + 4) && (column == 2) && ((numColumns & 0x07) == 0) && !corner3Read) {
|
|
||||||
result[resultOffset++] = (byte) readCorner3(numRows, numColumns);
|
|
||||||
row -= 2;
|
|
||||||
column += 2;
|
|
||||||
corner3Read = true;
|
|
||||||
} else if ((row == numRows - 2) && (column == 0) && ((numColumns & 0x07) == 4) && !corner4Read) {
|
|
||||||
result[resultOffset++] = (byte) readCorner4(numRows, numColumns);
|
|
||||||
row -= 2;
|
|
||||||
column += 2;
|
|
||||||
corner4Read = true;
|
|
||||||
} else {
|
|
||||||
// Sweep upward diagonally to the right
|
|
||||||
do {
|
|
||||||
if ((row < numRows) && (column >= 0) && !readMappingMatrix.get(column, row)) {
|
|
||||||
result[resultOffset++] = (byte) readUtah(row, column, numRows, numColumns);
|
|
||||||
}
|
|
||||||
row -= 2;
|
|
||||||
column += 2;
|
|
||||||
} while ((row >= 0) && (column < numColumns));
|
|
||||||
row += 1;
|
|
||||||
column += 3;
|
|
||||||
|
|
||||||
// Sweep downward diagonally to the left
|
|
||||||
do {
|
|
||||||
if ((row >= 0) && (column < numColumns) && !readMappingMatrix.get(column, row)) {
|
|
||||||
result[resultOffset++] = (byte) readUtah(row, column, numRows, numColumns);
|
|
||||||
}
|
|
||||||
row += 2;
|
|
||||||
column -= 2;
|
|
||||||
} while ((row < numRows) && (column >= 0));
|
|
||||||
row += 3;
|
|
||||||
column += 1;
|
|
||||||
}
|
|
||||||
} while ((row < numRows) || (column < numColumns));
|
|
||||||
|
|
||||||
if (resultOffset != version.getTotalCodewords()) {
|
|
||||||
throw FormatException.getFormatInstance();
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>Reads a bit of the mapping matrix accounting for boundary wrapping.</p>
|
|
||||||
*
|
|
||||||
* @param row Row to read in the mapping matrix
|
|
||||||
* @param column Column to read in the mapping matrix
|
|
||||||
* @param numRows Number of rows in the mapping matrix
|
|
||||||
* @param numColumns Number of columns in the mapping matrix
|
|
||||||
* @return value of the given bit in the mapping matrix
|
|
||||||
*/
|
|
||||||
private boolean readModule(int row, int column, int numRows, int numColumns) {
|
|
||||||
// Adjust the row and column indices based on boundary wrapping
|
|
||||||
if (row < 0) {
|
|
||||||
row += numRows;
|
|
||||||
column += 4 - ((numRows + 4) & 0x07);
|
|
||||||
}
|
|
||||||
if (column < 0) {
|
|
||||||
column += numColumns;
|
|
||||||
row += 4 - ((numColumns + 4) & 0x07);
|
|
||||||
}
|
|
||||||
if (row >= numRows) {
|
|
||||||
row -= numRows;
|
|
||||||
}
|
|
||||||
readMappingMatrix.set(column, row);
|
|
||||||
return mappingBitMatrix.get(column, row);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>Reads the 8 bits of the standard Utah-shaped pattern.</p>
|
|
||||||
*
|
|
||||||
* <p>See ISO 16022:2006, 5.8.1 Figure 6</p>
|
|
||||||
*
|
|
||||||
* @param row Current row in the mapping matrix, anchored at the 8th bit (LSB) of the pattern
|
|
||||||
* @param column Current column in the mapping matrix, anchored at the 8th bit (LSB) of the pattern
|
|
||||||
* @param numRows Number of rows in the mapping matrix
|
|
||||||
* @param numColumns Number of columns in the mapping matrix
|
|
||||||
* @return byte from the utah shape
|
|
||||||
*/
|
|
||||||
private int readUtah(int row, int column, int numRows, int numColumns) {
|
|
||||||
int currentByte = 0;
|
|
||||||
if (readModule(row - 2, column - 2, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(row - 2, column - 1, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(row - 1, column - 2, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(row - 1, column - 1, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(row - 1, column, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(row, column - 2, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(row, column - 1, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(row, column, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
return currentByte;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>Reads the 8 bits of the special corner condition 1.</p>
|
|
||||||
*
|
|
||||||
* <p>See ISO 16022:2006, Figure F.3</p>
|
|
||||||
*
|
|
||||||
* @param numRows Number of rows in the mapping matrix
|
|
||||||
* @param numColumns Number of columns in the mapping matrix
|
|
||||||
* @return byte from the Corner condition 1
|
|
||||||
*/
|
|
||||||
private int readCorner1(int numRows, int numColumns) {
|
|
||||||
int currentByte = 0;
|
|
||||||
if (readModule(numRows - 1, 0, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(numRows - 1, 1, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(numRows - 1, 2, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(0, numColumns - 2, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(0, numColumns - 1, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(1, numColumns - 1, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(2, numColumns - 1, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(3, numColumns - 1, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
return currentByte;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>Reads the 8 bits of the special corner condition 2.</p>
|
|
||||||
*
|
|
||||||
* <p>See ISO 16022:2006, Figure F.4</p>
|
|
||||||
*
|
|
||||||
* @param numRows Number of rows in the mapping matrix
|
|
||||||
* @param numColumns Number of columns in the mapping matrix
|
|
||||||
* @return byte from the Corner condition 2
|
|
||||||
*/
|
|
||||||
private int readCorner2(int numRows, int numColumns) {
|
|
||||||
int currentByte = 0;
|
|
||||||
if (readModule(numRows - 3, 0, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(numRows - 2, 0, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(numRows - 1, 0, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(0, numColumns - 4, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(0, numColumns - 3, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(0, numColumns - 2, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(0, numColumns - 1, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(1, numColumns - 1, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
return currentByte;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>Reads the 8 bits of the special corner condition 3.</p>
|
|
||||||
*
|
|
||||||
* <p>See ISO 16022:2006, Figure F.5</p>
|
|
||||||
*
|
|
||||||
* @param numRows Number of rows in the mapping matrix
|
|
||||||
* @param numColumns Number of columns in the mapping matrix
|
|
||||||
* @return byte from the Corner condition 3
|
|
||||||
*/
|
|
||||||
private int readCorner3(int numRows, int numColumns) {
|
|
||||||
int currentByte = 0;
|
|
||||||
if (readModule(numRows - 1, 0, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(numRows - 1, numColumns - 1, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(0, numColumns - 3, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(0, numColumns - 2, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(0, numColumns - 1, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(1, numColumns - 3, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(1, numColumns - 2, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(1, numColumns - 1, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
return currentByte;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>Reads the 8 bits of the special corner condition 4.</p>
|
|
||||||
*
|
|
||||||
* <p>See ISO 16022:2006, Figure F.6</p>
|
|
||||||
*
|
|
||||||
* @param numRows Number of rows in the mapping matrix
|
|
||||||
* @param numColumns Number of columns in the mapping matrix
|
|
||||||
* @return byte from the Corner condition 4
|
|
||||||
*/
|
|
||||||
private int readCorner4(int numRows, int numColumns) {
|
|
||||||
int currentByte = 0;
|
|
||||||
if (readModule(numRows - 3, 0, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(numRows - 2, 0, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(numRows - 1, 0, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(0, numColumns - 2, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(0, numColumns - 1, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(1, numColumns - 1, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(2, numColumns - 1, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
currentByte <<= 1;
|
|
||||||
if (readModule(3, numColumns - 1, numRows, numColumns)) {
|
|
||||||
currentByte |= 1;
|
|
||||||
}
|
|
||||||
return currentByte;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>Extracts the data region from a {@link BitMatrix} that contains
|
|
||||||
* alignment patterns.</p>
|
|
||||||
*
|
|
||||||
* @param bitMatrix Original {@link BitMatrix} with alignment patterns
|
|
||||||
* @return BitMatrix that has the alignment patterns removed
|
|
||||||
*/
|
|
||||||
private BitMatrix extractDataRegion(BitMatrix bitMatrix) {
|
|
||||||
int symbolSizeRows = version.getSymbolSizeRows();
|
|
||||||
int symbolSizeColumns = version.getSymbolSizeColumns();
|
|
||||||
|
|
||||||
if (bitMatrix.getHeight() != symbolSizeRows) {
|
|
||||||
throw new IllegalArgumentException("Dimension of bitMatrix must match the version size");
|
|
||||||
}
|
|
||||||
|
|
||||||
int dataRegionSizeRows = version.getDataRegionSizeRows();
|
|
||||||
int dataRegionSizeColumns = version.getDataRegionSizeColumns();
|
|
||||||
|
|
||||||
int numDataRegionsRow = symbolSizeRows / dataRegionSizeRows;
|
|
||||||
int numDataRegionsColumn = symbolSizeColumns / dataRegionSizeColumns;
|
|
||||||
|
|
||||||
int sizeDataRegionRow = numDataRegionsRow * dataRegionSizeRows;
|
|
||||||
int sizeDataRegionColumn = numDataRegionsColumn * dataRegionSizeColumns;
|
|
||||||
|
|
||||||
BitMatrix bitMatrixWithoutAlignment = new BitMatrix(sizeDataRegionColumn, sizeDataRegionRow);
|
|
||||||
for (int dataRegionRow = 0; dataRegionRow < numDataRegionsRow; ++dataRegionRow) {
|
|
||||||
int dataRegionRowOffset = dataRegionRow * dataRegionSizeRows;
|
|
||||||
for (int dataRegionColumn = 0; dataRegionColumn < numDataRegionsColumn; ++dataRegionColumn) {
|
|
||||||
int dataRegionColumnOffset = dataRegionColumn * dataRegionSizeColumns;
|
|
||||||
for (int i = 0; i < dataRegionSizeRows; ++i) {
|
|
||||||
int readRowOffset = dataRegionRow * (dataRegionSizeRows + 2) + 1 + i;
|
|
||||||
int writeRowOffset = dataRegionRowOffset + i;
|
|
||||||
for (int j = 0; j < dataRegionSizeColumns; ++j) {
|
|
||||||
int readColumnOffset = dataRegionColumn * (dataRegionSizeColumns + 2) + 1 + j;
|
|
||||||
if (bitMatrix.get(readColumnOffset, readRowOffset)) {
|
|
||||||
int writeColumnOffset = dataRegionColumnOffset + j;
|
|
||||||
bitMatrixWithoutAlignment.set(writeColumnOffset, writeRowOffset);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return bitMatrixWithoutAlignment;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
495
src/datamatrix/decoder/bit_matrix_parser.rs
Normal file
495
src/datamatrix/decoder/bit_matrix_parser.rs
Normal file
@@ -0,0 +1,495 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2007 ZXing authors
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use crate::{common::BitMatrix, Exceptions};
|
||||||
|
|
||||||
|
use super::{Version, VersionRef};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author bbrown@google.com (Brian Brown)
|
||||||
|
*/
|
||||||
|
pub struct BitMatrixParser {
|
||||||
|
mappingBitMatrix: BitMatrix,
|
||||||
|
readMappingMatrix: BitMatrix,
|
||||||
|
version: VersionRef,
|
||||||
|
}
|
||||||
|
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> {
|
||||||
|
let dimension = bitMatrix.getHeight();
|
||||||
|
if dimension < 8 || dimension > 144 || (dimension & 0x01) != 0 {
|
||||||
|
return Err(Exceptions::FormatException("".to_owned()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let version = Self::readVersion(bitMatrix)?;
|
||||||
|
let mappingBitMatrix = Self::extractDataRegion(bitMatrix, version)?;
|
||||||
|
let readMappingMatrix =
|
||||||
|
BitMatrix::new(mappingBitMatrix.getWidth(), mappingBitMatrix.getHeight())?;
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
mappingBitMatrix,
|
||||||
|
readMappingMatrix,
|
||||||
|
version,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getVersion(&self) -> &Version {
|
||||||
|
&self.version
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Creates the version object based on the dimension of the original bit matrix from
|
||||||
|
* the datamatrix code.</p>
|
||||||
|
*
|
||||||
|
* <p>See ISO 16022:2006 Table 7 - ECC 200 symbol attributes</p>
|
||||||
|
*
|
||||||
|
* @param bitMatrix Original {@link BitMatrix} including alignment patterns
|
||||||
|
* @return {@link Version} encapsulating the Data Matrix Code's "version"
|
||||||
|
* @throws FormatException if the dimensions of the mapping matrix are not valid
|
||||||
|
* Data Matrix dimensions.
|
||||||
|
*/
|
||||||
|
fn readVersion(bitMatrix: &BitMatrix) -> Result<VersionRef, Exceptions> {
|
||||||
|
let numRows = bitMatrix.getHeight();
|
||||||
|
let numColumns = bitMatrix.getWidth();
|
||||||
|
Version::getVersionForDimensions(numRows, numColumns)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Reads the bits in the {@link BitMatrix} representing the mapping matrix (No alignment patterns)
|
||||||
|
* in the correct order in order to reconstitute the codewords bytes contained within the
|
||||||
|
* Data Matrix Code.</p>
|
||||||
|
*
|
||||||
|
* @return bytes encoded within the Data Matrix Code
|
||||||
|
* @throws FormatException if the exact number of bytes expected is not read
|
||||||
|
*/
|
||||||
|
pub fn readCodewords(&self) -> Result<Vec<u8>, Exceptions> {
|
||||||
|
let result = vec![0u8; self.version.getTotalCodewords() as usize];
|
||||||
|
let resultOffset = 0;
|
||||||
|
|
||||||
|
let row = 4;
|
||||||
|
let column = 0_usize;
|
||||||
|
|
||||||
|
let numRows = self.mappingBitMatrix.getHeight() as usize;
|
||||||
|
let numColumns = self.mappingBitMatrix.getWidth() as usize;
|
||||||
|
|
||||||
|
let corner1Read = false;
|
||||||
|
let corner2Read = false;
|
||||||
|
let corner3Read = false;
|
||||||
|
let corner4Read = false;
|
||||||
|
|
||||||
|
// Read all of the codewords
|
||||||
|
loop {
|
||||||
|
// Check the four corner cases
|
||||||
|
if (row == numRows) && (column == 0) && !corner1Read {
|
||||||
|
result[resultOffset] = self.readCorner1(numRows, numColumns) as u8;
|
||||||
|
resultOffset += 1;
|
||||||
|
row -= 2;
|
||||||
|
column += 2;
|
||||||
|
corner1Read = true;
|
||||||
|
} else if (row == numRows - 2)
|
||||||
|
&& (column == 0)
|
||||||
|
&& ((numColumns & 0x03) != 0)
|
||||||
|
&& !corner2Read
|
||||||
|
{
|
||||||
|
result[resultOffset] = self.readCorner2(numRows, numColumns) as u8;
|
||||||
|
resultOffset += 1;
|
||||||
|
row -= 2;
|
||||||
|
column += 2;
|
||||||
|
corner2Read = true;
|
||||||
|
} else if (row == numRows + 4)
|
||||||
|
&& (column == 2)
|
||||||
|
&& ((numColumns & 0x07) == 0)
|
||||||
|
&& !corner3Read
|
||||||
|
{
|
||||||
|
result[resultOffset] = self.readCorner3(numRows, numColumns) as u8;
|
||||||
|
resultOffset += 1;
|
||||||
|
row -= 2;
|
||||||
|
column += 2;
|
||||||
|
corner3Read = true;
|
||||||
|
} else if (row == numRows - 2)
|
||||||
|
&& (column == 0)
|
||||||
|
&& ((numColumns & 0x07) == 4)
|
||||||
|
&& !corner4Read
|
||||||
|
{
|
||||||
|
result[resultOffset] = self.readCorner4(numRows, numColumns) as u8;
|
||||||
|
resultOffset += 1;
|
||||||
|
row -= 2;
|
||||||
|
column += 2;
|
||||||
|
corner4Read = true;
|
||||||
|
} else {
|
||||||
|
// Sweep upward diagonally to the right
|
||||||
|
loop {
|
||||||
|
if (row < numRows)
|
||||||
|
&& (column >= 0)
|
||||||
|
&& !self.readMappingMatrix.get(column as u32, row as u32)
|
||||||
|
{
|
||||||
|
result[resultOffset] =
|
||||||
|
self.readUtah(row, column, numRows, numColumns) as u8;
|
||||||
|
resultOffset += 1;
|
||||||
|
}
|
||||||
|
row -= 2;
|
||||||
|
column += 2;
|
||||||
|
if !((row >= 0) && (column < numColumns)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
row += 1;
|
||||||
|
column += 3;
|
||||||
|
|
||||||
|
// Sweep downward diagonally to the left
|
||||||
|
loop {
|
||||||
|
if (row >= 0)
|
||||||
|
&& (column < numColumns)
|
||||||
|
&& !self.readMappingMatrix.get(column as u32, row as u32)
|
||||||
|
{
|
||||||
|
result[resultOffset] =
|
||||||
|
self.readUtah(row, column, numRows, numColumns) as u8;
|
||||||
|
resultOffset += 1;
|
||||||
|
}
|
||||||
|
row += 2;
|
||||||
|
column -= 2;
|
||||||
|
|
||||||
|
if !((row < numRows) && (column >= 0)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
row += 3;
|
||||||
|
column += 1;
|
||||||
|
}
|
||||||
|
if !((row < numRows) || (column < numColumns)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if resultOffset != self.version.getTotalCodewords() as usize {
|
||||||
|
return Err(Exceptions::FormatException("".to_owned()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Reads a bit of the mapping matrix accounting for boundary wrapping.</p>
|
||||||
|
*
|
||||||
|
* @param row Row to read in the mapping matrix
|
||||||
|
* @param column Column to read in the mapping matrix
|
||||||
|
* @param numRows Number of rows in the mapping matrix
|
||||||
|
* @param numColumns Number of columns in the mapping matrix
|
||||||
|
* @return value of the given bit in the mapping matrix
|
||||||
|
*/
|
||||||
|
fn readModule(&self, row: usize, column: usize, numRows: usize, numColumns: usize) -> bool {
|
||||||
|
// Adjust the row and column indices based on boundary wrapping
|
||||||
|
if row < 0 {
|
||||||
|
row += numRows;
|
||||||
|
column += 4 - ((numRows + 4) & 0x07);
|
||||||
|
}
|
||||||
|
if column < 0 {
|
||||||
|
column += numColumns;
|
||||||
|
row += 4 - ((numColumns + 4) & 0x07);
|
||||||
|
}
|
||||||
|
if row >= numRows {
|
||||||
|
row -= numRows;
|
||||||
|
}
|
||||||
|
self.readMappingMatrix.set(column as u32, row as u32);
|
||||||
|
|
||||||
|
self.mappingBitMatrix.get(column as u32, row as u32)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Reads the 8 bits of the standard Utah-shaped pattern.</p>
|
||||||
|
*
|
||||||
|
* <p>See ISO 16022:2006, 5.8.1 Figure 6</p>
|
||||||
|
*
|
||||||
|
* @param row Current row in the mapping matrix, anchored at the 8th bit (LSB) of the pattern
|
||||||
|
* @param column Current column in the mapping matrix, anchored at the 8th bit (LSB) of the pattern
|
||||||
|
* @param numRows Number of rows in the mapping matrix
|
||||||
|
* @param numColumns Number of columns in the mapping matrix
|
||||||
|
* @return byte from the utah shape
|
||||||
|
*/
|
||||||
|
fn readUtah(&self, row: usize, column: usize, numRows: usize, numColumns: usize) -> u32 {
|
||||||
|
let mut currentByte = 0;
|
||||||
|
if self.readModule(row - 2, column - 2, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(row - 2, column - 1, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(row - 1, column - 2, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(row - 1, column - 1, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(row - 1, column, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(row, column - 2, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(row, column - 1, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(row, column, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
return currentByte;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Reads the 8 bits of the special corner condition 1.</p>
|
||||||
|
*
|
||||||
|
* <p>See ISO 16022:2006, Figure F.3</p>
|
||||||
|
*
|
||||||
|
* @param numRows Number of rows in the mapping matrix
|
||||||
|
* @param numColumns Number of columns in the mapping matrix
|
||||||
|
* @return byte from the Corner condition 1
|
||||||
|
*/
|
||||||
|
fn readCorner1(&self, numRows: usize, numColumns: usize) -> u32 {
|
||||||
|
let mut currentByte = 0;
|
||||||
|
if self.readModule(numRows - 1, 0, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(numRows - 1, 1, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(numRows - 1, 2, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(0, numColumns - 2, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(0, numColumns - 1, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(1, numColumns - 1, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(2, numColumns - 1, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(3, numColumns - 1, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
return currentByte;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Reads the 8 bits of the special corner condition 2.</p>
|
||||||
|
*
|
||||||
|
* <p>See ISO 16022:2006, Figure F.4</p>
|
||||||
|
*
|
||||||
|
* @param numRows Number of rows in the mapping matrix
|
||||||
|
* @param numColumns Number of columns in the mapping matrix
|
||||||
|
* @return byte from the Corner condition 2
|
||||||
|
*/
|
||||||
|
fn readCorner2(&self, numRows: usize, numColumns: usize) -> u32 {
|
||||||
|
let mut currentByte = 0;
|
||||||
|
if self.readModule(numRows - 3, 0, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(numRows - 2, 0, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(numRows - 1, 0, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(0, numColumns - 4, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(0, numColumns - 3, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(0, numColumns - 2, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(0, numColumns - 1, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(1, numColumns - 1, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
return currentByte;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Reads the 8 bits of the special corner condition 3.</p>
|
||||||
|
*
|
||||||
|
* <p>See ISO 16022:2006, Figure F.5</p>
|
||||||
|
*
|
||||||
|
* @param numRows Number of rows in the mapping matrix
|
||||||
|
* @param numColumns Number of columns in the mapping matrix
|
||||||
|
* @return byte from the Corner condition 3
|
||||||
|
*/
|
||||||
|
fn readCorner3(&self, numRows: usize, numColumns: usize) -> u32 {
|
||||||
|
let mut currentByte = 0;
|
||||||
|
if self.readModule(numRows - 1, 0, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(numRows - 1, numColumns - 1, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(0, numColumns - 3, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(0, numColumns - 2, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(0, numColumns - 1, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(1, numColumns - 3, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(1, numColumns - 2, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(1, numColumns - 1, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
return currentByte;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Reads the 8 bits of the special corner condition 4.</p>
|
||||||
|
*
|
||||||
|
* <p>See ISO 16022:2006, Figure F.6</p>
|
||||||
|
*
|
||||||
|
* @param numRows Number of rows in the mapping matrix
|
||||||
|
* @param numColumns Number of columns in the mapping matrix
|
||||||
|
* @return byte from the Corner condition 4
|
||||||
|
*/
|
||||||
|
fn readCorner4(&self, numRows: usize, numColumns: usize) -> u32 {
|
||||||
|
let mut currentByte = 0;
|
||||||
|
if self.readModule(numRows - 3, 0, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(numRows - 2, 0, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(numRows - 1, 0, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(0, numColumns - 2, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(0, numColumns - 1, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(1, numColumns - 1, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(2, numColumns - 1, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
currentByte <<= 1;
|
||||||
|
if self.readModule(3, numColumns - 1, numRows, numColumns) {
|
||||||
|
currentByte |= 1;
|
||||||
|
}
|
||||||
|
return currentByte;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Extracts the data region from a {@link BitMatrix} that contains
|
||||||
|
* alignment patterns.</p>
|
||||||
|
*
|
||||||
|
* @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> {
|
||||||
|
let symbolSizeRows = version.getSymbolSizeRows();
|
||||||
|
let symbolSizeColumns = version.getSymbolSizeColumns();
|
||||||
|
|
||||||
|
if bitMatrix.getHeight() != symbolSizeRows {
|
||||||
|
return Err(Exceptions::IllegalArgumentException(
|
||||||
|
"Dimension of bitMatrix must match the version size".to_owned(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let dataRegionSizeRows = version.getDataRegionSizeRows();
|
||||||
|
let dataRegionSizeColumns = version.getDataRegionSizeColumns();
|
||||||
|
|
||||||
|
let numDataRegionsRow = symbolSizeRows / dataRegionSizeRows;
|
||||||
|
let numDataRegionsColumn = symbolSizeColumns / dataRegionSizeColumns;
|
||||||
|
|
||||||
|
let sizeDataRegionRow = numDataRegionsRow * dataRegionSizeRows;
|
||||||
|
let sizeDataRegionColumn = numDataRegionsColumn * dataRegionSizeColumns;
|
||||||
|
|
||||||
|
let bitMatrixWithoutAlignment = BitMatrix::new(sizeDataRegionColumn, sizeDataRegionRow)?;
|
||||||
|
for dataRegionRow in 0..numDataRegionsRow {
|
||||||
|
// for (int dataRegionRow = 0; dataRegionRow < numDataRegionsRow; ++dataRegionRow) {
|
||||||
|
let dataRegionRowOffset = dataRegionRow * dataRegionSizeRows;
|
||||||
|
for dataRegionColumn in 0..numDataRegionsColumn {
|
||||||
|
// for (int dataRegionColumn = 0; dataRegionColumn < numDataRegionsColumn; ++dataRegionColumn) {
|
||||||
|
let dataRegionColumnOffset = dataRegionColumn * dataRegionSizeColumns;
|
||||||
|
for i in 0..dataRegionSizeRows {
|
||||||
|
// for (int i = 0; i < dataRegionSizeRows; ++i) {
|
||||||
|
let readRowOffset = dataRegionRow * (dataRegionSizeRows + 2) + 1 + i;
|
||||||
|
let writeRowOffset = dataRegionRowOffset + i;
|
||||||
|
for j in 0..dataRegionSizeColumns {
|
||||||
|
// for (int j = 0; j < dataRegionSizeColumns; ++j) {
|
||||||
|
let readColumnOffset =
|
||||||
|
dataRegionColumn * (dataRegionSizeColumns + 2) + 1 + j;
|
||||||
|
if bitMatrix.get(readColumnOffset, readRowOffset) {
|
||||||
|
let writeColumnOffset = dataRegionColumnOffset + j;
|
||||||
|
bitMatrixWithoutAlignment.set(writeColumnOffset, writeRowOffset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(bitMatrixWithoutAlignment)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,19 +14,10 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.google.zxing.datamatrix.decoder;
|
use encoding::Encoding;
|
||||||
|
|
||||||
import com.google.zxing.FormatException;
|
use crate::{Exceptions, common::{BitSource, ECIStringBuilder, DecoderRXingResult}};
|
||||||
import com.google.zxing.common.BitSource;
|
|
||||||
import com.google.zxing.common.DecoderRXingResult;
|
|
||||||
import com.google.zxing.common.ECIStringBuilder;
|
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Data Matrix Codes can encode text as bits in one of several modes, and can use multiple modes
|
* <p>Data Matrix Codes can encode text as bits in one of several modes, and can use multiple modes
|
||||||
@@ -37,9 +28,9 @@ import java.util.Set;
|
|||||||
* @author bbrown@google.com (Brian Brown)
|
* @author bbrown@google.com (Brian Brown)
|
||||||
* @author Sean Owen
|
* @author Sean Owen
|
||||||
*/
|
*/
|
||||||
final class DecodedBitStreamParser {
|
|
||||||
|
|
||||||
private enum Mode {
|
#[derive(Debug,PartialEq, Eq,Clone, Copy)]
|
||||||
|
enum Mode {
|
||||||
PAD_ENCODE, // Not really a mode
|
PAD_ENCODE, // Not really a mode
|
||||||
ASCII_ENCODE,
|
ASCII_ENCODE,
|
||||||
C40_ENCODE,
|
C40_ENCODE,
|
||||||
@@ -54,78 +45,71 @@ final class DecodedBitStreamParser {
|
|||||||
* See ISO 16022:2006, Annex C Table C.1
|
* See ISO 16022:2006, Annex C Table C.1
|
||||||
* The C40 Basic Character Set (*'s used for placeholders for the shift values)
|
* The C40 Basic Character Set (*'s used for placeholders for the shift values)
|
||||||
*/
|
*/
|
||||||
private static final char[] C40_BASIC_SET_CHARS = {
|
const C40_BASIC_SET_CHARS : [char;40]= [
|
||||||
'*', '*', '*', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
'*', '*', '*', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
|
||||||
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
|
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
|
||||||
};
|
];
|
||||||
|
|
||||||
private static final char[] C40_SHIFT2_SET_CHARS = {
|
const C40_SHIFT2_SET_CHARS :[char;27] = [
|
||||||
'!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.',
|
'!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.',
|
||||||
'/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_'
|
'/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_'
|
||||||
};
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* See ISO 16022:2006, Annex C Table C.2
|
* See ISO 16022:2006, Annex C Table C.2
|
||||||
* The Text Basic Character Set (*'s used for placeholders for the shift values)
|
* The Text Basic Character Set (*'s used for placeholders for the shift values)
|
||||||
*/
|
*/
|
||||||
private static final char[] TEXT_BASIC_SET_CHARS = {
|
const TEXT_BASIC_SET_CHARS : [char;40]=
|
||||||
'*', '*', '*', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
['*', '*', '*', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
||||||
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
|
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
|
||||||
};
|
];
|
||||||
|
|
||||||
// Shift 2 for Text is the same encoding as C40
|
// Shift 2 for Text is the same encoding as C40
|
||||||
private static final char[] TEXT_SHIFT2_SET_CHARS = C40_SHIFT2_SET_CHARS;
|
const TEXT_SHIFT2_SET_CHARS : [char;27]= C40_SHIFT2_SET_CHARS;
|
||||||
|
|
||||||
private static final char[] TEXT_SHIFT3_SET_CHARS = {
|
const TEXT_SHIFT3_SET_CHARS : [char;32]= [
|
||||||
'`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
|
'`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
|
||||||
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '{', '|', '}', '~', (char) 127
|
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '{', '|', '}', '~', 127 as char
|
||||||
};
|
];
|
||||||
|
|
||||||
private DecodedBitStreamParser() {
|
pub fn decode( bytes: &[u8]) -> Result<DecoderRXingResult,Exceptions> {
|
||||||
}
|
let bits = BitSource::new(bytes);
|
||||||
|
let result = ECIStringBuilder::with_capacity(100);
|
||||||
static DecoderRXingResult decode(byte[] bytes) throws FormatException {
|
let resultTrailer = String::new();
|
||||||
BitSource bits = new BitSource(bytes);
|
let byteSegments = Vec::new();//new ArrayList<>(1);
|
||||||
ECIStringBuilder result = new ECIStringBuilder(100);
|
let mode = Mode::ASCII_ENCODE;
|
||||||
StringBuilder resultTrailer = new StringBuilder(0);
|
|
||||||
List<byte[]> byteSegments = new ArrayList<>(1);
|
|
||||||
Mode mode = Mode.ASCII_ENCODE;
|
|
||||||
// Could look directly at 'bytes', if we're sure of not having to account for multi byte values
|
// Could look directly at 'bytes', if we're sure of not having to account for multi byte values
|
||||||
Set<Integer> fnc1Positions = new HashSet<>();
|
let fnc1Positions = Vec::new();
|
||||||
int symbologyModifier;
|
let symbologyModifier;
|
||||||
boolean isECIencoded = false;
|
let isECIencoded = false;
|
||||||
do {
|
loop {
|
||||||
if (mode == Mode.ASCII_ENCODE) {
|
if mode == Mode::ASCII_ENCODE {
|
||||||
mode = decodeAsciiSegment(bits, result, resultTrailer, fnc1Positions);
|
mode = decodeAsciiSegment(bits, result, resultTrailer, fnc1Positions);
|
||||||
} else {
|
} else {
|
||||||
switch (mode) {
|
match mode {
|
||||||
case C40_ENCODE:
|
Mode::C40_ENCODE=>
|
||||||
decodeC40Segment(bits, result, fnc1Positions);
|
decodeC40Segment(bits, result, fnc1Positions),
|
||||||
break;
|
Mode::TEXT_ENCODE=>
|
||||||
case TEXT_ENCODE:
|
decodeTextSegment(bits, result, fnc1Positions),
|
||||||
decodeTextSegment(bits, result, fnc1Positions);
|
Mode::ANSIX12_ENCODE=>
|
||||||
break;
|
decodeAnsiX12Segment(bits, result),
|
||||||
case ANSIX12_ENCODE:
|
Mode::EDIFACT_ENCODE=>
|
||||||
decodeAnsiX12Segment(bits, result);
|
decodeEdifactSegment(bits, result),
|
||||||
break;
|
Mode::BASE256_ENCODE=>
|
||||||
case EDIFACT_ENCODE:
|
decodeBase256Segment(bits, result, byteSegments),
|
||||||
decodeEdifactSegment(bits, result);
|
Mode::ECI_ENCODE=>{
|
||||||
break;
|
|
||||||
case BASE256_ENCODE:
|
|
||||||
decodeBase256Segment(bits, result, byteSegments);
|
|
||||||
break;
|
|
||||||
case ECI_ENCODE:
|
|
||||||
decodeECISegment(bits, result);
|
decodeECISegment(bits, result);
|
||||||
isECIencoded = true; // ECI detection only, atm continue decoding as ASCII
|
isECIencoded = true; // ECI detection only, atm continue decoding as ASCII
|
||||||
break;
|
},
|
||||||
default:
|
_=>
|
||||||
throw FormatException.getFormatInstance();
|
return Err(Exceptions::FormatException("".to_owned())),
|
||||||
}
|
}
|
||||||
mode = Mode.ASCII_ENCODE;
|
mode = Mode::ASCII_ENCODE;
|
||||||
}
|
}
|
||||||
} while (mode != Mode.PAD_ENCODE && bits.available() > 0);
|
if ! (mode != Mode::PAD_ENCODE && bits.available() > 0) { break }
|
||||||
|
} //while (mode != Mode.PAD_ENCODE && bits.available() > 0);
|
||||||
if (resultTrailer.length() > 0) {
|
if (resultTrailer.length() > 0) {
|
||||||
result.appendCharacters(resultTrailer);
|
result.appendCharacters(resultTrailer);
|
||||||
}
|
}
|
||||||
@@ -159,10 +143,10 @@ final class DecodedBitStreamParser {
|
|||||||
/**
|
/**
|
||||||
* See ISO 16022:2006, 5.2.3 and Annex C, Table C.2
|
* See ISO 16022:2006, 5.2.3 and Annex C, Table C.2
|
||||||
*/
|
*/
|
||||||
private static Mode decodeAsciiSegment(BitSource bits,
|
fn decodeAsciiSegment( bits:&BitSource,
|
||||||
ECIStringBuilder result,
|
result:&ECIStringBuilder,
|
||||||
StringBuilder resultTrailer,
|
resultTrailer:&String,
|
||||||
Set<Integer> fnc1positions) throws FormatException {
|
fnc1positions:&[usize]) -> Result<Mode,Exceptions> {
|
||||||
boolean upperShift = false;
|
boolean upperShift = false;
|
||||||
do {
|
do {
|
||||||
int oneByte = bits.readBits(8);
|
int oneByte = bits.readBits(8);
|
||||||
@@ -233,8 +217,8 @@ final class DecodedBitStreamParser {
|
|||||||
/**
|
/**
|
||||||
* See ISO 16022:2006, 5.2.5 and Annex C, Table C.1
|
* See ISO 16022:2006, 5.2.5 and Annex C, Table C.1
|
||||||
*/
|
*/
|
||||||
private static void decodeC40Segment(BitSource bits, ECIStringBuilder result, Set<Integer> fnc1positions)
|
fn decodeC40Segment( bits:&BitSource, result:&ECIStringBuilder, fnc1positions:&[usize])
|
||||||
throws FormatException {
|
-> Result<(),Exceptions> {
|
||||||
// Three C40 values are encoded in a 16-bit value as
|
// Three C40 values are encoded in a 16-bit value as
|
||||||
// (1600 * C1) + (40 * C2) + C3 + 1
|
// (1600 * C1) + (40 * C2) + C3 + 1
|
||||||
// TODO(bbrown): The Upper Shift with C40 doesn't work in the 4 value scenario all the time
|
// TODO(bbrown): The Upper Shift with C40 doesn't work in the 4 value scenario all the time
|
||||||
@@ -325,153 +309,158 @@ final class DecodedBitStreamParser {
|
|||||||
/**
|
/**
|
||||||
* See ISO 16022:2006, 5.2.6 and Annex C, Table C.2
|
* See ISO 16022:2006, 5.2.6 and Annex C, Table C.2
|
||||||
*/
|
*/
|
||||||
private static void decodeTextSegment(BitSource bits, ECIStringBuilder result, Set<Integer> fnc1positions)
|
fn decodeTextSegment( bits:&BitSource, result:&ECIStringBuilder, fnc1positions:&[usize])
|
||||||
throws FormatException {
|
-> Result<(),Exceptions> {
|
||||||
// Three Text values are encoded in a 16-bit value as
|
// Three Text values are encoded in a 16-bit value as
|
||||||
// (1600 * C1) + (40 * C2) + C3 + 1
|
// (1600 * C1) + (40 * C2) + C3 + 1
|
||||||
// TODO(bbrown): The Upper Shift with Text doesn't work in the 4 value scenario all the time
|
// TODO(bbrown): The Upper Shift with Text doesn't work in the 4 value scenario all the time
|
||||||
boolean upperShift = false;
|
let upperShift = false;
|
||||||
|
|
||||||
int[] cValues = new int[3];
|
let cValues = [0;3];//new int[3];
|
||||||
int shift = 0;
|
let shift = 0;
|
||||||
do {
|
loop {
|
||||||
// If there is only one byte left then it will be encoded as ASCII
|
// If there is only one byte left then it will be encoded as ASCII
|
||||||
if (bits.available() == 8) {
|
if bits.available() == 8 {
|
||||||
return;
|
return Ok(())
|
||||||
}
|
}
|
||||||
int firstByte = bits.readBits(8);
|
let firstByte = bits.readBits(8)?;
|
||||||
if (firstByte == 254) { // Unlatch codeword
|
if firstByte == 254 { // Unlatch codeword
|
||||||
return;
|
return Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
parseTwoBytes(firstByte, bits.readBits(8), cValues);
|
parseTwoBytes(firstByte, bits.readBits(8)?, &cValues);
|
||||||
|
|
||||||
for (int i = 0; i < 3; i++) {
|
for cValue in cValues {
|
||||||
int cValue = cValues[i];
|
// for (int i = 0; i < 3; i++) {
|
||||||
switch (shift) {
|
// int cValue = cValues[i];
|
||||||
case 0:
|
match shift {
|
||||||
if (cValue < 3) {
|
0=>
|
||||||
|
if cValue < 3 {
|
||||||
shift = cValue + 1;
|
shift = cValue + 1;
|
||||||
} else if (cValue < TEXT_BASIC_SET_CHARS.length) {
|
} else if cValue < TEXT_BASIC_SET_CHARS.len() {
|
||||||
char textChar = TEXT_BASIC_SET_CHARS[cValue];
|
let textChar = TEXT_BASIC_SET_CHARS[cValue];
|
||||||
if (upperShift) {
|
if upperShift {
|
||||||
result.append((char) (textChar + 128));
|
result.append_char( (textChar + 128));
|
||||||
upperShift = false;
|
upperShift = false;
|
||||||
} else {
|
} else {
|
||||||
result.append(textChar);
|
result.append(textChar);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw FormatException.getFormatInstance();
|
return Err(Exceptions::FormatException("".to_owned()));
|
||||||
}
|
},
|
||||||
break;
|
1=>
|
||||||
case 1:
|
{if upperShift {
|
||||||
if (upperShift) {
|
result.append_char( (cValue + 128));
|
||||||
result.append((char) (cValue + 128));
|
|
||||||
upperShift = false;
|
upperShift = false;
|
||||||
} else {
|
} else {
|
||||||
result.append((char) cValue);
|
result.append_char( cValue);
|
||||||
}
|
}
|
||||||
shift = 0;
|
shift = 0;},
|
||||||
break;
|
|
||||||
case 2:
|
2=>
|
||||||
// Shift 2 for Text is the same encoding as C40
|
{// Shift 2 for Text is the same encoding as C40
|
||||||
if (cValue < TEXT_SHIFT2_SET_CHARS.length) {
|
if cValue < TEXT_SHIFT2_SET_CHARS.len() {
|
||||||
char textChar = TEXT_SHIFT2_SET_CHARS[cValue];
|
let textChar = TEXT_SHIFT2_SET_CHARS[cValue];
|
||||||
if (upperShift) {
|
if upperShift {
|
||||||
result.append((char) (textChar + 128));
|
result.append_char( (textChar + 128));
|
||||||
upperShift = false;
|
upperShift = false;
|
||||||
} else {
|
} else {
|
||||||
result.append(textChar);
|
result.append_char(textChar);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
switch (cValue) {
|
match cValue {
|
||||||
case 27: // FNC1
|
27=>{ // FNC1
|
||||||
fnc1positions.add(result.length());
|
fnc1positions.push(result.length());
|
||||||
result.append((char) 29); // translate as ASCII 29
|
result.append_char( 29); // translate as ASCII 29
|
||||||
break;
|
},
|
||||||
case 30: // Upper Shift
|
30=> // Upper Shift
|
||||||
upperShift = true;
|
upperShift = true,
|
||||||
break;
|
|
||||||
default:
|
_=>
|
||||||
throw FormatException.getFormatInstance();
|
return Err(Exceptions::FormatException("".to_owned())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
shift = 0;
|
shift = 0;},
|
||||||
break;
|
3=>
|
||||||
case 3:
|
if cValue < TEXT_SHIFT3_SET_CHARS.len() {
|
||||||
if (cValue < TEXT_SHIFT3_SET_CHARS.length) {
|
let textChar = TEXT_SHIFT3_SET_CHARS[cValue];
|
||||||
char textChar = TEXT_SHIFT3_SET_CHARS[cValue];
|
if upperShift {
|
||||||
if (upperShift) {
|
result.append_char( (textChar + 128));
|
||||||
result.append((char) (textChar + 128));
|
|
||||||
upperShift = false;
|
upperShift = false;
|
||||||
} else {
|
} else {
|
||||||
result.append(textChar);
|
result.append_char(textChar);
|
||||||
}
|
}
|
||||||
shift = 0;
|
shift = 0;
|
||||||
} else {
|
} else {
|
||||||
throw FormatException.getFormatInstance();
|
return Err(Exceptions::FormatException("".to_owned()));
|
||||||
}
|
},
|
||||||
break;
|
|
||||||
default:
|
_=>
|
||||||
throw FormatException.getFormatInstance();
|
return Err(Exceptions::FormatException("".to_owned())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (bits.available() > 0);
|
if !(bits.available() > 0){break}
|
||||||
|
} //while (bits.available() > 0);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* See ISO 16022:2006, 5.2.7
|
* See ISO 16022:2006, 5.2.7
|
||||||
*/
|
*/
|
||||||
private static void decodeAnsiX12Segment(BitSource bits,
|
fn decodeAnsiX12Segment( bits:&BitSource,
|
||||||
ECIStringBuilder result) throws FormatException {
|
result:&ECIStringBuilder) -> Result<(),Exceptions> {
|
||||||
// Three ANSI X12 values are encoded in a 16-bit value as
|
// Three ANSI X12 values are encoded in a 16-bit value as
|
||||||
// (1600 * C1) + (40 * C2) + C3 + 1
|
// (1600 * C1) + (40 * C2) + C3 + 1
|
||||||
|
|
||||||
int[] cValues = new int[3];
|
let cValues = [0;3];//new int[3];
|
||||||
do {
|
loop {
|
||||||
// If there is only one byte left then it will be encoded as ASCII
|
// If there is only one byte left then it will be encoded as ASCII
|
||||||
if (bits.available() == 8) {
|
if bits.available() == 8 {
|
||||||
return;
|
return Ok(())
|
||||||
}
|
}
|
||||||
int firstByte = bits.readBits(8);
|
let firstByte = bits.readBits(8)?;
|
||||||
if (firstByte == 254) { // Unlatch codeword
|
if firstByte == 254 { // Unlatch codeword
|
||||||
return;
|
return Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
parseTwoBytes(firstByte, bits.readBits(8), cValues);
|
parseTwoBytes(firstByte, bits.readBits(8)?, &cValues);
|
||||||
|
|
||||||
for (int i = 0; i < 3; i++) {
|
for cValue in cValues {
|
||||||
int cValue = cValues[i];
|
// for (int i = 0; i < 3; i++) {
|
||||||
switch (cValue) {
|
// int cValue = cValues[i];
|
||||||
case 0: // X12 segment terminator <CR>
|
match cValue {
|
||||||
result.append('\r');
|
0=> // X12 segment terminator <CR>
|
||||||
break;
|
result.append_char('\r'),
|
||||||
case 1: // X12 segment separator *
|
|
||||||
result.append('*');
|
1=> // X12 segment separator *
|
||||||
break;
|
result.append_char('*'),
|
||||||
case 2: // X12 sub-element separator >
|
|
||||||
result.append('>');
|
2=> // X12 sub-element separator >
|
||||||
break;
|
result.append_char('>'),
|
||||||
case 3: // space
|
|
||||||
result.append(' ');
|
3=> // space
|
||||||
break;
|
result.append_char(' '),
|
||||||
default:
|
|
||||||
if (cValue < 14) { // 0 - 9
|
_=>
|
||||||
result.append((char) (cValue + 44));
|
if cValue < 14 { // 0 - 9
|
||||||
} else if (cValue < 40) { // A - Z
|
result.append_char( char::from_u32(cValue + 44).unwrap());
|
||||||
result.append((char) (cValue + 51));
|
} else if cValue < 40 { // A - Z
|
||||||
|
result.append_char( char::from_u32(cValue + 51).unwrap());
|
||||||
} else {
|
} else {
|
||||||
throw FormatException.getFormatInstance();
|
return Err(Exceptions::FormatException("".to_owned()))
|
||||||
}
|
},
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (bits.available() > 0);
|
if ! (bits.available() > 0) { break }
|
||||||
|
} //while (bits.available() > 0);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void parseTwoBytes(int firstByte, int secondByte, int[] result) {
|
fn parseTwoBytes( firstByte:u32, secondByte:u32, result:&[u32]) {
|
||||||
int fullBitValue = (firstByte << 8) + secondByte - 1;
|
let fullBitValue = (firstByte << 8) + secondByte - 1;
|
||||||
int temp = fullBitValue / 1600;
|
let temp = fullBitValue / 1600;
|
||||||
result[0] = temp;
|
result[0] = temp;
|
||||||
fullBitValue -= temp * 1600;
|
fullBitValue -= temp * 1600;
|
||||||
temp = fullBitValue / 40;
|
temp = fullBitValue / 40;
|
||||||
@@ -482,84 +471,97 @@ final class DecodedBitStreamParser {
|
|||||||
/**
|
/**
|
||||||
* See ISO 16022:2006, 5.2.8 and Annex C Table C.3
|
* See ISO 16022:2006, 5.2.8 and Annex C Table C.3
|
||||||
*/
|
*/
|
||||||
private static void decodeEdifactSegment(BitSource bits, ECIStringBuilder result) {
|
fn decodeEdifactSegment( bits:&BitSource, result:&ECIStringBuilder) -> Result<(),Exceptions>{
|
||||||
do {
|
loop {
|
||||||
// If there is only two or less bytes left then it will be encoded as ASCII
|
// If there is only two or less bytes left then it will be encoded as ASCII
|
||||||
if (bits.available() <= 16) {
|
if bits.available() <= 16 {
|
||||||
return;
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < 4; i++) {
|
for i in 0..4 {
|
||||||
int edifactValue = bits.readBits(6);
|
// for (int i = 0; i < 4; i++) {
|
||||||
|
let edifactValue = bits.readBits(6)?;
|
||||||
|
|
||||||
// Check for the unlatch character
|
// Check for the unlatch character
|
||||||
if (edifactValue == 0x1F) { // 011111
|
if edifactValue == 0x1F { // 011111
|
||||||
// Read rest of byte, which should be 0, and stop
|
// Read rest of byte, which should be 0, and stop
|
||||||
int bitsLeft = 8 - bits.getBitOffset();
|
let bitsLeft = 8 - bits.getBitOffset();
|
||||||
if (bitsLeft != 8) {
|
if bitsLeft != 8 {
|
||||||
bits.readBits(bitsLeft);
|
bits.readBits(bitsLeft);
|
||||||
}
|
}
|
||||||
return;
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((edifactValue & 0x20) == 0) { // no 1 in the leading (6th) bit
|
if (edifactValue & 0x20) == 0 { // no 1 in the leading (6th) bit
|
||||||
edifactValue |= 0x40; // Add a leading 01 to the 6 bit binary value
|
edifactValue |= 0x40; // Add a leading 01 to the 6 bit binary value
|
||||||
}
|
}
|
||||||
result.append((char) edifactValue);
|
result.append_char( char::from_u32(edifactValue).unwrap());
|
||||||
}
|
}
|
||||||
} while (bits.available() > 0);
|
|
||||||
|
if ! (bits.available() > 0) { break }
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* See ISO 16022:2006, 5.2.9 and Annex B, B.2
|
* See ISO 16022:2006, 5.2.9 and Annex B, B.2
|
||||||
*/
|
*/
|
||||||
private static void decodeBase256Segment(BitSource bits,
|
fn decodeBase256Segment( bits:&BitSource,
|
||||||
ECIStringBuilder result,
|
result:&ECIStringBuilder,
|
||||||
Collection<byte[]> byteSegments)
|
byteSegments:&Vec<Vec<u8>>)
|
||||||
throws FormatException {
|
-> Result<(),Exceptions> {
|
||||||
// Figure out how long the Base 256 Segment is.
|
// Figure out how long the Base 256 Segment is.
|
||||||
int codewordPosition = 1 + bits.getByteOffset(); // position is 1-indexed
|
let codewordPosition = 1 + bits.getByteOffset(); // position is 1-indexed
|
||||||
int d1 = unrandomize255State(bits.readBits(8), codewordPosition++);
|
let d1 = unrandomize255State(bits.readBits(8)?, codewordPosition);
|
||||||
int count;
|
codewordPosition +=1;
|
||||||
if (d1 == 0) { // Read the remainder of the symbol
|
let count;
|
||||||
count = bits.available() / 8;
|
if d1 == 0 { // Read the remainder of the symbol
|
||||||
} else if (d1 < 250) {
|
count = bits.available() as u32 / 8;
|
||||||
|
} else if d1 < 250 {
|
||||||
count = d1;
|
count = d1;
|
||||||
} else {
|
} else {
|
||||||
count = 250 * (d1 - 249) + unrandomize255State(bits.readBits(8), codewordPosition++);
|
count = 250 * (d1 - 249) + unrandomize255State(bits.readBits(8)?, codewordPosition);
|
||||||
|
codewordPosition +=1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We're seeing NegativeArraySizeException errors from users.
|
// We're seeing NegativeArraySizeException errors from users.
|
||||||
if (count < 0) {
|
if (count < 0) {
|
||||||
throw FormatException.getFormatInstance();
|
return Err(Exceptions::FormatException("".to_owned()))
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] bytes = new byte[count];
|
let bytes = vec![0u8;count as usize];
|
||||||
for (int i = 0; i < count; i++) {
|
for i in 0..count as usize {
|
||||||
|
// for (int i = 0; i < count; i++) {
|
||||||
// Have seen this particular error in the wild, such as at
|
// Have seen this particular error in the wild, such as at
|
||||||
// http://www.bcgen.com/demo/IDAutomationStreamingDataMatrix.aspx?MODE=3&D=Fred&PFMT=3&PT=F&X=0.3&O=0&LM=0.2
|
// http://www.bcgen.com/demo/IDAutomationStreamingDataMatrix.aspx?MODE=3&D=Fred&PFMT=3&PT=F&X=0.3&O=0&LM=0.2
|
||||||
if (bits.available() < 8) {
|
if bits.available() < 8 {
|
||||||
throw FormatException.getFormatInstance();
|
return Err(Exceptions::FormatException("".to_owned()))
|
||||||
}
|
}
|
||||||
bytes[i] = (byte) unrandomize255State(bits.readBits(8), codewordPosition++);
|
bytes[i] = unrandomize255State(bits.readBits(8)?, codewordPosition) as u8;
|
||||||
|
codewordPosition+=1;
|
||||||
}
|
}
|
||||||
byteSegments.add(bytes);
|
byteSegments.push(bytes);
|
||||||
result.append(new String(bytes, StandardCharsets.ISO_8859_1));
|
result.append_string(&encoding::all::ISO_8859_1.decode(&bytes, encoding::DecoderTrap::Strict).expect("decode"));
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* See ISO 16022:2007, 5.4.1
|
* See ISO 16022:2007, 5.4.1
|
||||||
*/
|
*/
|
||||||
private static void decodeECISegment(BitSource bits,
|
fn decodeECISegment( bits:&BitSource,
|
||||||
ECIStringBuilder result)
|
result:&ECIStringBuilder)
|
||||||
throws FormatException {
|
-> Result<(),Exceptions> {
|
||||||
if (bits.available() < 8) {
|
if bits.available() < 8 {
|
||||||
throw FormatException.getFormatInstance();
|
return Err(Exceptions::FormatException("".to_owned()))
|
||||||
}
|
}
|
||||||
int c1 = bits.readBits(8);
|
let c1 = bits.readBits(8)?;
|
||||||
if (c1 <= 127) {
|
if c1 <= 127 {
|
||||||
result.appendECI(c1 - 1);
|
result.appendECI(c1 - 1)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
//currently we only support character set ECIs
|
//currently we only support character set ECIs
|
||||||
/*} else {
|
/*} else {
|
||||||
if (bits.available() < 8) {
|
if (bits.available() < 8) {
|
||||||
@@ -580,11 +582,11 @@ final class DecodedBitStreamParser {
|
|||||||
/**
|
/**
|
||||||
* See ISO 16022:2006, Annex B, B.2
|
* See ISO 16022:2006, Annex B, B.2
|
||||||
*/
|
*/
|
||||||
private static int unrandomize255State(int randomizedBase256Codeword,
|
fn unrandomize255State( randomizedBase256Codeword:u32,
|
||||||
int base256CodewordPosition) {
|
base256CodewordPosition:usize) -> u32{
|
||||||
int pseudoRandomNumber = ((149 * base256CodewordPosition) % 255) + 1;
|
let pseudoRandomNumber = ((149 * base256CodewordPosition as u32) % 255) + 1;
|
||||||
int tempVariable = randomizedBase256Codeword - pseudoRandomNumber;
|
let tempVariable = randomizedBase256Codeword - pseudoRandomNumber;
|
||||||
return tempVariable >= 0 ? tempVariable : tempVariable + 256;
|
|
||||||
|
if tempVariable >= 0 {tempVariable} else {tempVariable + 256}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
@@ -14,15 +14,10 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.google.zxing.datamatrix.decoder;
|
use crate::{common::{reedsolomon::{ReedSolomonDecoder, get_predefined_genericgf, PredefinedGenericGF}, DecoderRXingResult, BitMatrix}, Exceptions};
|
||||||
|
|
||||||
|
use super::{DataBlock, BitMatrixParser, decoded_bit_stream_parser};
|
||||||
|
|
||||||
import com.google.zxing.ChecksumException;
|
|
||||||
import com.google.zxing.FormatException;
|
|
||||||
import com.google.zxing.common.BitMatrix;
|
|
||||||
import com.google.zxing.common.DecoderRXingResult;
|
|
||||||
import com.google.zxing.common.reedsolomon.GenericGF;
|
|
||||||
import com.google.zxing.common.reedsolomon.ReedSolomonDecoder;
|
|
||||||
import com.google.zxing.common.reedsolomon.ReedSolomonException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>The main class which implements Data Matrix Code decoding -- as opposed to locating and extracting
|
* <p>The main class which implements Data Matrix Code decoding -- as opposed to locating and extracting
|
||||||
@@ -30,12 +25,14 @@ import com.google.zxing.common.reedsolomon.ReedSolomonException;
|
|||||||
*
|
*
|
||||||
* @author bbrown@google.com (Brian Brown)
|
* @author bbrown@google.com (Brian Brown)
|
||||||
*/
|
*/
|
||||||
public final class Decoder {
|
pub struct Decoder(ReedSolomonDecoder);
|
||||||
|
|
||||||
private final ReedSolomonDecoder rsDecoder;
|
impl Decoder {
|
||||||
|
|
||||||
public Decoder() {
|
pub fn new() -> Self {
|
||||||
rsDecoder = new ReedSolomonDecoder(GenericGF.DATA_MATRIX_FIELD_256);
|
|
||||||
|
Self(ReedSolomonDecoder::new(get_predefined_genericgf(PredefinedGenericGF::DataMatrixField256)))
|
||||||
|
// rsDecoder = new ReedSolomonDecoder(GenericGF.DATA_MATRIX_FIELD_256);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -47,8 +44,8 @@ public final class Decoder {
|
|||||||
* @throws FormatException if the Data Matrix Code cannot be decoded
|
* @throws FormatException if the Data Matrix Code cannot be decoded
|
||||||
* @throws ChecksumException if error correction fails
|
* @throws ChecksumException if error correction fails
|
||||||
*/
|
*/
|
||||||
public DecoderRXingResult decode(boolean[][] image) throws FormatException, ChecksumException {
|
pub fn decode_bools(&self, image:&Vec<Vec<bool>>) -> Result<DecoderRXingResult,Exceptions> {
|
||||||
return decode(BitMatrix.parse(image));
|
self.decode(&BitMatrix::parse_bools(&image))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -60,39 +57,42 @@ public final class Decoder {
|
|||||||
* @throws FormatException if the Data Matrix Code cannot be decoded
|
* @throws FormatException if the Data Matrix Code cannot be decoded
|
||||||
* @throws ChecksumException if error correction fails
|
* @throws ChecksumException if error correction fails
|
||||||
*/
|
*/
|
||||||
public DecoderRXingResult decode(BitMatrix bits) throws FormatException, ChecksumException {
|
pub fn decode(&self, bits:&BitMatrix) -> Result<DecoderRXingResult,Exceptions> {
|
||||||
|
|
||||||
// Construct a parser and read version, error-correction level
|
// Construct a parser and read version, error-correction level
|
||||||
BitMatrixParser parser = new BitMatrixParser(bits);
|
let parser = BitMatrixParser::new(bits)?;
|
||||||
Version version = parser.getVersion();
|
let version = parser.getVersion();
|
||||||
|
|
||||||
|
|
||||||
// Read codewords
|
// Read codewords
|
||||||
byte[] codewords = parser.readCodewords();
|
let codewords = parser.readCodewords()?;
|
||||||
// Separate into data blocks
|
// Separate into data blocks
|
||||||
DataBlock[] dataBlocks = DataBlock.getDataBlocks(codewords, version);
|
let dataBlocks = DataBlock::getDataBlocks(&codewords, version)?;
|
||||||
|
|
||||||
// Count total number of data bytes
|
// Count total number of data bytes
|
||||||
int totalBytes = 0;
|
let totalBytes = 0;
|
||||||
for (DataBlock db : dataBlocks) {
|
for db in dataBlocks {
|
||||||
totalBytes += db.getNumDataCodewords();
|
totalBytes += db.getNumDataCodewords();
|
||||||
}
|
}
|
||||||
byte[] resultBytes = new byte[totalBytes];
|
let resultBytes = vec![0u8;totalBytes as usize];
|
||||||
|
|
||||||
int dataBlocksCount = dataBlocks.length;
|
let dataBlocksCount = dataBlocks.len();
|
||||||
// Error-correct and copy data blocks together into a stream of bytes
|
// Error-correct and copy data blocks together into a stream of bytes
|
||||||
for (int j = 0; j < dataBlocksCount; j++) {
|
for j in 0..dataBlocksCount {
|
||||||
DataBlock dataBlock = dataBlocks[j];
|
// for (int j = 0; j < dataBlocksCount; j++) {
|
||||||
byte[] codewordBytes = dataBlock.getCodewords();
|
let dataBlock = dataBlocks[j];
|
||||||
int numDataCodewords = dataBlock.getNumDataCodewords();
|
let codewordBytes = dataBlock.getCodewords();
|
||||||
correctErrors(codewordBytes, numDataCodewords);
|
let numDataCodewords = dataBlock.getNumDataCodewords() as usize;
|
||||||
for (int i = 0; i < numDataCodewords; i++) {
|
self.correctErrors(codewordBytes, numDataCodewords as u32);
|
||||||
|
for i in 0..numDataCodewords {
|
||||||
|
// for (int i = 0; i < numDataCodewords; i++) {
|
||||||
// De-interlace data blocks.
|
// De-interlace data blocks.
|
||||||
resultBytes[i * dataBlocksCount + j] = codewordBytes[i];
|
resultBytes[i * dataBlocksCount + j] = codewordBytes[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode the contents of that stream of bytes
|
// Decode the contents of that stream of bytes
|
||||||
return DecodedBitStreamParser.decode(resultBytes);
|
decoded_bit_stream_parser::decode(&resultBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -103,23 +103,29 @@ public final class Decoder {
|
|||||||
* @param numDataCodewords number of codewords that are data bytes
|
* @param numDataCodewords number of codewords that are data bytes
|
||||||
* @throws ChecksumException if error correction fails
|
* @throws ChecksumException if error correction fails
|
||||||
*/
|
*/
|
||||||
private void correctErrors(byte[] codewordBytes, int numDataCodewords) throws ChecksumException {
|
fn correctErrors(&self, codewordBytes:&[u8], numDataCodewords:u32) -> Result<(),Exceptions> {
|
||||||
int numCodewords = codewordBytes.length;
|
let numCodewords = codewordBytes.len();
|
||||||
// First read into an array of ints
|
// First read into an array of ints
|
||||||
int[] codewordsInts = new int[numCodewords];
|
// let codewordsInts = vec![0i32;numCodewords];
|
||||||
for (int i = 0; i < numCodewords; i++) {
|
// for i in 0..numCodewords {
|
||||||
codewordsInts[i] = codewordBytes[i] & 0xFF;
|
// // for (int i = 0; i < numCodewords; i++) {
|
||||||
}
|
// codewordsInts[i] = codewordBytes[i];
|
||||||
try {
|
// }
|
||||||
rsDecoder.decode(codewordsInts, codewordBytes.length - numDataCodewords);
|
let codewordsInts : Vec<i32> = codewordBytes.iter().map(|x| *x as i32).collect();
|
||||||
} catch (ReedSolomonException ignored) {
|
|
||||||
throw ChecksumException.getChecksumInstance();
|
//try {
|
||||||
}
|
self.0.decode(&mut codewordsInts, codewordBytes.len() as i32- numDataCodewords as i32)?;
|
||||||
|
//} catch (ReedSolomonException ignored) {
|
||||||
|
//throw ChecksumException.getChecksumInstance();
|
||||||
|
//}
|
||||||
// Copy back into array of bytes -- only need to worry about the bytes that were data
|
// Copy back into array of bytes -- only need to worry about the bytes that were data
|
||||||
// We don't care about errors in the error-correction codewords
|
// We don't care about errors in the error-correction codewords
|
||||||
for (int i = 0; i < numDataCodewords; i++) {
|
for i in 0..numDataCodewords as usize {
|
||||||
codewordBytes[i] = (byte) codewordsInts[i];
|
// for (int i = 0; i < numDataCodewords; i++) {
|
||||||
|
codewordBytes[i] = codewordsInts[i] as u8;
|
||||||
}
|
}
|
||||||
|
// codewordsInts.into_iter().take(numDataCodewords as usize).map(|x| x as u8).collect::<Vec<u8>>()
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,12 @@
|
|||||||
|
|
||||||
mod version;
|
mod version;
|
||||||
mod data_block;
|
mod data_block;
|
||||||
|
mod decoder;
|
||||||
|
mod bit_matrix_parser;
|
||||||
|
|
||||||
pub use version::*;
|
pub use version::*;
|
||||||
pub use data_block::*;
|
pub use data_block::*;
|
||||||
|
pub use decoder::*;
|
||||||
|
pub use bit_matrix_parser::*;
|
||||||
|
|
||||||
|
pub mod decoded_bit_stream_parser;
|
||||||
@@ -23,6 +23,8 @@ lazy_static! {
|
|||||||
static ref VERSIONS: Vec<Version> = Version::buildVersions();
|
static ref VERSIONS: Vec<Version> = Version::buildVersions();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub type VersionRef = &'static Version;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The Version object encapsulates attributes about a particular
|
* The Version object encapsulates attributes about a particular
|
||||||
* size Data Matrix Code.
|
* size Data Matrix Code.
|
||||||
|
|||||||
Reference in New Issue
Block a user