2 * Copyright (C) 2010 ZXing authors
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.oned.rss;
19 import com.google.zxing.NotFoundException;
20 import com.google.zxing.oned.OneDReader;
22 public abstract class AbstractRSSReader extends OneDReader {
24 private static final int MAX_AVG_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.2f);
25 private static final int MAX_INDIVIDUAL_VARIANCE = (int) (PATTERN_MATCH_RESULT_SCALE_FACTOR * 0.4f);
27 private static final float MIN_FINDER_PATTERN_RATIO = 9.5f / 12.0f;
28 private static final float MAX_FINDER_PATTERN_RATIO = 12.5f / 14.0f;
30 protected final int[] decodeFinderCounters;
31 protected final int[] dataCharacterCounters;
32 protected final float[] oddRoundingErrors;
33 protected final float[] evenRoundingErrors;
34 protected final int[] oddCounts;
35 protected final int[] evenCounts;
37 protected AbstractRSSReader(){
38 decodeFinderCounters = new int[4];
39 dataCharacterCounters = new int[8];
40 oddRoundingErrors = new float[4];
41 evenRoundingErrors = new float[4];
42 oddCounts = new int[dataCharacterCounters.length / 2];
43 evenCounts = new int[dataCharacterCounters.length / 2];
47 protected static int parseFinderValue(int[] counters, int [][] finderPatterns) throws NotFoundException {
48 for (int value = 0; value < finderPatterns.length; value++) {
49 if (patternMatchVariance(counters, finderPatterns[value], MAX_INDIVIDUAL_VARIANCE) <
54 throw NotFoundException.getNotFoundInstance();
57 protected static int count(int[] array) {
59 for (int i = 0; i < array.length; i++) {
65 protected static void increment(int[] array, float[] errors) {
67 float biggestError = errors[0];
68 for (int i = 1; i < array.length; i++) {
69 if (errors[i] > biggestError) {
70 biggestError = errors[i];
77 protected static void decrement(int[] array, float[] errors) {
79 float biggestError = errors[0];
80 for (int i = 1; i < array.length; i++) {
81 if (errors[i] < biggestError) {
82 biggestError = errors[i];
89 protected static boolean isFinderPattern(int[] counters) {
90 int firstTwoSum = counters[0] + counters[1];
91 int sum = firstTwoSum + counters[2] + counters[3];
92 float ratio = (float) firstTwoSum / (float) sum;
93 if (ratio >= MIN_FINDER_PATTERN_RATIO && ratio <= MAX_FINDER_PATTERN_RATIO) {
94 // passes ratio test in spec, but see if the counts are unreasonable
95 int minCounter = Integer.MAX_VALUE;
96 int maxCounter = Integer.MIN_VALUE;
97 for (int i = 0; i < counters.length; i++) {
98 int counter = counters[i];
99 if (counter > maxCounter) {
100 maxCounter = counter;
102 if (counter < minCounter) {
103 minCounter = counter;
106 return maxCounter < 10 * minCounter;