git-svn-id: http://zxing.googlecode.com/svn/trunk@6 59b500cc-1b3d-0410-9834-0bbf25fbcc57
[zxing.git] / core / src / com / google / zxing / qrcode / decoder / Mode.java
1 /*
2  * Copyright 2007 Google Inc.
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 import com.google.zxing.ReaderException;
20
21 /**
22  * <p>See ISO 18004:2006, 6.4.1, Tables 2 and 3. This enum encapsulates the various modes in which
23  * data can be encoded to bits in the QR code standard.</p>
24  *
25  * @author srowen@google.com (Sean Owen)
26  */
27 final class Mode {
28
29   // No, we can't use an enum here. J2ME doesn't support it.
30   
31   static final Mode TERMINATOR = new Mode(new int[] {0, 0, 0}); // Not really a mode...
32   static final Mode NUMERIC = new Mode(new int[] {10, 12, 14});
33   static final Mode ALPHANUMERIC = new Mode(new int[] {9, 11, 13});
34   static final Mode BYTE = new Mode(new int[] {8, 16, 16});
35   static final Mode KANJI = new Mode(new int[] {8, 10, 12});
36
37   private int[] characterCountBitsForVersions;
38
39   private Mode(int[] characterCountBitsForVersions) {
40     this.characterCountBitsForVersions = characterCountBitsForVersions;
41   }
42
43   static Mode forBits(int bits) throws ReaderException {
44     switch (bits) {
45       case 0x0:
46         return TERMINATOR;
47       case 0x1:
48         return NUMERIC;
49       case 0x2:
50         return ALPHANUMERIC;
51       case 0x4:
52         return BYTE;
53       case 0x8:
54         return KANJI;
55       default:
56         throw new ReaderException("Illegal mode bits: " + bits);
57     }
58   }
59
60   int getCharacterCountBits(Version version) {
61     int number = version.getVersionNumber();
62     int offset;
63     if (number <= 9) {
64       offset = 0;
65     } else if (number <= 26) {
66       offset = 1;
67     } else {
68       offset = 2;
69     }
70     return characterCountBitsForVersions[offset];
71   }
72
73 }