"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "core/src/main/java/com/google/zxing/datamatrix/decoder/DecodedBitStreamParser.java" between
zxing-zxing-3.4.1.tar.gz and zxing-zxing-3.5.0.tar.gz

About: ZXing ("zebra crossing") is a multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages. Info: Project is in maintenance mode (no active development).

DecodedBitStreamParser.java  (zxing-zxing-3.4.1):DecodedBitStreamParser.java  (zxing-zxing-3.5.0)
skipping to change at line 22 skipping to change at line 22
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.google.zxing.datamatrix.decoder; package com.google.zxing.datamatrix.decoder;
import com.google.zxing.FormatException; import com.google.zxing.FormatException;
import com.google.zxing.common.BitSource; import com.google.zxing.common.BitSource;
import com.google.zxing.common.DecoderResult; import com.google.zxing.common.DecoderResult;
import com.google.zxing.common.ECIStringBuilder;
import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.List; 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
* in one Data Matrix Code. This class decodes the bits back into text.</p> * in one Data Matrix Code. This class decodes the bits back into text.</p>
* *
* <p>See ISO 16022:2006, 5.2.1 - 5.2.9.2</p> * <p>See ISO 16022:2006, 5.2.1 - 5.2.9.2</p>
* *
* @author bbrown@google.com (Brian Brown) * @author bbrown@google.com (Brian Brown)
* @author Sean Owen * @author Sean Owen
*/ */
final class DecodedBitStreamParser { final class DecodedBitStreamParser {
private enum Mode { private enum Mode {
PAD_ENCODE, // Not really a mode PAD_ENCODE, // Not really a mode
ASCII_ENCODE, ASCII_ENCODE,
C40_ENCODE, C40_ENCODE,
TEXT_ENCODE, TEXT_ENCODE,
ANSIX12_ENCODE, ANSIX12_ENCODE,
EDIFACT_ENCODE, EDIFACT_ENCODE,
BASE256_ENCODE BASE256_ENCODE,
ECI_ENCODE
} }
/** /**
* 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 = { private static final char[] C40_BASIC_SET_CHARS = {
'*', '*', '*', ' ', '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'
skipping to change at line 87 skipping to change at line 91
private static final char[] TEXT_SHIFT3_SET_CHARS = { private static final char[] TEXT_SHIFT3_SET_CHARS = {
'`', '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', '{', '|', '}', '~', (char) 127
}; };
private DecodedBitStreamParser() { private DecodedBitStreamParser() {
} }
static DecoderResult decode(byte[] bytes) throws FormatException { static DecoderResult decode(byte[] bytes) throws FormatException {
BitSource bits = new BitSource(bytes); BitSource bits = new BitSource(bytes);
StringBuilder result = new StringBuilder(100); ECIStringBuilder result = new ECIStringBuilder(100);
StringBuilder resultTrailer = new StringBuilder(0); StringBuilder resultTrailer = new StringBuilder(0);
List<byte[]> byteSegments = new ArrayList<>(1); List<byte[]> byteSegments = new ArrayList<>(1);
Mode mode = Mode.ASCII_ENCODE; Mode mode = Mode.ASCII_ENCODE;
// Could look directly at 'bytes', if we're sure of not having to account fo
r multi byte values
Set<Integer> fnc1Positions = new HashSet<>();
int symbologyModifier;
boolean isECIencoded = false;
do { do {
if (mode == Mode.ASCII_ENCODE) { if (mode == Mode.ASCII_ENCODE) {
mode = decodeAsciiSegment(bits, result, resultTrailer); mode = decodeAsciiSegment(bits, result, resultTrailer, fnc1Positions);
} else { } else {
switch (mode) { switch (mode) {
case C40_ENCODE: case C40_ENCODE:
decodeC40Segment(bits, result); decodeC40Segment(bits, result, fnc1Positions);
break; break;
case TEXT_ENCODE: case TEXT_ENCODE:
decodeTextSegment(bits, result); decodeTextSegment(bits, result, fnc1Positions);
break; break;
case ANSIX12_ENCODE: case ANSIX12_ENCODE:
decodeAnsiX12Segment(bits, result); decodeAnsiX12Segment(bits, result);
break; break;
case EDIFACT_ENCODE: case EDIFACT_ENCODE:
decodeEdifactSegment(bits, result); decodeEdifactSegment(bits, result);
break; break;
case BASE256_ENCODE: case BASE256_ENCODE:
decodeBase256Segment(bits, result, byteSegments); decodeBase256Segment(bits, result, byteSegments);
break; break;
case ECI_ENCODE:
decodeECISegment(bits, result);
isECIencoded = true; // ECI detection only, atm continue decoding as
ASCII
break;
default: default:
throw FormatException.getFormatInstance(); throw FormatException.getFormatInstance();
} }
mode = Mode.ASCII_ENCODE; mode = Mode.ASCII_ENCODE;
} }
} while (mode != Mode.PAD_ENCODE && bits.available() > 0); } while (mode != Mode.PAD_ENCODE && bits.available() > 0);
if (resultTrailer.length() > 0) { if (resultTrailer.length() > 0) {
result.append(resultTrailer); result.appendCharacters(resultTrailer);
}
if (isECIencoded) {
// Examples for this numbers can be found in this documentation of a hardw
are barcode scanner:
// https://honeywellaidc.force.com/supportppr/s/article/List-of-barcode-sy
mbology-AIM-Identifiers
if (fnc1Positions.contains(0) || fnc1Positions.contains(4)) {
symbologyModifier = 5;
} else if (fnc1Positions.contains(1) || fnc1Positions.contains(5)) {
symbologyModifier = 6;
} else {
symbologyModifier = 4;
}
} else {
if (fnc1Positions.contains(0) || fnc1Positions.contains(4)) {
symbologyModifier = 2;
} else if (fnc1Positions.contains(1) || fnc1Positions.contains(5)) {
symbologyModifier = 3;
} else {
symbologyModifier = 1;
}
} }
return new DecoderResult(bytes, result.toString(), byteSegments.isEmpty() ?
null : byteSegments, null); return new DecoderResult(bytes,
result.toString(),
byteSegments.isEmpty() ? null : byteSegments,
null,
symbologyModifier);
} }
/** /**
* 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, private static Mode decodeAsciiSegment(BitSource bits,
StringBuilder result, ECIStringBuilder result,
StringBuilder resultTrailer) throws For StringBuilder resultTrailer,
matException { Set<Integer> fnc1positions) throws Form
atException {
boolean upperShift = false; boolean upperShift = false;
do { do {
int oneByte = bits.readBits(8); int oneByte = bits.readBits(8);
if (oneByte == 0) { if (oneByte == 0) {
throw FormatException.getFormatInstance(); throw FormatException.getFormatInstance();
} else if (oneByte <= 128) { // ASCII data (ASCII value + 1) } else if (oneByte <= 128) { // ASCII data (ASCII value + 1)
if (upperShift) { if (upperShift) {
oneByte += 128; oneByte += 128;
//upperShift = false; //upperShift = false;
} }
skipping to change at line 156 skipping to change at line 193
result.append('0'); result.append('0');
} }
result.append(value); result.append(value);
} else { } else {
switch (oneByte) { switch (oneByte) {
case 230: // Latch to C40 encodation case 230: // Latch to C40 encodation
return Mode.C40_ENCODE; return Mode.C40_ENCODE;
case 231: // Latch to Base 256 encodation case 231: // Latch to Base 256 encodation
return Mode.BASE256_ENCODE; return Mode.BASE256_ENCODE;
case 232: // FNC1 case 232: // FNC1
fnc1positions.add(result.length());
result.append((char) 29); // translate as ASCII 29 result.append((char) 29); // translate as ASCII 29
break; break;
case 233: // Structured Append case 233: // Structured Append
case 234: // Reader Programming case 234: // Reader Programming
// Ignore these symbols for now // Ignore these symbols for now
//throw ReaderException.getInstance(); //throw ReaderException.getInstance();
break; break;
case 235: // Upper Shift (shift to Extended ASCII) case 235: // Upper Shift (shift to Extended ASCII)
upperShift = true; upperShift = true;
break; break;
skipping to change at line 181 skipping to change at line 219
result.append("[)>\u001E06\u001D"); result.append("[)>\u001E06\u001D");
resultTrailer.insert(0, "\u001E\u0004"); resultTrailer.insert(0, "\u001E\u0004");
break; break;
case 238: // Latch to ANSI X12 encodation case 238: // Latch to ANSI X12 encodation
return Mode.ANSIX12_ENCODE; return Mode.ANSIX12_ENCODE;
case 239: // Latch to Text encodation case 239: // Latch to Text encodation
return Mode.TEXT_ENCODE; return Mode.TEXT_ENCODE;
case 240: // Latch to EDIFACT encodation case 240: // Latch to EDIFACT encodation
return Mode.EDIFACT_ENCODE; return Mode.EDIFACT_ENCODE;
case 241: // ECI Character case 241: // ECI Character
// TODO(bbrown): I think we need to support ECI return Mode.ECI_ENCODE;
//throw ReaderException.getInstance();
// Ignore this symbol for now
break;
default: default:
// Not to be used in ASCII encodation // Not to be used in ASCII encodation
// but work around encoders that end with 254, latch back to ASCII // but work around encoders that end with 254, latch back to ASCII
if (oneByte != 254 || bits.available() != 0) { if (oneByte != 254 || bits.available() != 0) {
throw FormatException.getFormatInstance(); throw FormatException.getFormatInstance();
} }
break; break;
} }
} }
} while (bits.available() > 0); } while (bits.available() > 0);
return Mode.ASCII_ENCODE; return Mode.ASCII_ENCODE;
} }
/** /**
* 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, StringBuilder result) thr private static void decodeC40Segment(BitSource bits, ECIStringBuilder result,
ows FormatException { Set<Integer> fnc1positions)
throws FormatException {
// 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 scenar io all the time // TODO(bbrown): The Upper Shift with C40 doesn't work in the 4 value scenar io all the time
boolean upperShift = false; boolean upperShift = false;
int[] cValues = new int[3]; int[] cValues = new int[3];
int shift = 0; int shift = 0;
do { do {
// 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
skipping to change at line 261 skipping to change at line 297
char c40char = C40_SHIFT2_SET_CHARS[cValue]; char c40char = C40_SHIFT2_SET_CHARS[cValue];
if (upperShift) { if (upperShift) {
result.append((char) (c40char + 128)); result.append((char) (c40char + 128));
upperShift = false; upperShift = false;
} else { } else {
result.append(c40char); result.append(c40char);
} }
} else { } else {
switch (cValue) { switch (cValue) {
case 27: // FNC1 case 27: // FNC1
fnc1positions.add(result.length());
result.append((char) 29); // translate as ASCII 29 result.append((char) 29); // translate as ASCII 29
break; break;
case 30: // Upper Shift case 30: // Upper Shift
upperShift = true; upperShift = true;
break; break;
default: default:
throw FormatException.getFormatInstance(); throw FormatException.getFormatInstance();
} }
} }
shift = 0; shift = 0;
skipping to change at line 291 skipping to change at line 328
default: default:
throw FormatException.getFormatInstance(); throw FormatException.getFormatInstance();
} }
} }
} while (bits.available() > 0); } while (bits.available() > 0);
} }
/** /**
* 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, StringBuilder result) th private static void decodeTextSegment(BitSource bits, ECIStringBuilder result,
rows FormatException { Set<Integer> fnc1positions)
throws FormatException {
// 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 scena rio all the time // TODO(bbrown): The Upper Shift with Text doesn't work in the 4 value scena rio all the time
boolean upperShift = false; boolean upperShift = false;
int[] cValues = new int[3]; int[] cValues = new int[3];
int shift = 0; int shift = 0;
do { do {
// 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) {
skipping to change at line 351 skipping to change at line 389
char textChar = TEXT_SHIFT2_SET_CHARS[cValue]; char 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(textChar);
} }
} else { } else {
switch (cValue) { switch (cValue) {
case 27: // FNC1 case 27: // FNC1
fnc1positions.add(result.length());
result.append((char) 29); // translate as ASCII 29 result.append((char) 29); // translate as ASCII 29
break; break;
case 30: // Upper Shift case 30: // Upper Shift
upperShift = true; upperShift = true;
break; break;
default: default:
throw FormatException.getFormatInstance(); throw FormatException.getFormatInstance();
} }
} }
shift = 0; shift = 0;
skipping to change at line 387 skipping to change at line 426
throw FormatException.getFormatInstance(); throw FormatException.getFormatInstance();
} }
} }
} while (bits.available() > 0); } while (bits.available() > 0);
} }
/** /**
* See ISO 16022:2006, 5.2.7 * See ISO 16022:2006, 5.2.7
*/ */
private static void decodeAnsiX12Segment(BitSource bits, private static void decodeAnsiX12Segment(BitSource bits,
StringBuilder result) throws FormatEx ception { ECIStringBuilder result) throws Forma tException {
// 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]; int[] cValues = new int[3];
do { do {
// 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;
} }
int firstByte = bits.readBits(8); int firstByte = bits.readBits(8);
skipping to change at line 446 skipping to change at line 485
result[0] = temp; result[0] = temp;
fullBitValue -= temp * 1600; fullBitValue -= temp * 1600;
temp = fullBitValue / 40; temp = fullBitValue / 40;
result[1] = temp; result[1] = temp;
result[2] = fullBitValue - temp * 40; result[2] = fullBitValue - temp * 40;
} }
/** /**
* 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, StringBuilder result) { private static void decodeEdifactSegment(BitSource bits, ECIStringBuilder resu lt) {
do { do {
// If there is only two or less bytes left then it will be encoded as ASCI I // If there is only two or less bytes left then it will be encoded as ASCI I
if (bits.available() <= 16) { if (bits.available() <= 16) {
return; return;
} }
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
int edifactValue = bits.readBits(6); int edifactValue = bits.readBits(6);
// Check for the unlatch character // Check for the unlatch character
skipping to change at line 478 skipping to change at line 517
} }
result.append((char) edifactValue); result.append((char) edifactValue);
} }
} while (bits.available() > 0); } while (bits.available() > 0);
} }
/** /**
* 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, private static void decodeBase256Segment(BitSource bits,
StringBuilder result, ECIStringBuilder result,
Collection<byte[]> byteSegments) Collection<byte[]> byteSegments)
throws FormatException { throws FormatException {
// 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 int codewordPosition = 1 + bits.getByteOffset(); // position is 1-indexed
int d1 = unrandomize255State(bits.readBits(8), codewordPosition++); int d1 = unrandomize255State(bits.readBits(8), codewordPosition++);
int count; int count;
if (d1 == 0) { // Read the remainder of the symbol if (d1 == 0) { // Read the remainder of the symbol
count = bits.available() / 8; count = bits.available() / 8;
} else if (d1 < 250) { } else if (d1 < 250) {
count = d1; count = d1;
skipping to change at line 508 skipping to change at line 547
byte[] bytes = new byte[count]; byte[] bytes = new byte[count];
for (int i = 0; i < count; i++) { 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(); throw FormatException.getFormatInstance();
} }
bytes[i] = (byte) unrandomize255State(bits.readBits(8), codewordPosition++ ); bytes[i] = (byte) unrandomize255State(bits.readBits(8), codewordPosition++ );
} }
byteSegments.add(bytes); byteSegments.add(bytes);
try { result.append(new String(bytes, StandardCharsets.ISO_8859_1));
result.append(new String(bytes, "ISO8859_1")); }
} catch (UnsupportedEncodingException uee) {
throw new IllegalStateException("Platform does not support required encodi /**
ng: " + uee); * See ISO 16022:2007, 5.4.1
*/
private static void decodeECISegment(BitSource bits,
ECIStringBuilder result)
throws FormatException {
if (bits.available() < 8) {
throw FormatException.getFormatInstance();
}
int c1 = bits.readBits(8);
if (c1 <= 127) {
result.appendECI(c1 - 1);
} }
//currently we only support character set ECIs
/*} else {
if (bits.available() < 8) {
throw FormatException.getFormatInstance();
}
int c2 = bits.readBits(8);
if (c1 >= 128 && c1 <= 191) {
} else {
if (bits.available() < 8) {
throw FormatException.getFormatInstance();
}
int c3 = bits.readBits(8);
}
}*/
} }
/** /**
* See ISO 16022:2006, Annex B, B.2 * See ISO 16022:2006, Annex B, B.2
*/ */
private static int unrandomize255State(int randomizedBase256Codeword, private static int unrandomize255State(int randomizedBase256Codeword,
int base256CodewordPosition) { int base256CodewordPosition) {
int pseudoRandomNumber = ((149 * base256CodewordPosition) % 255) + 1; int pseudoRandomNumber = ((149 * base256CodewordPosition) % 255) + 1;
int tempVariable = randomizedBase256Codeword - pseudoRandomNumber; int tempVariable = randomizedBase256Codeword - pseudoRandomNumber;
return tempVariable >= 0 ? tempVariable : tempVariable + 256; return tempVariable >= 0 ? tempVariable : tempVariable + 256;
 End of changes. 25 change blocks. 
28 lines changed or deleted 94 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)