From 16382431d612b45f5cda741384e1f8fb4d829f3b Mon Sep 17 00:00:00 2001 From: dswitkin Date: Thu, 13 Nov 2008 17:12:53 +0000 Subject: [PATCH] Converted the Mode and ECLevel enums in QRCode.java. git-svn-id: http://zxing.googlecode.com/svn/trunk@695 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../google/zxing/qrcode/encoder/Encoder.java | 80 ++++------ .../zxing/qrcode/encoder/MatrixUtil.java | 10 +- .../google/zxing/qrcode/encoder/QRCode.java | 149 +++++++++--------- .../google/zxing/qrcode/encoder/Renderer.java | 6 +- 4 files changed, 115 insertions(+), 130 deletions(-) diff --git a/core/src/com/google/zxing/qrcode/encoder/Encoder.java b/core/src/com/google/zxing/qrcode/encoder/Encoder.java index 0db9c152..e96f00aa 100644 --- a/core/src/com/google/zxing/qrcode/encoder/Encoder.java +++ b/core/src/com/google/zxing/qrcode/encoder/Encoder.java @@ -285,10 +285,9 @@ public final class Encoder { // Note that there is no way to encode bytes in MODE_KANJI. We might // want to add EncodeWithMode() with which clients can specify the // encoding mode. For now, we don't need the functionality. - static boolean Encode(final StringPiece& bytes, QRCode.ECLevel ec_level, - QRCode *qr_code) { + static boolean Encode(final StringPiece& bytes, int ec_level, QRCode *qr_code) { // Step 1: Choose the mode (encoding). - final QRCode.Mode mode = ChooseMode(bytes); + final int mode = ChooseMode(bytes); // Step 2: Append "bytes" into "data_bits" in appropriate encoding. BitVector data_bits; @@ -372,36 +371,34 @@ public final class Encoder { // distinguish Shift_JIS from other encodings such as ISO-8859-1, from // data bytes alone. For example "\xE0\xE0" can be interpreted as one // character in Shift_JIS, but also two characters in ISO-8859-1. - static QRCode.Mode ChooseMode(final StringPiece &bytes) { - boolean has_numeric = false; - boolean has_alphanumeric = false; - boolean has_other = false; - for (int i = 0; i < bytes.size(); ++i) { - final int byte = bytes[i]; - if (byte >= '0' && byte <= '9') { - has_numeric = true; - } else if (GetAlphanumericCode(byte) != -1) { - has_alphanumeric = true; - } else { - has_other = true; - } - } - if (has_other) { + static int ChooseMode(final StringPiece &bytes) { + boolean has_numeric = false; + boolean has_alphanumeric = false; + boolean has_other = false; + for (int i = 0; i < bytes.size(); ++i) { + final int byte = bytes[i]; + if (byte >= '0' && byte <= '9') { + has_numeric = true; + } else if (GetAlphanumericCode(byte) != -1) { + has_alphanumeric = true; + } else { + has_other = true; + } + } + if (has_other) { + return QRCode.MODE_8BIT_BYTE; + } else if (has_alphanumeric) { + return QRCode.MODE_ALPHANUMERIC; + } else if (has_numeric) { + return QRCode.MODE_NUMERIC; + } + // "bytes" must be empty to reach here. + Debug.DCHECK(bytes.empty()); return QRCode.MODE_8BIT_BYTE; - } else if (has_alphanumeric) { - return QRCode.MODE_ALPHANUMERIC; - } else if (has_numeric) { - return QRCode.MODE_NUMERIC; } - // "bytes" must be empty to reach here. - Debug.DCHECK(bytes.empty()); - return QRCode.MODE_8BIT_BYTE; -} - private static int ChooseMaskPattern(final BitVector &bits, - QRCode.ECLevel ec_level, - int version, - QRCodeMatrix *matrix) { + private static int ChooseMaskPattern(final BitVector &bits, int ec_level, int version, + QRCodeMatrix *matrix) { if (!QRCode.IsValidMatrixWidth(matrix.width())) { Debug.LOG_ERROR("Invalid matrix width: " + matrix.width()); return -1; @@ -429,8 +426,7 @@ public final class Encoder { // Initialize "qr_code" according to "num_input_bytes", "ec_level", // and "mode". On success, modify "qr_code" and return true. On // error, return false. - static boolean InitQRCode(int num_input_bytes, QRCode.ECLevel ec_level, - QRCode.Mode mode, QRCode *qr_code) { + static boolean InitQRCode(int num_input_bytes, int ec_level, int mode, QRCode *qr_code) { qr_code.set_ec_level(ec_level); qr_code.set_mode(mode); @@ -634,7 +630,7 @@ public final class Encoder { // Append mode info. On success, store the result in "bits" and // return true. On error, return false. - static boolean AppendModeInfo(QRCode.Mode mode, BitVector *bits) { + static boolean AppendModeInfo(int mode, BitVector *bits) { final int code = QRCode.GetModeCode(mode); if (code == -1) { Debug.LOG_ERROR("Invalid mode: " + mode); @@ -647,10 +643,7 @@ public final class Encoder { // Append length info. On success, store the result in "bits" and // return true. On error, return false. - static boolean AppendLengthInfo(int num_bytes, - int version, - QRCode.Mode mode, - BitVector *bits) { + static boolean AppendLengthInfo(int num_bytes, int version, int mode, BitVector *bits) { int num_letters = num_bytes; // In Kanji mode, a letter is represented in two bytes. if (mode == QRCode.MODE_KANJI) { @@ -671,11 +664,9 @@ public final class Encoder { return true; } - // Append "bytes" in "mode" mode (encoding) into "bits". On - // success, store the result in "bits" and return true. On error, - // return false. - static boolean AppendBytes(final StringPiece &bytes, - QRCode.Mode mode, BitVector *bits) { + // Append "bytes" in "mode" mode (encoding) into "bits". On success, store the result in "bits" + // and return true. On error, return false. + static boolean AppendBytes(final StringPiece &bytes, int mode, BitVector *bits) { switch (mode) { case QRCode.MODE_NUMERIC: return AppendNumericBytes(bytes, bits); @@ -692,9 +683,8 @@ public final class Encoder { return false; } - // Append "bytes" to "bits" using QRCode.MODE_NUMERIC mode. - // On success, store the result in "bits" and return true. On error, - // return false. + // Append "bytes" to "bits" using QRCode.MODE_NUMERIC mode. On success, store the result in "bits" + // and return true. On error, return false. static boolean AppendNumericBytes(final StringPiece &bytes, BitVector *bits) { // Validate all the bytes first. for (int i = 0; i < bytes.size(); ++i) { diff --git a/core/src/com/google/zxing/qrcode/encoder/MatrixUtil.java b/core/src/com/google/zxing/qrcode/encoder/MatrixUtil.java index 6cdf66ab..0726b906 100644 --- a/core/src/com/google/zxing/qrcode/encoder/MatrixUtil.java +++ b/core/src/com/google/zxing/qrcode/encoder/MatrixUtil.java @@ -157,7 +157,7 @@ public final class MatrixUtil { // "version" and "mask_pattern". On success, store the result in // "matrix" and return true. On error, return false. public static boolean BuildMatrix(final BitVector &data_bits, - QRCode.ECLevel ec_level, + int ec_level, int version, int mask_pattern, QRCodeMatrix *matrix) { @@ -199,9 +199,7 @@ public final class MatrixUtil { // Embed type information. On success, modify the matrix and return // true. On error, return false. - public static boolean EmbedTypeInfo(QRCode.ECLevel ec_level, - int mask_pattern, - QRCodeMatrix *matrix) { + public static boolean EmbedTypeInfo(int ec_level, int mask_pattern, QRCodeMatrix *matrix) { BitVector type_info_bits; if (!MakeTypeInfoBits(ec_level, mask_pattern, &type_info_bits)) { return false; @@ -382,9 +380,7 @@ public final class MatrixUtil { // result in "bits" and return true. On error, return false. // Encode error correction level and mask pattern. See 8.9 of // JISX0510:2004 (p.45) for details. - public static boolean MakeTypeInfoBits(QRCode.ECLevel ec_level, - final int mask_pattern, - BitVector *bits) { + public static boolean MakeTypeInfoBits(int ec_level, final int mask_pattern, BitVector *bits) { final int ec_code = QRCode.GetECLevelCode(ec_level); if (ec_code == -1) { return false; diff --git a/core/src/com/google/zxing/qrcode/encoder/QRCode.java b/core/src/com/google/zxing/qrcode/encoder/QRCode.java index 48aca021..19dc11a6 100644 --- a/core/src/com/google/zxing/qrcode/encoder/QRCode.java +++ b/core/src/com/google/zxing/qrcode/encoder/QRCode.java @@ -46,8 +46,8 @@ public final class QRCode { // JAVAPORT: Do not remove trailing slashes yet. There are very likely conflicts with local // variables and parameters which will introduce insidious bugs. - private Mode mode_; - private ECLevel ec_level_; + private int mode_; + private int ec_level_; private int version_; private int matrix_width_; private int mask_pattern_; @@ -58,39 +58,40 @@ public final class QRCode { private QRCodeMatrix *matrix_; - // They call encoding "mode". The modes are defined in 8.3 of - // JISX0510:2004 (p.14). It's unlikely (probably we will not - // support complicated modes) but if you add an item to this, please - // also add it to ModeToString(), GetModeCode(), - // GetNumBitsForLength(), Encoder.AppendBytes(), + // They call encoding "mode". The modes are defined in 8.3 of JISX0510:2004 (p.14). It's unlikely + // (probably we will not support complicated modes) but if you add an item to this, please also + // add it to ModeToString(), GetModeCode(), GetNumBitsForLength(), Encoder.AppendBytes(), and // Encoder.ChooseMode(). - public enum Mode { - MODE_UNDEFINED = -1, - MODE_NUMERIC, - MODE_ALPHANUMERIC, - MODE_8BIT_BYTE, - MODE_KANJI, // Shift_JIS - // The following modes are unimplemented. - // MODE_ECI, - // MODE_MIXED, - // MODE_CONCATENATED, - // MODE_FNC1, - NUM_MODES, // Always keep this at the end. - }; + // + // JAVAPORT: These used to be C++ enums, but the code evaluates them as integers, and requires + // negative values. I don't want to take the ParsedResultType approach of a class full of statics + // of that class's type. The best compromise here is integer constants. + // + // Formerly enum Mode + public static final int MODE_UNDEFINED = -1; + public static final int MODE_NUMERIC = 0; + public static final int MODE_ALPHANUMERIC = 1; + public static final int MODE_8BIT_BYTE = 2; + public static final int MODE_KANJI = 3; // Shift_JIS + // The following modes are unimplemented. + // MODE_ECI, + // MODE_MIXED, + // MODE_CONCATENATED, + // MODE_FNC1, + public static final int NUM_MODES = 4; - // The error correction levels are defined in the table 22 of - // JISX0510:2004 (p.45). It's very unlikely (we've already covered - // all of them!) but if you add an item to this, please also add it - // to ECLevelToString() and GetECLevelCode(). - public enum ECLevel { - EC_LEVEL_UNDEFINED = -1, - // They don't have names in the standard! - EC_LEVEL_L, // 7% of corruption can be recovered. - EC_LEVEL_M, // 15% - EC_LEVEL_Q, // 25% - EC_LEVEL_H, // 30% - NUM_EC_LEVELS, // Always keep this at the end. - }; + // The error correction levels are defined in the table 22 of JISX0510:2004 (p.45). It's very + // unlikely (we've already covered all of them!) but if you add an item to this, please also add + // it to ECLevelToString() and GetECLevelCode(). + // + // Formerly enum ECLevel + public static final int EC_LEVEL_UNDEFINED = -1; + // They don't have names in the standard! + public static final int EC_LEVEL_L = 0; // 7% of corruption can be recovered. + public static final int EC_LEVEL_M = 1; // 15% + public static final int EC_LEVEL_Q = 2; // 25% + public static final int EC_LEVEL_H = 3; // 30% + public static final int NUM_EC_LEVELS = 4; public QRCode() { mode_ = MODE_UNDEFINED; @@ -106,9 +107,9 @@ public final class QRCode { } // Mode of the QR Code. - public Mode mode() { return mode_; } + public int mode() { return mode_; } // Error correction level of the QR Code. - public ECLevel ec_level() { return ec_level_; } + public int ec_level() { return ec_level_; } // Version of the QR Code. The bigger size, the bigger version. public int version() { return version_; } // Matrix width of the QR Code. @@ -199,8 +200,8 @@ public final class QRCode { return result; } - public void set_mode(Mode value) { mode_ = value; } - public void set_ec_level(ECLevel value) { ec_level_ = value; } + public void set_mode(int value) { mode_ = value; } + public void set_ec_level(int value) { ec_level_ = value; } public void set_version(int value) { version_ = value; } public void set_matrix_width(int value) { matrix_width_ = value; } public void set_mask_pattern(int value) { mask_pattern_ = value; } @@ -218,11 +219,11 @@ public final class QRCode { return version >= kMinVersion && version <= kMaxVersion; } // Check if "mask_pattern" is valid. - public static boolean IsValidECLevel(ECLevel ec_level) { + public static boolean IsValidECLevel(int ec_level) { return ec_level >= 0 && ec_level < NUM_EC_LEVELS; } // Check if "mode" is valid. - public static boolean IsValidMode(final QRCode.Mode mode) { + public static boolean IsValidMode(final int mode) { return mode >= 0 && mode < NUM_MODES; } // Check if "width" is valid. @@ -235,47 +236,47 @@ public final class QRCode { } // Convert "ec_level" to String for debugging. - public static final char *ECLevelToString(QRCode.ECLevel ec_level) { - switch (ec_level) { - case QRCode.EC_LEVEL_UNDEFINED: - return "UNDEFINED"; - case QRCode.EC_LEVEL_L: - return "L"; - case QRCode.EC_LEVEL_M: - return "M"; - case QRCode.EC_LEVEL_Q: - return "Q"; - case QRCode.EC_LEVEL_H: - return "H"; - default: - break; + public static final String ECLevelToString(int ec_level) { + switch (ec_level) { + case QRCode.EC_LEVEL_UNDEFINED: + return "UNDEFINED"; + case QRCode.EC_LEVEL_L: + return "L"; + case QRCode.EC_LEVEL_M: + return "M"; + case QRCode.EC_LEVEL_Q: + return "Q"; + case QRCode.EC_LEVEL_H: + return "H"; + default: + break; + } + return "UNKNOWN"; } - return "UNKNOWN"; -} // Convert "mode" to String for debugging. - public static final char *ModeToString(QRCode.Mode mode) { - switch (mode) { - case QRCode.MODE_UNDEFINED: - return "UNDEFINED"; - case QRCode.MODE_NUMERIC: - return "NUMERIC"; - case QRCode.MODE_ALPHANUMERIC: - return "ALPHANUMERIC"; - case QRCode.MODE_8BIT_BYTE: - return "8BIT_BYTE"; - case QRCode.MODE_KANJI: - return "KANJI"; - default: - break; + public static final String ModeToString(int mode) { + switch (mode) { + case QRCode.MODE_UNDEFINED: + return "UNDEFINED"; + case QRCode.MODE_NUMERIC: + return "NUMERIC"; + case QRCode.MODE_ALPHANUMERIC: + return "ALPHANUMERIC"; + case QRCode.MODE_8BIT_BYTE: + return "8BIT_BYTE"; + case QRCode.MODE_KANJI: + return "KANJI"; + default: + break; + } + return "UNKNOWN"; } - return "UNKNOWN"; -} // Return the code of error correction level. On error, return -1. // The codes of error correction levels are defined in the table 22 // of JISX0510:2004 (p.45). - public static int GetECLevelCode(final QRCode.ECLevel ec_level) { + public static int GetECLevelCode(final int ec_level) { switch (ec_level) { case QRCode.EC_LEVEL_L: return 1; @@ -294,7 +295,7 @@ public final class QRCode { // Return the code of mode. On error, return -1. // The codes of modes are defined in the table 2 of JISX0510:2004 // (p.16). - public static int GetModeCode(final QRCode.Mode mode) { + public static int GetModeCode(final int mode) { switch (mode) { case QRCode.MODE_NUMERIC: return 1; @@ -312,7 +313,7 @@ public final class QRCode { // Return the number of bits needed for representing the length info // of QR Code with "version" and "mode". On error, return -1. - public static int GetNumBitsForLength(int version, QRCode.Mode mode) { + public static int GetNumBitsForLength(int version, int mode) { if (!IsValidVersion(version)) { Debug.LOG_ERROR("Invalid version: " + version); return -1; diff --git a/core/src/com/google/zxing/qrcode/encoder/Renderer.java b/core/src/com/google/zxing/qrcode/encoder/Renderer.java index 789df3e7..af9a431d 100644 --- a/core/src/com/google/zxing/qrcode/encoder/Renderer.java +++ b/core/src/com/google/zxing/qrcode/encoder/Renderer.java @@ -119,10 +119,8 @@ public final class Renderer { // Similar to RenderAsPNG but it renders QR code from data in // "bytes" with error correction level "ec_level". This is the // friendliest function in the QR code library. - public static boolean RenderAsPNGFromData(final StringPiece& bytes, - QRCode.ECLevel ec_level, - int cell_size, - String *result) { + public static boolean RenderAsPNGFromData(final StringPiece& bytes, int ec_level, int cell_size, + String *result) { QRCode qr_code; if (!Encoder.Encode(bytes, ec_level, &qr_code)) { return false; -- 2.20.1