Converted the Mode and ECLevel enums in QRCode.java.

git-svn-id: https://zxing.googlecode.com/svn/trunk@695 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
dswitkin 2008-11-13 17:12:53 +00:00
parent f50305895d
commit 60f3af2607
4 changed files with 115 additions and 130 deletions

View file

@ -285,10 +285,9 @@ public final class Encoder {
// Note that there is no way to encode bytes in MODE_KANJI. We might // Note that there is no way to encode bytes in MODE_KANJI. We might
// want to add EncodeWithMode() with which clients can specify the // want to add EncodeWithMode() with which clients can specify the
// encoding mode. For now, we don't need the functionality. // encoding mode. For now, we don't need the functionality.
static boolean Encode(final StringPiece& bytes, QRCode.ECLevel ec_level, static boolean Encode(final StringPiece& bytes, int ec_level, QRCode *qr_code) {
QRCode *qr_code) {
// Step 1: Choose the mode (encoding). // 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. // Step 2: Append "bytes" into "data_bits" in appropriate encoding.
BitVector data_bits; BitVector data_bits;
@ -372,7 +371,7 @@ public final class Encoder {
// distinguish Shift_JIS from other encodings such as ISO-8859-1, from // distinguish Shift_JIS from other encodings such as ISO-8859-1, from
// data bytes alone. For example "\xE0\xE0" can be interpreted as one // data bytes alone. For example "\xE0\xE0" can be interpreted as one
// character in Shift_JIS, but also two characters in ISO-8859-1. // character in Shift_JIS, but also two characters in ISO-8859-1.
static QRCode.Mode ChooseMode(final StringPiece &bytes) { static int ChooseMode(final StringPiece &bytes) {
boolean has_numeric = false; boolean has_numeric = false;
boolean has_alphanumeric = false; boolean has_alphanumeric = false;
boolean has_other = false; boolean has_other = false;
@ -398,9 +397,7 @@ public final class Encoder {
return QRCode.MODE_8BIT_BYTE; return QRCode.MODE_8BIT_BYTE;
} }
private static int ChooseMaskPattern(final BitVector &bits, private static int ChooseMaskPattern(final BitVector &bits, int ec_level, int version,
QRCode.ECLevel ec_level,
int version,
QRCodeMatrix *matrix) { QRCodeMatrix *matrix) {
if (!QRCode.IsValidMatrixWidth(matrix.width())) { if (!QRCode.IsValidMatrixWidth(matrix.width())) {
Debug.LOG_ERROR("Invalid matrix width: " + matrix.width()); Debug.LOG_ERROR("Invalid matrix width: " + matrix.width());
@ -429,8 +426,7 @@ public final class Encoder {
// Initialize "qr_code" according to "num_input_bytes", "ec_level", // Initialize "qr_code" according to "num_input_bytes", "ec_level",
// and "mode". On success, modify "qr_code" and return true. On // and "mode". On success, modify "qr_code" and return true. On
// error, return false. // error, return false.
static boolean InitQRCode(int num_input_bytes, QRCode.ECLevel ec_level, static boolean InitQRCode(int num_input_bytes, int ec_level, int mode, QRCode *qr_code) {
QRCode.Mode mode, QRCode *qr_code) {
qr_code.set_ec_level(ec_level); qr_code.set_ec_level(ec_level);
qr_code.set_mode(mode); qr_code.set_mode(mode);
@ -634,7 +630,7 @@ public final class Encoder {
// Append mode info. On success, store the result in "bits" and // Append mode info. On success, store the result in "bits" and
// return true. On error, return false. // 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); final int code = QRCode.GetModeCode(mode);
if (code == -1) { if (code == -1) {
Debug.LOG_ERROR("Invalid mode: " + mode); 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 // Append length info. On success, store the result in "bits" and
// return true. On error, return false. // return true. On error, return false.
static boolean AppendLengthInfo(int num_bytes, static boolean AppendLengthInfo(int num_bytes, int version, int mode, BitVector *bits) {
int version,
QRCode.Mode mode,
BitVector *bits) {
int num_letters = num_bytes; int num_letters = num_bytes;
// In Kanji mode, a letter is represented in two bytes. // In Kanji mode, a letter is represented in two bytes.
if (mode == QRCode.MODE_KANJI) { if (mode == QRCode.MODE_KANJI) {
@ -671,11 +664,9 @@ public final class Encoder {
return true; return true;
} }
// Append "bytes" in "mode" mode (encoding) into "bits". On // Append "bytes" in "mode" mode (encoding) into "bits". On success, store the result in "bits"
// success, store the result in "bits" and return true. On error, // and return true. On error, return false.
// return false. static boolean AppendBytes(final StringPiece &bytes, int mode, BitVector *bits) {
static boolean AppendBytes(final StringPiece &bytes,
QRCode.Mode mode, BitVector *bits) {
switch (mode) { switch (mode) {
case QRCode.MODE_NUMERIC: case QRCode.MODE_NUMERIC:
return AppendNumericBytes(bytes, bits); return AppendNumericBytes(bytes, bits);
@ -692,9 +683,8 @@ public final class Encoder {
return false; return false;
} }
// Append "bytes" to "bits" using QRCode.MODE_NUMERIC mode. // Append "bytes" to "bits" using QRCode.MODE_NUMERIC mode. On success, store the result in "bits"
// On success, store the result in "bits" and return true. On error, // and return true. On error, return false.
// return false.
static boolean AppendNumericBytes(final StringPiece &bytes, BitVector *bits) { static boolean AppendNumericBytes(final StringPiece &bytes, BitVector *bits) {
// Validate all the bytes first. // Validate all the bytes first.
for (int i = 0; i < bytes.size(); ++i) { for (int i = 0; i < bytes.size(); ++i) {

View file

@ -157,7 +157,7 @@ public final class MatrixUtil {
// "version" and "mask_pattern". On success, store the result in // "version" and "mask_pattern". On success, store the result in
// "matrix" and return true. On error, return false. // "matrix" and return true. On error, return false.
public static boolean BuildMatrix(final BitVector &data_bits, public static boolean BuildMatrix(final BitVector &data_bits,
QRCode.ECLevel ec_level, int ec_level,
int version, int version,
int mask_pattern, int mask_pattern,
QRCodeMatrix *matrix) { QRCodeMatrix *matrix) {
@ -199,9 +199,7 @@ public final class MatrixUtil {
// Embed type information. On success, modify the matrix and return // Embed type information. On success, modify the matrix and return
// true. On error, return false. // true. On error, return false.
public static boolean EmbedTypeInfo(QRCode.ECLevel ec_level, public static boolean EmbedTypeInfo(int ec_level, int mask_pattern, QRCodeMatrix *matrix) {
int mask_pattern,
QRCodeMatrix *matrix) {
BitVector type_info_bits; BitVector type_info_bits;
if (!MakeTypeInfoBits(ec_level, mask_pattern, &type_info_bits)) { if (!MakeTypeInfoBits(ec_level, mask_pattern, &type_info_bits)) {
return false; return false;
@ -382,9 +380,7 @@ public final class MatrixUtil {
// result in "bits" and return true. On error, return false. // result in "bits" and return true. On error, return false.
// Encode error correction level and mask pattern. See 8.9 of // Encode error correction level and mask pattern. See 8.9 of
// JISX0510:2004 (p.45) for details. // JISX0510:2004 (p.45) for details.
public static boolean MakeTypeInfoBits(QRCode.ECLevel ec_level, public static boolean MakeTypeInfoBits(int ec_level, final int mask_pattern, BitVector *bits) {
final int mask_pattern,
BitVector *bits) {
final int ec_code = QRCode.GetECLevelCode(ec_level); final int ec_code = QRCode.GetECLevelCode(ec_level);
if (ec_code == -1) { if (ec_code == -1) {
return false; return false;

View file

@ -46,8 +46,8 @@ public final class QRCode {
// JAVAPORT: Do not remove trailing slashes yet. There are very likely conflicts with local // JAVAPORT: Do not remove trailing slashes yet. There are very likely conflicts with local
// variables and parameters which will introduce insidious bugs. // variables and parameters which will introduce insidious bugs.
private Mode mode_; private int mode_;
private ECLevel ec_level_; private int ec_level_;
private int version_; private int version_;
private int matrix_width_; private int matrix_width_;
private int mask_pattern_; private int mask_pattern_;
@ -58,39 +58,40 @@ public final class QRCode {
private QRCodeMatrix *matrix_; private QRCodeMatrix *matrix_;
// They call encoding "mode". The modes are defined in 8.3 of // They call encoding "mode". The modes are defined in 8.3 of JISX0510:2004 (p.14). It's unlikely
// JISX0510:2004 (p.14). It's unlikely (probably we will not // (probably we will not support complicated modes) but if you add an item to this, please also
// support complicated modes) but if you add an item to this, please // add it to ModeToString(), GetModeCode(), GetNumBitsForLength(), Encoder.AppendBytes(), and
// also add it to ModeToString(), GetModeCode(),
// GetNumBitsForLength(), Encoder.AppendBytes(),
// Encoder.ChooseMode(). // Encoder.ChooseMode().
public enum Mode { //
MODE_UNDEFINED = -1, // JAVAPORT: These used to be C++ enums, but the code evaluates them as integers, and requires
MODE_NUMERIC, // negative values. I don't want to take the ParsedResultType approach of a class full of statics
MODE_ALPHANUMERIC, // of that class's type. The best compromise here is integer constants.
MODE_8BIT_BYTE, //
MODE_KANJI, // Shift_JIS // 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. // The following modes are unimplemented.
// MODE_ECI, // MODE_ECI,
// MODE_MIXED, // MODE_MIXED,
// MODE_CONCATENATED, // MODE_CONCATENATED,
// MODE_FNC1, // MODE_FNC1,
NUM_MODES, // Always keep this at the end. public static final int NUM_MODES = 4;
};
// The error correction levels are defined in the table 22 of // The error correction levels are defined in the table 22 of JISX0510:2004 (p.45). It's very
// JISX0510:2004 (p.45). It's very unlikely (we've already covered // unlikely (we've already covered all of them!) but if you add an item to this, please also add
// all of them!) but if you add an item to this, please also add it // it to ECLevelToString() and GetECLevelCode().
// to ECLevelToString() and GetECLevelCode(). //
public enum ECLevel { // Formerly enum ECLevel
EC_LEVEL_UNDEFINED = -1, public static final int EC_LEVEL_UNDEFINED = -1;
// They don't have names in the standard! // They don't have names in the standard!
EC_LEVEL_L, // 7% of corruption can be recovered. public static final int EC_LEVEL_L = 0; // 7% of corruption can be recovered.
EC_LEVEL_M, // 15% public static final int EC_LEVEL_M = 1; // 15%
EC_LEVEL_Q, // 25% public static final int EC_LEVEL_Q = 2; // 25%
EC_LEVEL_H, // 30% public static final int EC_LEVEL_H = 3; // 30%
NUM_EC_LEVELS, // Always keep this at the end. public static final int NUM_EC_LEVELS = 4;
};
public QRCode() { public QRCode() {
mode_ = MODE_UNDEFINED; mode_ = MODE_UNDEFINED;
@ -106,9 +107,9 @@ public final class QRCode {
} }
// Mode of the QR Code. // Mode of the QR Code.
public Mode mode() { return mode_; } public int mode() { return mode_; }
// Error correction level of the QR Code. // 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. // Version of the QR Code. The bigger size, the bigger version.
public int version() { return version_; } public int version() { return version_; }
// Matrix width of the QR Code. // Matrix width of the QR Code.
@ -199,8 +200,8 @@ public final class QRCode {
return result; return result;
} }
public void set_mode(Mode value) { mode_ = value; } public void set_mode(int value) { mode_ = value; }
public void set_ec_level(ECLevel value) { ec_level_ = value; } public void set_ec_level(int value) { ec_level_ = value; }
public void set_version(int value) { version_ = value; } public void set_version(int value) { version_ = value; }
public void set_matrix_width(int value) { matrix_width_ = value; } public void set_matrix_width(int value) { matrix_width_ = value; }
public void set_mask_pattern(int value) { mask_pattern_ = value; } public void set_mask_pattern(int value) { mask_pattern_ = value; }
@ -218,11 +219,11 @@ public final class QRCode {
return version >= kMinVersion && version <= kMaxVersion; return version >= kMinVersion && version <= kMaxVersion;
} }
// Check if "mask_pattern" is valid. // 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; return ec_level >= 0 && ec_level < NUM_EC_LEVELS;
} }
// Check if "mode" is valid. // 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; return mode >= 0 && mode < NUM_MODES;
} }
// Check if "width" is valid. // Check if "width" is valid.
@ -235,7 +236,7 @@ public final class QRCode {
} }
// Convert "ec_level" to String for debugging. // Convert "ec_level" to String for debugging.
public static final char *ECLevelToString(QRCode.ECLevel ec_level) { public static final String ECLevelToString(int ec_level) {
switch (ec_level) { switch (ec_level) {
case QRCode.EC_LEVEL_UNDEFINED: case QRCode.EC_LEVEL_UNDEFINED:
return "UNDEFINED"; return "UNDEFINED";
@ -254,7 +255,7 @@ public final class QRCode {
} }
// Convert "mode" to String for debugging. // Convert "mode" to String for debugging.
public static final char *ModeToString(QRCode.Mode mode) { public static final String ModeToString(int mode) {
switch (mode) { switch (mode) {
case QRCode.MODE_UNDEFINED: case QRCode.MODE_UNDEFINED:
return "UNDEFINED"; return "UNDEFINED";
@ -275,7 +276,7 @@ public final class QRCode {
// Return the code of error correction level. On error, return -1. // Return the code of error correction level. On error, return -1.
// The codes of error correction levels are defined in the table 22 // The codes of error correction levels are defined in the table 22
// of JISX0510:2004 (p.45). // 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) { switch (ec_level) {
case QRCode.EC_LEVEL_L: case QRCode.EC_LEVEL_L:
return 1; return 1;
@ -294,7 +295,7 @@ public final class QRCode {
// Return the code of mode. On error, return -1. // Return the code of mode. On error, return -1.
// The codes of modes are defined in the table 2 of JISX0510:2004 // The codes of modes are defined in the table 2 of JISX0510:2004
// (p.16). // (p.16).
public static int GetModeCode(final QRCode.Mode mode) { public static int GetModeCode(final int mode) {
switch (mode) { switch (mode) {
case QRCode.MODE_NUMERIC: case QRCode.MODE_NUMERIC:
return 1; return 1;
@ -312,7 +313,7 @@ public final class QRCode {
// Return the number of bits needed for representing the length info // Return the number of bits needed for representing the length info
// of QR Code with "version" and "mode". On error, return -1. // 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)) { if (!IsValidVersion(version)) {
Debug.LOG_ERROR("Invalid version: " + version); Debug.LOG_ERROR("Invalid version: " + version);
return -1; return -1;

View file

@ -119,9 +119,7 @@ public final class Renderer {
// Similar to RenderAsPNG but it renders QR code from data in // Similar to RenderAsPNG but it renders QR code from data in
// "bytes" with error correction level "ec_level". This is the // "bytes" with error correction level "ec_level". This is the
// friendliest function in the QR code library. // friendliest function in the QR code library.
public static boolean RenderAsPNGFromData(final StringPiece& bytes, public static boolean RenderAsPNGFromData(final StringPiece& bytes, int ec_level, int cell_size,
QRCode.ECLevel ec_level,
int cell_size,
String *result) { String *result) {
QRCode qr_code; QRCode qr_code;
if (!Encoder.Encode(bytes, ec_level, &qr_code)) { if (!Encoder.Encode(bytes, ec_level, &qr_code)) {