Catch more weird edge cases where readBits fails on bad QR codes and just return FormatException; remove a deprecated PDF417 method

git-svn-id: https://zxing.googlecode.com/svn/trunk@2542 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2012-12-05 20:37:20 +00:00
parent 02e0dcece1
commit cc9a55d1a2
2 changed files with 57 additions and 79 deletions

View file

@ -22,7 +22,6 @@ import com.google.zxing.Writer;
import com.google.zxing.WriterException; import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix; import com.google.zxing.common.BitMatrix;
import java.util.EnumMap;
import java.util.Map; import java.util.Map;
/** /**
@ -70,28 +69,6 @@ public final class PDF417Writer implements Writer {
return encode(contents, format, width, height, null); return encode(contents, format, width, height, null);
} }
/**
* @deprecated Use {@link #encode(String, BarcodeFormat, int, int, Map)} instead, with hints to
* specify the encoding options.
*/
@Deprecated
public BitMatrix encode(String contents,
BarcodeFormat format,
boolean compact,
int width,
int height,
int minCols,
int maxCols,
int minRows,
int maxRows,
Compaction compaction) throws WriterException {
Map<EncodeHintType, Object> hints = new EnumMap<EncodeHintType,Object>(EncodeHintType.class);
hints.put(EncodeHintType.PDF417_COMPACT, compact);
hints.put(EncodeHintType.PDF417_COMPACTION, compaction);
hints.put(EncodeHintType.PDF417_DIMENSIONS, new Dimensions(minCols, maxCols, minRows, maxRows));
return encode(contents, format, width, height, hints);
}
/** /**
* Takes encoder, accounts for width/height, and retrieves bit matrix * Takes encoder, accounts for width/height, and retrieves bit matrix
*/ */

View file

@ -59,9 +59,10 @@ final class DecodedBitStreamParser {
Map<DecodeHintType,?> hints) throws FormatException { Map<DecodeHintType,?> hints) throws FormatException {
BitSource bits = new BitSource(bytes); BitSource bits = new BitSource(bytes);
StringBuilder result = new StringBuilder(50); StringBuilder result = new StringBuilder(50);
List<byte[]> byteSegments = new ArrayList<byte[]>(1);
try {
CharacterSetECI currentCharacterSetECI = null; CharacterSetECI currentCharacterSetECI = null;
boolean fc1InEffect = false; boolean fc1InEffect = false;
List<byte[]> byteSegments = new ArrayList<byte[]>(1);
Mode mode; Mode mode;
do { do {
// While still another segment to read... // While still another segment to read...
@ -69,11 +70,7 @@ final class DecodedBitStreamParser {
// OK, assume we're done. Really, a TERMINATOR mode should have been recorded here // OK, assume we're done. Really, a TERMINATOR mode should have been recorded here
mode = Mode.TERMINATOR; mode = Mode.TERMINATOR;
} else { } else {
try {
mode = Mode.forBits(bits.readBits(4)); // mode is encoded by 4 bits mode = Mode.forBits(bits.readBits(4)); // mode is encoded by 4 bits
} catch (IllegalArgumentException iae) {
throw FormatException.getFormatInstance();
}
} }
if (mode != Mode.TERMINATOR) { if (mode != Mode.TERMINATOR) {
if (mode == Mode.FNC1_FIRST_POSITION || mode == Mode.FNC1_SECOND_POSITION) { if (mode == Mode.FNC1_FIRST_POSITION || mode == Mode.FNC1_SECOND_POSITION) {
@ -121,6 +118,10 @@ final class DecodedBitStreamParser {
} }
} }
} while (mode != Mode.TERMINATOR); } while (mode != Mode.TERMINATOR);
} catch (IllegalArgumentException iae) {
// from readBits() calls
throw FormatException.getFormatInstance();
}
return new DecoderResult(bytes, return new DecoderResult(bytes,
result.toString(), result.toString(),