Alter many encode hints to accept String equivalents

This commit is contained in:
Sean Owen 2015-10-05 14:13:17 +01:00
parent aa50b59613
commit a6707cb440
7 changed files with 43 additions and 42 deletions

View file

@ -29,6 +29,7 @@ public enum EncodeHintType {
* {@link com.google.zxing.qrcode.decoder.ErrorCorrectionLevel ErrorCorrectionLevel}. * {@link com.google.zxing.qrcode.decoder.ErrorCorrectionLevel ErrorCorrectionLevel}.
* For Aztec it is of type {@link Integer}, representing the minimal percentage of error correction words. * For Aztec it is of type {@link Integer}, representing the minimal percentage of error correction words.
* For PDF417 it is of type {@link Integer}, valid values being 0 to 8. * For PDF417 it is of type {@link Integer}, valid values being 0 to 8.
* In all cases, it can also be a {@link String} representation of the desired value as well.
* Note: an Aztec symbol should have a minimum of 25% EC words. * Note: an Aztec symbol should have a minimum of 25% EC words.
*/ */
ERROR_CORRECTION, ERROR_CORRECTION,
@ -63,18 +64,20 @@ public enum EncodeHintType {
/** /**
* Specifies margin, in pixels, to use when generating the barcode. The meaning can vary * Specifies margin, in pixels, to use when generating the barcode. The meaning can vary
* by format; for example it controls margin before and after the barcode horizontally for * by format; for example it controls margin before and after the barcode horizontally for
* most 1D formats. (Type {@link Integer}). * most 1D formats. (Type {@link Integer}, or {@link String} representation of the integer value).
*/ */
MARGIN, MARGIN,
/** /**
* Specifies whether to use compact mode for PDF417 (type {@link Boolean}). * Specifies whether to use compact mode for PDF417 (type {@link Boolean}, or "true" or "false"
* {@link String} value).
*/ */
PDF417_COMPACT, PDF417_COMPACT,
/** /**
* Specifies what compaction mode to use for PDF417 (type * Specifies what compaction mode to use for PDF417 (type
* {@link com.google.zxing.pdf417.encoder.Compaction Compaction}). * {@link com.google.zxing.pdf417.encoder.Compaction Compaction} or {@link String} value of one of its
* enum values).
*/ */
PDF417_COMPACTION, PDF417_COMPACTION,
@ -85,10 +88,11 @@ public enum EncodeHintType {
PDF417_DIMENSIONS, PDF417_DIMENSIONS,
/** /**
* Specifies the required number of layers for an Aztec code: * Specifies the required number of layers for an Aztec code.
* a negative number (-1, -2, -3, -4) specifies a compact Aztec code * A negative number (-1, -2, -3, -4) specifies a compact Aztec code.
* 0 indicates to use the minimum number of layers (the default) * 0 indicates to use the minimum number of layers (the default).
* a positive number (1, 2, .. 32) specifies a normaol (non-compact) Aztec code * A positive number (1, 2, .. 32) specifies a normal (non-compact) Aztec code.
* (Type {@link Integer}, or {@link String} representation of the integer value).
*/ */
AZTEC_LAYERS, AZTEC_LAYERS,
} }

View file

@ -37,16 +37,21 @@ public final class AztecWriter implements Writer {
@Override @Override
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints) { public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints) {
String charset = hints == null ? null : (String) hints.get(EncodeHintType.CHARACTER_SET); Charset charset = DEFAULT_CHARSET;
Number eccPercent = hints == null ? null : (Number) hints.get(EncodeHintType.ERROR_CORRECTION); int eccPercent = Encoder.DEFAULT_EC_PERCENT;
Number layers = hints == null ? null : (Number) hints.get(EncodeHintType.AZTEC_LAYERS); int layers = Encoder.DEFAULT_AZTEC_LAYERS;
return encode(contents, if (hints != null) {
format, if (hints.containsKey(EncodeHintType.CHARACTER_SET)) {
width, charset = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString());
height, }
charset == null ? DEFAULT_CHARSET : Charset.forName(charset), if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
eccPercent == null ? Encoder.DEFAULT_EC_PERCENT : eccPercent.intValue(), eccPercent = Integer.parseInt(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
layers == null ? Encoder.DEFAULT_AZTEC_LAYERS : layers.intValue()); }
if (hints.containsKey(EncodeHintType.AZTEC_LAYERS)) {
layers = Integer.parseInt(hints.get(EncodeHintType.AZTEC_LAYERS).toString());
}
}
return encode(contents, format, width, height, charset, eccPercent, layers);
} }
private static BitMatrix encode(String contents, BarcodeFormat format, private static BitMatrix encode(String contents, BarcodeFormat format,

View file

@ -49,11 +49,8 @@ public final class StringUtils {
* default encoding if none of these can possibly be correct * default encoding if none of these can possibly be correct
*/ */
public static String guessEncoding(byte[] bytes, Map<DecodeHintType,?> hints) { public static String guessEncoding(byte[] bytes, Map<DecodeHintType,?> hints) {
if (hints != null) { if (hints != null && hints.containsKey(DecodeHintType.CHARACTER_SET)) {
String characterSet = (String) hints.get(DecodeHintType.CHARACTER_SET); return hints.get(DecodeHintType.CHARACTER_SET).toString();
if (characterSet != null) {
return characterSet;
}
} }
// For now, merely tries to distinguish ISO-8859-1, UTF-8 and Shift_JIS, // For now, merely tries to distinguish ISO-8859-1, UTF-8 and Shift_JIS,
// which should be by far the most common encodings. // which should be by far the most common encodings.

View file

@ -60,11 +60,8 @@ public abstract class OneDimensionalCodeWriter implements Writer {
} }
int sidesMargin = getDefaultMargin(); int sidesMargin = getDefaultMargin();
if (hints != null) { if (hints != null && hints.containsKey(EncodeHintType.MARGIN)) {
Integer sidesMarginInt = (Integer) hints.get(EncodeHintType.MARGIN); sidesMargin = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
if (sidesMarginInt != null) {
sidesMargin = sidesMarginInt;
}
} }
boolean[] code = encode(contents); boolean[] code = encode(contents);

View file

@ -60,10 +60,10 @@ public final class PDF417Writer implements Writer {
if (hints != null) { if (hints != null) {
if (hints.containsKey(EncodeHintType.PDF417_COMPACT)) { if (hints.containsKey(EncodeHintType.PDF417_COMPACT)) {
encoder.setCompact((Boolean) hints.get(EncodeHintType.PDF417_COMPACT)); encoder.setCompact(Boolean.valueOf(hints.get(EncodeHintType.PDF417_COMPACT).toString()));
} }
if (hints.containsKey(EncodeHintType.PDF417_COMPACTION)) { if (hints.containsKey(EncodeHintType.PDF417_COMPACTION)) {
encoder.setCompaction((Compaction) hints.get(EncodeHintType.PDF417_COMPACTION)); encoder.setCompaction(Compaction.valueOf(hints.get(EncodeHintType.PDF417_COMPACTION).toString()));
} }
if (hints.containsKey(EncodeHintType.PDF417_DIMENSIONS)) { if (hints.containsKey(EncodeHintType.PDF417_DIMENSIONS)) {
Dimensions dimensions = (Dimensions) hints.get(EncodeHintType.PDF417_DIMENSIONS); Dimensions dimensions = (Dimensions) hints.get(EncodeHintType.PDF417_DIMENSIONS);
@ -73,14 +73,14 @@ public final class PDF417Writer implements Writer {
dimensions.getMinRows()); dimensions.getMinRows());
} }
if (hints.containsKey(EncodeHintType.MARGIN)) { if (hints.containsKey(EncodeHintType.MARGIN)) {
margin = ((Number) hints.get(EncodeHintType.MARGIN)).intValue(); margin = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
} }
if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) { if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
errorCorrectionLevel = ((Number) hints.get(EncodeHintType.ERROR_CORRECTION)).intValue(); errorCorrectionLevel = Integer.parseInt(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
} }
if (hints.containsKey(EncodeHintType.CHARACTER_SET)) { if (hints.containsKey(EncodeHintType.CHARACTER_SET)) {
String encoding = (String) hints.get(EncodeHintType.CHARACTER_SET); Charset encoding = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString());
encoder.setEncoding(Charset.forName(encoding)); encoder.setEncoding(encoding);
} }
} }

View file

@ -67,13 +67,11 @@ public final class QRCodeWriter implements Writer {
ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L; ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
int quietZone = QUIET_ZONE_SIZE; int quietZone = QUIET_ZONE_SIZE;
if (hints != null) { if (hints != null) {
ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints.get(EncodeHintType.ERROR_CORRECTION); if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) {
if (requestedECLevel != null) { errorCorrectionLevel = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString());
errorCorrectionLevel = requestedECLevel;
} }
Integer quietZoneInt = (Integer) hints.get(EncodeHintType.MARGIN); if (hints.containsKey(EncodeHintType.MARGIN)) {
if (quietZoneInt != null) { quietZone = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString());
quietZone = quietZoneInt;
} }
} }

View file

@ -77,9 +77,9 @@ public final class Encoder {
Map<EncodeHintType,?> hints) throws WriterException { Map<EncodeHintType,?> hints) throws WriterException {
// Determine what character encoding has been specified by the caller, if any // Determine what character encoding has been specified by the caller, if any
String encoding = hints == null ? null : (String) hints.get(EncodeHintType.CHARACTER_SET); String encoding = DEFAULT_BYTE_MODE_ENCODING;
if (encoding == null) { if (hints != null && hints.containsKey(EncodeHintType.CHARACTER_SET)) {
encoding = DEFAULT_BYTE_MODE_ENCODING; encoding = hints.get(EncodeHintType.CHARACTER_SET).toString();
} }
// Pick an encoding mode appropriate for the content. Note that this will not attempt to use // Pick an encoding mode appropriate for the content. Note that this will not attempt to use