Add to result the raw, but parsed, bytes of byte segments in 2D barcodes
[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 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 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 ECI = new Mode(null); // character counts don't apply
36   static final Mode KANJI = new Mode(new int[]{8, 10, 12});
37   static final Mode FNC1_FIRST_POSITION = new Mode(null);
38   static final Mode FNC1_SECOND_POSITION = new Mode(null);
39
40   private final int[] characterCountBitsForVersions;
41
42   private Mode(int[] characterCountBitsForVersions) {
43     this.characterCountBitsForVersions = characterCountBitsForVersions;
44   }
45
46   /**
47    * @param bits four bits encoding a QR Code data mode
48    * @return {@link Mode} encoded by these bits
49    * @throws ReaderException if bits do not correspond to a known mode
50    */
51   static Mode forBits(int bits) throws ReaderException {
52     switch (bits) {
53       case 0x0:
54         return TERMINATOR;
55       case 0x1:
56         return NUMERIC;
57       case 0x2:
58         return ALPHANUMERIC;
59       case 0x4:
60         return BYTE;
61       case 0x5:
62         return FNC1_FIRST_POSITION;
63       case 0x7:
64         return ECI;
65       case 0x8:
66         return KANJI;
67       case 0x9:
68         return FNC1_SECOND_POSITION;
69       default:
70         throw new ReaderException("Unsupported mode bits: " + bits);
71     }
72   }
73
74   /**
75    * @param version version in question
76    * @return number of bits used, in this QR Code symbol {@link Version}, to encode the
77    *         count of characters that will follow encoded in this {@link Mode}
78    */
79   int getCharacterCountBits(Version version) {
80     if (characterCountBitsForVersions == null) {
81       throw new IllegalArgumentException("Character count doesn't apply to this mode");
82     }
83     int number = version.getVersionNumber();
84     int offset;
85     if (number <= 9) {
86       offset = 0;
87     } else if (number <= 26) {
88       offset = 1;
89     } else {
90       offset = 2;
91     }
92     return characterCountBitsForVersions[offset];
93   }
94
95 }