5 * Created by Lukasz Warchol on 10-01-15.
6 * Copyright 2010 ZXing authors All rights reserved.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
21 #include "OneDReader.h"
22 #include <zxing/ReaderException.h>
29 OneDReader::OneDReader() {
32 Ref<Result> OneDReader::decode(Ref<BinaryBitmap> image) {
34 return doDecode(image);
35 }catch (ReaderException re) {
36 if (false /*tryHarder && image.isRotateSupported()*/) {
38 BinaryBitmap rotatedImage = image.rotateCounterClockwise();
39 Result result = doDecode(rotatedImage, hints);
40 // Record that we found it rotated 90 degrees CCW / 270 degrees CW
41 Hashtable metadata = result.getResultMetadata();
42 int orientation = 270;
43 if (metadata != null && metadata.containsKey(ResultMetadataType.ORIENTATION)) {
44 // But if we found it reversed in doDecode(), add in that result here:
45 orientation = (orientation +
46 ((Integer) metadata.get(ResultMetadataType.ORIENTATION)).intValue()) % 360;
48 result.putMetadata(ResultMetadataType.ORIENTATION, new Integer(orientation));
49 // Update result points
50 ResultPoint[] points = result.getResultPoints();
51 int height = rotatedImage.getHeight();
52 for (int i = 0; i < points.length; i++) {
53 points[i] = new ResultPoint(height - points[i].getY() - 1, points[i].getX());
63 Ref<Result> OneDReader::doDecode(Ref<BinaryBitmap> image){
64 int width = image->getWidth();
65 int height = image->getHeight();
66 Ref<BitArray> row(new BitArray(width));
67 // BitArray row = new BitArray(width);
69 int middle = height >> 1;
70 bool tryHarder = true;//hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
71 int rowStep = (int)fmax(1, height >> (tryHarder ? 7 : 4));
74 maxLines = height; // Look at the whole image, not just the center
76 maxLines = 9; // Nine rows spaced 1/16 apart is roughly the middle half of the image
79 for (int x = 0; x < maxLines; x++) {
81 // Scanning from the middle out. Determine which row we're looking at next:
82 int rowStepsAboveOrBelow = (x + 1) >> 1;
83 bool isAbove = (x & 0x01) == 0; // i.e. is x even?
84 int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow);
85 if (rowNumber < 0 || rowNumber >= height) {
86 // Oops, if we run off the top or bottom, stop
90 // Estimate black point for this row and load it:
92 row = image->getBlackRow(rowNumber, row);
93 }catch (ReaderException re) {
97 // While we have the image data in a BitArray, it's fairly cheap to reverse it in place to
98 // handle decoding upside down barcodes.
99 for (int attempt = 0; attempt < 2; attempt++) {
100 if (attempt == 1) { // trying again?
101 row->reverse(); // reverse the row and continue
104 // Look for a barcode
105 Ref<Result> result = decodeRow(rowNumber, row);
106 // We found our barcode
108 // // But it was upside down, so note that
109 // result.putMetadata(ResultMetadataType.ORIENTATION, new Integer(180));
110 // // And remember to flip the result points horizontally.
111 // ResultPoint[] points = result.getResultPoints();
112 // points[0] = new ResultPoint(width - points[0].getX() - 1, points[0].getY());
113 // points[1] = new ResultPoint(width - points[1].getX() - 1, points[1].getY());
116 } catch (ReaderException re) {
117 // continue -- just couldn't decode this row
121 throw ReaderException("");
124 int OneDReader::patternMatchVariance(int counters[], int countersSize, const int pattern[], int maxIndividualVariance) {
125 int numCounters = countersSize;
127 int patternLength = 0;
128 for (int i = 0; i < numCounters; i++) {
129 total += counters[i];
130 patternLength += pattern[i];
132 if (total < patternLength) {
133 // If we don't even have one pixel per unit of bar width, assume this is too small
134 // to reliably match, so fail:
137 // We're going to fake floating-point math in integers. We just need to use more bits.
138 // Scale up patternLength so that intermediate values below like scaledCounter will have
139 // more "significant digits"
140 int unitBarWidth = (total << INTEGER_MATH_SHIFT) / patternLength;
141 maxIndividualVariance = (maxIndividualVariance * unitBarWidth) >> INTEGER_MATH_SHIFT;
143 int totalVariance = 0;
144 for (int x = 0; x < numCounters; x++) {
145 int counter = counters[x] << INTEGER_MATH_SHIFT;
146 int scaledPattern = pattern[x] * unitBarWidth;
147 int variance = counter > scaledPattern ? counter - scaledPattern : scaledPattern - counter;
148 if (variance > maxIndividualVariance) {
151 totalVariance += variance;
153 return totalVariance / total;
156 void OneDReader::recordPattern(Ref<BitArray> row, int start, int counters[], int countersCount){
157 int numCounters = countersCount;//sizeof(counters) / sizeof(int);
158 for (int i = 0; i < numCounters; i++) {
161 int end = row->getSize();
163 throw ReaderException("recordPattern: start >= end");
165 bool isWhite = !row->get(start);
166 int counterPosition = 0;
169 bool pixel = row->get(i);
170 if (pixel ^ isWhite) { // that is, exactly one is true
171 counters[counterPosition]++;
174 if (counterPosition == numCounters) {
177 counters[counterPosition] = 1;
178 isWhite ^= true; // isWhite = !isWhite;
183 // If we read fully the last section of pixels and filled up our counters -- or filled
184 // the last counter but ran off the side of the image, OK. Otherwise, a problem.
185 if (!(counterPosition == numCounters || (counterPosition == numCounters - 1 && i == end))) {
186 throw ReaderException("recordPattern");
190 OneDReader::~OneDReader() {