diff --git a/core/src/main/java/com/google/zxing/EncodeHintType.java b/core/src/main/java/com/google/zxing/EncodeHintType.java index ff97b21aa..eb7938e42 100644 --- a/core/src/main/java/com/google/zxing/EncodeHintType.java +++ b/core/src/main/java/com/google/zxing/EncodeHintType.java @@ -29,6 +29,7 @@ public enum EncodeHintType { * {@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 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. */ ERROR_CORRECTION, @@ -63,18 +64,20 @@ public enum EncodeHintType { /** * 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 - * most 1D formats. (Type {@link Integer}). + * most 1D formats. (Type {@link Integer}, or {@link String} representation of the integer value). */ 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, /** * 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, @@ -85,10 +88,11 @@ public enum EncodeHintType { PDF417_DIMENSIONS, /** - * Specifies the required number of layers for an 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) - * a positive number (1, 2, .. 32) specifies a normaol (non-compact) Aztec code + * Specifies the required number of layers for an 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). + * 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, } diff --git a/core/src/main/java/com/google/zxing/aztec/AztecWriter.java b/core/src/main/java/com/google/zxing/aztec/AztecWriter.java index afa73ef83..a078a9bb0 100644 --- a/core/src/main/java/com/google/zxing/aztec/AztecWriter.java +++ b/core/src/main/java/com/google/zxing/aztec/AztecWriter.java @@ -37,16 +37,21 @@ public final class AztecWriter implements Writer { @Override public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map hints) { - String charset = hints == null ? null : (String) hints.get(EncodeHintType.CHARACTER_SET); - Number eccPercent = hints == null ? null : (Number) hints.get(EncodeHintType.ERROR_CORRECTION); - Number layers = hints == null ? null : (Number) hints.get(EncodeHintType.AZTEC_LAYERS); - return encode(contents, - format, - width, - height, - charset == null ? DEFAULT_CHARSET : Charset.forName(charset), - eccPercent == null ? Encoder.DEFAULT_EC_PERCENT : eccPercent.intValue(), - layers == null ? Encoder.DEFAULT_AZTEC_LAYERS : layers.intValue()); + Charset charset = DEFAULT_CHARSET; + int eccPercent = Encoder.DEFAULT_EC_PERCENT; + int layers = Encoder.DEFAULT_AZTEC_LAYERS; + if (hints != null) { + if (hints.containsKey(EncodeHintType.CHARACTER_SET)) { + charset = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString()); + } + if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) { + eccPercent = Integer.parseInt(hints.get(EncodeHintType.ERROR_CORRECTION).toString()); + } + 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, diff --git a/core/src/main/java/com/google/zxing/common/StringUtils.java b/core/src/main/java/com/google/zxing/common/StringUtils.java index 9ea1d822e..318df2733 100644 --- a/core/src/main/java/com/google/zxing/common/StringUtils.java +++ b/core/src/main/java/com/google/zxing/common/StringUtils.java @@ -49,11 +49,8 @@ public final class StringUtils { * default encoding if none of these can possibly be correct */ public static String guessEncoding(byte[] bytes, Map hints) { - if (hints != null) { - String characterSet = (String) hints.get(DecodeHintType.CHARACTER_SET); - if (characterSet != null) { - return characterSet; - } + if (hints != null && hints.containsKey(DecodeHintType.CHARACTER_SET)) { + return hints.get(DecodeHintType.CHARACTER_SET).toString(); } // For now, merely tries to distinguish ISO-8859-1, UTF-8 and Shift_JIS, // which should be by far the most common encodings. diff --git a/core/src/main/java/com/google/zxing/oned/OneDimensionalCodeWriter.java b/core/src/main/java/com/google/zxing/oned/OneDimensionalCodeWriter.java index c2dad999e..190c654db 100644 --- a/core/src/main/java/com/google/zxing/oned/OneDimensionalCodeWriter.java +++ b/core/src/main/java/com/google/zxing/oned/OneDimensionalCodeWriter.java @@ -60,11 +60,8 @@ public abstract class OneDimensionalCodeWriter implements Writer { } int sidesMargin = getDefaultMargin(); - if (hints != null) { - Integer sidesMarginInt = (Integer) hints.get(EncodeHintType.MARGIN); - if (sidesMarginInt != null) { - sidesMargin = sidesMarginInt; - } + if (hints != null && hints.containsKey(EncodeHintType.MARGIN)) { + sidesMargin = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString()); } boolean[] code = encode(contents); diff --git a/core/src/main/java/com/google/zxing/pdf417/PDF417Writer.java b/core/src/main/java/com/google/zxing/pdf417/PDF417Writer.java index cb37884a6..000225063 100644 --- a/core/src/main/java/com/google/zxing/pdf417/PDF417Writer.java +++ b/core/src/main/java/com/google/zxing/pdf417/PDF417Writer.java @@ -60,10 +60,10 @@ public final class PDF417Writer implements Writer { if (hints != null) { 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)) { - encoder.setCompaction((Compaction) hints.get(EncodeHintType.PDF417_COMPACTION)); + encoder.setCompaction(Compaction.valueOf(hints.get(EncodeHintType.PDF417_COMPACTION).toString())); } if (hints.containsKey(EncodeHintType.PDF417_DIMENSIONS)) { Dimensions dimensions = (Dimensions) hints.get(EncodeHintType.PDF417_DIMENSIONS); @@ -73,14 +73,14 @@ public final class PDF417Writer implements Writer { dimensions.getMinRows()); } 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)) { - errorCorrectionLevel = ((Number) hints.get(EncodeHintType.ERROR_CORRECTION)).intValue(); + errorCorrectionLevel = Integer.parseInt(hints.get(EncodeHintType.ERROR_CORRECTION).toString()); } if (hints.containsKey(EncodeHintType.CHARACTER_SET)) { - String encoding = (String) hints.get(EncodeHintType.CHARACTER_SET); - encoder.setEncoding(Charset.forName(encoding)); + Charset encoding = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString()); + encoder.setEncoding(encoding); } } diff --git a/core/src/main/java/com/google/zxing/qrcode/QRCodeWriter.java b/core/src/main/java/com/google/zxing/qrcode/QRCodeWriter.java index f5dcf089e..ce7ee0736 100644 --- a/core/src/main/java/com/google/zxing/qrcode/QRCodeWriter.java +++ b/core/src/main/java/com/google/zxing/qrcode/QRCodeWriter.java @@ -67,13 +67,11 @@ public final class QRCodeWriter implements Writer { ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L; int quietZone = QUIET_ZONE_SIZE; if (hints != null) { - ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints.get(EncodeHintType.ERROR_CORRECTION); - if (requestedECLevel != null) { - errorCorrectionLevel = requestedECLevel; + if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) { + errorCorrectionLevel = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString()); } - Integer quietZoneInt = (Integer) hints.get(EncodeHintType.MARGIN); - if (quietZoneInt != null) { - quietZone = quietZoneInt; + if (hints.containsKey(EncodeHintType.MARGIN)) { + quietZone = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString()); } } diff --git a/core/src/main/java/com/google/zxing/qrcode/encoder/Encoder.java b/core/src/main/java/com/google/zxing/qrcode/encoder/Encoder.java index ae2fc5456..8b6d0cf59 100644 --- a/core/src/main/java/com/google/zxing/qrcode/encoder/Encoder.java +++ b/core/src/main/java/com/google/zxing/qrcode/encoder/Encoder.java @@ -77,9 +77,9 @@ public final class Encoder { Map hints) throws WriterException { // Determine what character encoding has been specified by the caller, if any - String encoding = hints == null ? null : (String) hints.get(EncodeHintType.CHARACTER_SET); - if (encoding == null) { - encoding = DEFAULT_BYTE_MODE_ENCODING; + String encoding = DEFAULT_BYTE_MODE_ENCODING; + if (hints != null && hints.containsKey(EncodeHintType.CHARACTER_SET)) { + encoding = hints.get(EncodeHintType.CHARACTER_SET).toString(); } // Pick an encoding mode appropriate for the content. Note that this will not attempt to use