Rabbit-R1/switch port/java/sources/com/google/zxing/oned/CodaBarReader.java
2024-05-21 17:08:36 -04:00

253 lines
8.5 KiB
Java

package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.DecodeHintType;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.ResultPoint;
import com.google.zxing.common.BitArray;
import java.util.Arrays;
import java.util.Map;
/* loaded from: classes3.dex */
public final class CodaBarReader extends OneDReader {
private static final float MAX_ACCEPTABLE = 2.0f;
private static final int MIN_CHARACTER_LENGTH = 3;
private static final float PADDING = 1.5f;
private static final String ALPHABET_STRING = "0123456789-$:/.+ABCD";
static final char[] ALPHABET = ALPHABET_STRING.toCharArray();
static final int[] CHARACTER_ENCODINGS = {3, 6, 9, 96, 18, 66, 33, 36, 48, 72, 12, 24, 69, 81, 84, 21, 26, 41, 11, 14};
private static final char[] STARTEND_ENCODING = {'A', 'B', 'C', 'D'};
private final StringBuilder decodeRowResult = new StringBuilder(20);
private int[] counters = new int[80];
private int counterLength = 0;
@Override // com.google.zxing.oned.OneDReader
public Result decodeRow(int i, BitArray bitArray, Map<DecodeHintType, ?> map) throws NotFoundException {
int i2;
Arrays.fill(this.counters, 0);
setCounters(bitArray);
int findStartPattern = findStartPattern();
this.decodeRowResult.setLength(0);
int i3 = findStartPattern;
while (true) {
int narrowWidePattern = toNarrowWidePattern(i3);
if (narrowWidePattern == -1) {
throw NotFoundException.getNotFoundInstance();
}
this.decodeRowResult.append((char) narrowWidePattern);
i2 = i3 + 8;
if ((this.decodeRowResult.length() <= 1 || !arrayContains(STARTEND_ENCODING, ALPHABET[narrowWidePattern])) && i2 < this.counterLength) {
i3 = i2;
}
}
int i4 = i3 + 7;
int i5 = this.counters[i4];
int i6 = 0;
for (int i7 = -8; i7 < -1; i7++) {
i6 += this.counters[i2 + i7];
}
if (i2 < this.counterLength && i5 < i6 / 2) {
throw NotFoundException.getNotFoundInstance();
}
validatePattern(findStartPattern);
for (int i8 = 0; i8 < this.decodeRowResult.length(); i8++) {
StringBuilder sb = this.decodeRowResult;
sb.setCharAt(i8, ALPHABET[sb.charAt(i8)]);
}
char charAt = this.decodeRowResult.charAt(0);
char[] cArr = STARTEND_ENCODING;
if (!arrayContains(cArr, charAt)) {
throw NotFoundException.getNotFoundInstance();
}
StringBuilder sb2 = this.decodeRowResult;
if (!arrayContains(cArr, sb2.charAt(sb2.length() - 1))) {
throw NotFoundException.getNotFoundInstance();
}
if (this.decodeRowResult.length() <= 3) {
throw NotFoundException.getNotFoundInstance();
}
if (map == null || !map.containsKey(DecodeHintType.RETURN_CODABAR_START_END)) {
StringBuilder sb3 = this.decodeRowResult;
sb3.deleteCharAt(sb3.length() - 1);
this.decodeRowResult.deleteCharAt(0);
}
int i9 = 0;
for (int i10 = 0; i10 < findStartPattern; i10++) {
i9 += this.counters[i10];
}
float f = i9;
while (findStartPattern < i4) {
i9 += this.counters[findStartPattern];
findStartPattern++;
}
float f2 = i;
return new Result(this.decodeRowResult.toString(), null, new ResultPoint[]{new ResultPoint(f, f2), new ResultPoint(i9, f2)}, BarcodeFormat.CODABAR);
}
private void validatePattern(int i) throws NotFoundException {
int i2 = 0;
int[] iArr = {0, 0, 0, 0};
int[] iArr2 = {0, 0, 0, 0};
int length = this.decodeRowResult.length() - 1;
int i3 = i;
int i4 = 0;
while (true) {
int i5 = CHARACTER_ENCODINGS[this.decodeRowResult.charAt(i4)];
for (int i6 = 6; i6 >= 0; i6--) {
int i7 = (i6 & 1) + ((i5 & 1) << 1);
iArr[i7] = iArr[i7] + this.counters[i3 + i6];
iArr2[i7] = iArr2[i7] + 1;
i5 >>= 1;
}
if (i4 >= length) {
break;
}
i3 += 8;
i4++;
}
float[] fArr = new float[4];
float[] fArr2 = new float[4];
for (int i8 = 0; i8 < 2; i8++) {
fArr2[i8] = 0.0f;
int i9 = i8 + 2;
int i10 = iArr[i9];
int i11 = iArr2[i9];
float f = ((iArr[i8] / iArr2[i8]) + (i10 / i11)) / 2.0f;
fArr2[i9] = f;
fArr[i8] = f;
fArr[i9] = ((i10 * 2.0f) + PADDING) / i11;
}
loop3: while (true) {
int i12 = CHARACTER_ENCODINGS[this.decodeRowResult.charAt(i2)];
for (int i13 = 6; i13 >= 0; i13--) {
int i14 = (i13 & 1) + ((i12 & 1) << 1);
float f2 = this.counters[i + i13];
if (f2 < fArr2[i14] || f2 > fArr[i14]) {
break loop3;
}
i12 >>= 1;
}
if (i2 >= length) {
return;
}
i += 8;
i2++;
}
throw NotFoundException.getNotFoundInstance();
}
private void setCounters(BitArray bitArray) throws NotFoundException {
int i = 0;
this.counterLength = 0;
int nextUnset = bitArray.getNextUnset(0);
int size = bitArray.getSize();
if (nextUnset >= size) {
throw NotFoundException.getNotFoundInstance();
}
boolean z = true;
while (nextUnset < size) {
if (bitArray.get(nextUnset) != z) {
i++;
} else {
counterAppend(i);
z = !z;
i = 1;
}
nextUnset++;
}
counterAppend(i);
}
private void counterAppend(int i) {
int[] iArr = this.counters;
int i2 = this.counterLength;
iArr[i2] = i;
int i3 = i2 + 1;
this.counterLength = i3;
if (i3 >= iArr.length) {
int[] iArr2 = new int[i3 << 1];
System.arraycopy(iArr, 0, iArr2, 0, i3);
this.counters = iArr2;
}
}
private int findStartPattern() throws NotFoundException {
for (int i = 1; i < this.counterLength; i += 2) {
int narrowWidePattern = toNarrowWidePattern(i);
if (narrowWidePattern != -1 && arrayContains(STARTEND_ENCODING, ALPHABET[narrowWidePattern])) {
int i2 = 0;
for (int i3 = i; i3 < i + 7; i3++) {
i2 += this.counters[i3];
}
if (i == 1 || this.counters[i - 1] >= i2 / 2) {
return i;
}
}
}
throw NotFoundException.getNotFoundInstance();
}
/* JADX INFO: Access modifiers changed from: package-private */
public static boolean arrayContains(char[] cArr, char c) {
if (cArr != null) {
for (char c2 : cArr) {
if (c2 == c) {
return true;
}
}
}
return false;
}
private int toNarrowWidePattern(int i) {
int i2 = i + 7;
if (i2 >= this.counterLength) {
return -1;
}
int[] iArr = this.counters;
int i3 = Integer.MAX_VALUE;
int i4 = 0;
int i5 = Integer.MAX_VALUE;
int i6 = 0;
for (int i7 = i; i7 < i2; i7 += 2) {
int i8 = iArr[i7];
if (i8 < i5) {
i5 = i8;
}
if (i8 > i6) {
i6 = i8;
}
}
int i9 = (i5 + i6) / 2;
int i10 = 0;
for (int i11 = i + 1; i11 < i2; i11 += 2) {
int i12 = iArr[i11];
if (i12 < i3) {
i3 = i12;
}
if (i12 > i10) {
i10 = i12;
}
}
int i13 = (i3 + i10) / 2;
int i14 = 128;
int i15 = 0;
for (int i16 = 0; i16 < 7; i16++) {
i14 >>= 1;
if (iArr[i + i16] > ((i16 & 1) == 0 ? i9 : i13)) {
i15 |= i14;
}
}
while (true) {
int[] iArr2 = CHARACTER_ENCODINGS;
if (i4 >= iArr2.length) {
return -1;
}
if (iArr2[i4] == i15) {
return i4;
}
i4++;
}
}
}