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 enum Mode {
26
27 TERMINATOR(new int[]{0, 0, 0}, 0x00), // Not really a mode...
28 NUMERIC(new int[]{10, 12, 14}, 0x01),
29 ALPHANUMERIC(new int[]{9, 11, 13}, 0x02),
30 STRUCTURED_APPEND(new int[]{0, 0, 0}, 0x03), // Not supported
31 BYTE(new int[]{8, 16, 16}, 0x04),
32 ECI(new int[]{0, 0, 0}, 0x07), // character counts don't apply
33 KANJI(new int[]{8, 10, 12}, 0x08),
34 FNC1_FIRST_POSITION(new int[]{0, 0, 0}, 0x05),
35 FNC1_SECOND_POSITION(new int[]{0, 0, 0}, 0x09),
36 /** See GBT 18284-2000; "Hanzi" is a transliteration of this mode name. */
37 HANZI(new int[]{8, 10, 12}, 0x0D);
38
39 private final int[] characterCountBitsForVersions;
40 private final int bits;
41
42 Mode(int[] characterCountBitsForVersions, int bits) {
43 this.characterCountBitsForVersions = characterCountBitsForVersions;
44 this.bits = bits;
45 }
46
47 /**
48 * @param bits four bits encoding a QR Code data mode
49 * @return Mode encoded by these bits
50 * @throws IllegalArgumentException if bits do not correspond to a known mode
51 */
52 public static Mode forBits(int bits) {
53 switch (bits) {
54 case 0x0:
55 return TERMINATOR;
56 case 0x1:
57 return NUMERIC;
58 case 0x2:
59 return ALPHANUMERIC;
60 case 0x3:
61 return STRUCTURED_APPEND;
62 case 0x4:
63 return BYTE;
64 case 0x5:
65 return FNC1_FIRST_POSITION;
66 case 0x7:
67 return ECI;
68 case 0x8:
69 return KANJI;
70 case 0x9:
71 return FNC1_SECOND_POSITION;
72 case 0xD:
73 // 0xD is defined in GBT 18284-2000, may not be supported in foreign country
74 return HANZI;
75 default:
76 throw new IllegalArgumentException();
77 }
78 }
79
80 /**
81 * @param version version in question
82 * @return number of bits used, in this QR Code symbol {@link Version}, to encode the
83 * count of characters that will follow encoded in this Mode
84 */
85 public int getCharacterCountBits(Version version) {
86 int number = version.getVersionNumber();
87 int offset;
88 if (number <= 9) {
89 offset = 0;
90 } else if (number <= 26) {
91 offset = 1;
92 } else {
93 offset = 2;
94 }
95 return characterCountBitsForVersions[offset];
96 }
97
98 public int getBits() {
99 return bits;
100 }
101
102 }
103