Move character encoding logic out to common, try again to improve its handling of...
[zxing.git] / core / src / com / google / zxing / qrcode / decoder / Mode.java
1 /*
2  * Copyright 2007 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.qrcode.decoder;
18
19 /**
20  * <p>See ISO 18004:2006, 6.4.1, Tables 2 and 3. This enum encapsulates the various modes in which
21  * data can be encoded to bits in the QR code standard.</p>
22  *
23  * @author Sean Owen
24  */
25 public final class Mode {
26
27   // No, we can't use an enum here. J2ME doesn't support it.
28
29   public static final Mode TERMINATOR = new Mode(new int[]{0, 0, 0}, 0x00, "TERMINATOR"); // Not really a mode...
30   public static final Mode NUMERIC = new Mode(new int[]{10, 12, 14}, 0x01, "NUMERIC");
31   public static final Mode ALPHANUMERIC = new Mode(new int[]{9, 11, 13}, 0x02, "ALPHANUMERIC");
32   public static final Mode STRUCTURED_APPEND = new Mode(new int[]{0, 0, 0}, 0x03, "STRUCTURED_APPEND"); // Not supported
33   public static final Mode BYTE = new Mode(new int[]{8, 16, 16}, 0x04, "BYTE");
34   public static final Mode ECI = new Mode(null, 0x07, "ECI"); // character counts don't apply
35   public static final Mode KANJI = new Mode(new int[]{8, 10, 12}, 0x08, "KANJI");
36   public static final Mode FNC1_FIRST_POSITION = new Mode(null, 0x05, "FNC1_FIRST_POSITION");
37   public static final Mode FNC1_SECOND_POSITION = new Mode(null, 0x09, "FNC1_SECOND_POSITION");
38
39   private final int[] characterCountBitsForVersions;
40   private final int bits;
41   private final String name;
42
43   private Mode(int[] characterCountBitsForVersions, int bits, String name) {
44     this.characterCountBitsForVersions = characterCountBitsForVersions;
45     this.bits = bits;
46     this.name = name;
47   }
48
49   /**
50    * @param bits four bits encoding a QR Code data mode
51    * @return {@link Mode} encoded by these bits
52    * @throws IllegalArgumentException if bits do not correspond to a known mode
53    */
54   public static Mode forBits(int bits) {
55     switch (bits) {
56       case 0x0:
57         return TERMINATOR;
58       case 0x1:
59         return NUMERIC;
60       case 0x2:
61         return ALPHANUMERIC;
62       case 0x3:
63         return STRUCTURED_APPEND;
64       case 0x4:
65         return BYTE;
66       case 0x5:
67         return FNC1_FIRST_POSITION;
68       case 0x7:
69         return ECI;
70       case 0x8:
71         return KANJI;
72       case 0x9:
73         return FNC1_SECOND_POSITION;
74       default:
75         throw new IllegalArgumentException();
76     }
77   }
78
79   /**
80    * @param version version in question
81    * @return number of bits used, in this QR Code symbol {@link Version}, to encode the
82    *         count of characters that will follow encoded in this {@link Mode}
83    */
84   public int getCharacterCountBits(Version version) {
85     if (characterCountBitsForVersions == null) {
86       throw new IllegalArgumentException("Character count doesn't apply to this mode");
87     }
88     int number = version.getVersionNumber();
89     int offset;
90     if (number <= 9) {
91       offset = 0;
92     } else if (number <= 26) {
93       offset = 1;
94     } else {
95       offset = 2;
96     }
97     return characterCountBitsForVersions[offset];
98   }
99
100   public int getBits() {
101     return bits;
102   }
103
104   public String getName() {
105     return name;
106   }
107
108   public String toString() {
109     return name;
110   }
111
112 }