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

228 lines
9.6 KiB
Java

package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.ChecksumException;
import com.google.zxing.DecodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.NotFoundException;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.ResultMetadataType;
import com.google.zxing.ResultPoint;
import com.google.zxing.ResultPointCallback;
import com.google.zxing.common.BitArray;
import java.util.Arrays;
import java.util.Map;
/* loaded from: classes3.dex */
public abstract class UPCEANReader extends OneDReader {
static final int[][] L_AND_G_PATTERNS;
static final int[][] L_PATTERNS;
private static final float MAX_AVG_VARIANCE = 0.48f;
private static final float MAX_INDIVIDUAL_VARIANCE = 0.7f;
static final int[] START_END_PATTERN = {1, 1, 1};
static final int[] MIDDLE_PATTERN = {1, 1, 1, 1, 1};
static final int[] END_PATTERN = {1, 1, 1, 1, 1, 1};
private final StringBuilder decodeRowStringBuffer = new StringBuilder(20);
private final UPCEANExtensionSupport extensionReader = new UPCEANExtensionSupport();
private final EANManufacturerOrgSupport eanManSupport = new EANManufacturerOrgSupport();
/* JADX INFO: Access modifiers changed from: protected */
public abstract int decodeMiddle(BitArray bitArray, int[] iArr, StringBuilder sb) throws NotFoundException;
abstract BarcodeFormat getBarcodeFormat();
static {
int[][] iArr = {new int[]{3, 2, 1, 1}, new int[]{2, 2, 2, 1}, new int[]{2, 1, 2, 2}, new int[]{1, 4, 1, 1}, new int[]{1, 1, 3, 2}, new int[]{1, 2, 3, 1}, new int[]{1, 1, 1, 4}, new int[]{1, 3, 1, 2}, new int[]{1, 2, 1, 3}, new int[]{3, 1, 1, 2}};
L_PATTERNS = iArr;
int[][] iArr2 = new int[20];
L_AND_G_PATTERNS = iArr2;
System.arraycopy(iArr, 0, iArr2, 0, 10);
for (int i = 10; i < 20; i++) {
int[] iArr3 = L_PATTERNS[i - 10];
int[] iArr4 = new int[iArr3.length];
for (int i2 = 0; i2 < iArr3.length; i2++) {
iArr4[i2] = iArr3[(iArr3.length - i2) - 1];
}
L_AND_G_PATTERNS[i] = iArr4;
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public static int[] findStartGuardPattern(BitArray bitArray) throws NotFoundException {
int[] iArr = new int[START_END_PATTERN.length];
int[] iArr2 = null;
boolean z = false;
int i = 0;
while (!z) {
int[] iArr3 = START_END_PATTERN;
Arrays.fill(iArr, 0, iArr3.length, 0);
iArr2 = findGuardPattern(bitArray, i, false, iArr3, iArr);
int i2 = iArr2[0];
int i3 = iArr2[1];
int i4 = i2 - (i3 - i2);
if (i4 >= 0) {
z = bitArray.isRange(i4, i2, false);
}
i = i3;
}
return iArr2;
}
@Override // com.google.zxing.oned.OneDReader
public Result decodeRow(int i, BitArray bitArray, Map<DecodeHintType, ?> map) throws NotFoundException, ChecksumException, FormatException {
return decodeRow(i, bitArray, findStartGuardPattern(bitArray), map);
}
public Result decodeRow(int i, BitArray bitArray, int[] iArr, Map<DecodeHintType, ?> map) throws NotFoundException, ChecksumException, FormatException {
int i2;
String lookupCountryIdentifier;
ResultPointCallback resultPointCallback = map == null ? null : (ResultPointCallback) map.get(DecodeHintType.NEED_RESULT_POINT_CALLBACK);
if (resultPointCallback != null) {
resultPointCallback.foundPossibleResultPoint(new ResultPoint((iArr[0] + iArr[1]) / 2.0f, i));
}
StringBuilder sb = this.decodeRowStringBuffer;
sb.setLength(0);
int decodeMiddle = decodeMiddle(bitArray, iArr, sb);
if (resultPointCallback != null) {
resultPointCallback.foundPossibleResultPoint(new ResultPoint(decodeMiddle, i));
}
int[] decodeEnd = decodeEnd(bitArray, decodeMiddle);
if (resultPointCallback != null) {
resultPointCallback.foundPossibleResultPoint(new ResultPoint((decodeEnd[0] + decodeEnd[1]) / 2.0f, i));
}
int i3 = decodeEnd[1];
int i4 = (i3 - decodeEnd[0]) + i3;
if (i4 >= bitArray.getSize() || !bitArray.isRange(i3, i4, false)) {
throw NotFoundException.getNotFoundInstance();
}
String sb2 = sb.toString();
if (sb2.length() < 8) {
throw FormatException.getFormatInstance();
}
if (!checkChecksum(sb2)) {
throw ChecksumException.getChecksumInstance();
}
BarcodeFormat barcodeFormat = getBarcodeFormat();
float f = i;
Result result = new Result(sb2, null, new ResultPoint[]{new ResultPoint((iArr[1] + iArr[0]) / 2.0f, f), new ResultPoint((decodeEnd[1] + decodeEnd[0]) / 2.0f, f)}, barcodeFormat);
try {
Result decodeRow = this.extensionReader.decodeRow(i, bitArray, decodeEnd[1]);
result.putMetadata(ResultMetadataType.UPC_EAN_EXTENSION, decodeRow.getText());
result.putAllMetadata(decodeRow.getResultMetadata());
result.addResultPoints(decodeRow.getResultPoints());
i2 = decodeRow.getText().length();
} catch (ReaderException unused) {
i2 = 0;
}
int[] iArr2 = map != null ? (int[]) map.get(DecodeHintType.ALLOWED_EAN_EXTENSIONS) : null;
if (iArr2 != null) {
for (int i5 : iArr2) {
if (i2 != i5) {
}
}
throw NotFoundException.getNotFoundInstance();
}
if ((barcodeFormat == BarcodeFormat.EAN_13 || barcodeFormat == BarcodeFormat.UPC_A) && (lookupCountryIdentifier = this.eanManSupport.lookupCountryIdentifier(sb2)) != null) {
result.putMetadata(ResultMetadataType.POSSIBLE_COUNTRY, lookupCountryIdentifier);
}
return result;
}
/* JADX INFO: Access modifiers changed from: package-private */
public boolean checkChecksum(String str) throws FormatException {
return checkStandardUPCEANChecksum(str);
}
/* JADX INFO: Access modifiers changed from: package-private */
public static boolean checkStandardUPCEANChecksum(CharSequence charSequence) throws FormatException {
int length = charSequence.length();
if (length == 0) {
return false;
}
int i = length - 1;
return getStandardUPCEANChecksum(charSequence.subSequence(0, i)) == Character.digit(charSequence.charAt(i), 10);
}
/* JADX INFO: Access modifiers changed from: package-private */
public static int getStandardUPCEANChecksum(CharSequence charSequence) throws FormatException {
int length = charSequence.length();
int i = 0;
for (int i2 = length - 1; i2 >= 0; i2 -= 2) {
int charAt = charSequence.charAt(i2) - '0';
if (charAt < 0 || charAt > 9) {
throw FormatException.getFormatInstance();
}
i += charAt;
}
int i3 = i * 3;
for (int i4 = length - 2; i4 >= 0; i4 -= 2) {
int charAt2 = charSequence.charAt(i4) - '0';
if (charAt2 < 0 || charAt2 > 9) {
throw FormatException.getFormatInstance();
}
i3 += charAt2;
}
return (1000 - i3) % 10;
}
int[] decodeEnd(BitArray bitArray, int i) throws NotFoundException {
return findGuardPattern(bitArray, i, false, START_END_PATTERN);
}
/* JADX INFO: Access modifiers changed from: package-private */
public static int[] findGuardPattern(BitArray bitArray, int i, boolean z, int[] iArr) throws NotFoundException {
return findGuardPattern(bitArray, i, z, iArr, new int[iArr.length]);
}
private static int[] findGuardPattern(BitArray bitArray, int i, boolean z, int[] iArr, int[] iArr2) throws NotFoundException {
int size = bitArray.getSize();
int nextUnset = z ? bitArray.getNextUnset(i) : bitArray.getNextSet(i);
int length = iArr.length;
boolean z2 = z;
int i2 = 0;
int i3 = nextUnset;
while (nextUnset < size) {
if (bitArray.get(nextUnset) != z2) {
iArr2[i2] = iArr2[i2] + 1;
} else {
if (i2 != length - 1) {
i2++;
} else {
if (patternMatchVariance(iArr2, iArr, 0.7f) < MAX_AVG_VARIANCE) {
return new int[]{i3, nextUnset};
}
i3 += iArr2[0] + iArr2[1];
int i4 = i2 - 1;
System.arraycopy(iArr2, 2, iArr2, 0, i4);
iArr2[i4] = 0;
iArr2[i2] = 0;
i2--;
}
iArr2[i2] = 1;
z2 = !z2;
}
nextUnset++;
}
throw NotFoundException.getNotFoundInstance();
}
/* JADX INFO: Access modifiers changed from: package-private */
public static int decodeDigit(BitArray bitArray, int[] iArr, int i, int[][] iArr2) throws NotFoundException {
recordPattern(bitArray, i, iArr);
int length = iArr2.length;
float f = MAX_AVG_VARIANCE;
int i2 = -1;
for (int i3 = 0; i3 < length; i3++) {
float patternMatchVariance = patternMatchVariance(iArr, iArr2[i3], 0.7f);
if (patternMatchVariance < f) {
i2 = i3;
f = patternMatchVariance;
}
}
if (i2 >= 0) {
return i2;
}
throw NotFoundException.getNotFoundInstance();
}
}