2 * Copyright 2007 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.qrcode.decoder;
20 * @author srowen@google.com (Sean Owen)
22 final class FormatInformation {
24 private static final int FORMAT_INFO_MASK_QR = 0x5412;
27 * See ISO 18004:2006, Annex C, Table C.1
29 private static final int[][] FORMAT_INFO_DECODE_LOOKUP = new int[][]{
64 private static final int[] BITS_SET_IN_HALF_BYTE =
65 new int[]{0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
67 private final ErrorCorrectionLevel errorCorrectionLevel;
68 private final byte dataMask;
70 private FormatInformation(int formatInfo) {
72 errorCorrectionLevel =
73 ErrorCorrectionLevel.forBits((formatInfo >> 3) & 0x03);
75 dataMask = (byte) (formatInfo & 0x07);
78 static int numBitsDiffering(int a, int b) {
81 BITS_SET_IN_HALF_BYTE[a & 0x0F] +
82 BITS_SET_IN_HALF_BYTE[(a >>> 4 & 0x0F)] +
83 BITS_SET_IN_HALF_BYTE[(a >>> 8 & 0x0F)] +
84 BITS_SET_IN_HALF_BYTE[(a >>> 12 & 0x0F)] +
85 BITS_SET_IN_HALF_BYTE[(a >>> 16 & 0x0F)] +
86 BITS_SET_IN_HALF_BYTE[(a >>> 20 & 0x0F)] +
87 BITS_SET_IN_HALF_BYTE[(a >>> 24 & 0x0F)] +
88 BITS_SET_IN_HALF_BYTE[(a >>> 28 & 0x0F)];
91 static FormatInformation decodeFormatInformation(int rawFormatInfo) {
92 FormatInformation formatInfo = doDecodeFormatInformation(rawFormatInfo);
93 if (formatInfo != null) {
96 // Should return null, but, some QR codes apparently
97 // do not mask this info. Try again, first masking the raw bits so
98 // the function will unmask
99 return doDecodeFormatInformation(rawFormatInfo ^ FORMAT_INFO_MASK_QR);
102 private static FormatInformation doDecodeFormatInformation(
105 int unmaskedFormatInfo = rawFormatInfo ^ FORMAT_INFO_MASK_QR;
106 int bestDifference = Integer.MAX_VALUE;
107 int bestFormatInfo = 0;
108 for (int i = 0; i < FORMAT_INFO_DECODE_LOOKUP.length; i++) {
109 int[] decodeInfo = FORMAT_INFO_DECODE_LOOKUP[i];
110 int targetInfo = decodeInfo[0];
111 if (targetInfo == unmaskedFormatInfo) {
112 return new FormatInformation(decodeInfo[1]);
114 int bitsDifference = numBitsDiffering(unmaskedFormatInfo, targetInfo);
115 if (bitsDifference < bestDifference) {
116 bestFormatInfo = decodeInfo[1];
117 bestDifference = bitsDifference;
120 if (bestDifference <= 3) {
121 return new FormatInformation(bestFormatInfo);
126 ErrorCorrectionLevel getErrorCorrectionLevel() {
127 return errorCorrectionLevel;
134 public int hashCode() {
135 return (errorCorrectionLevel.ordinal() << 3) | (int) dataMask;
138 public boolean equals(Object o) {
139 if (!(o instanceof FormatInformation)) {
142 FormatInformation other = (FormatInformation) o;
143 return this.errorCorrectionLevel == other.errorCorrectionLevel &&
144 this.dataMask == other.dataMask;