what-the-bank/sources/com/kofax/kmc/ken/engines/ocr/OcrEngine.java

320 lines
12 KiB
Java

package com.kofax.kmc.ken.engines.ocr;
import android.graphics.Bitmap;
import android.os.Environment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.kofax.kmc.ken.engines.ImageProcessor;
import com.kofax.kmc.ken.engines.data.Image;
import com.kofax.kmc.ken.engines.data.ImagePerfectionProfile;
import com.kofax.kmc.ken.engines.ocr.OcrParameters;
import com.kofax.kmc.ken.engines.ocr.OcrRegion;
import com.kofax.kmc.kut.utilities.IpLibUtil;
import com.kofax.kmc.kut.utilities.appstats.type.AppStatsOCRType;
import com.kofax.kmc.kut.utilities.error.ErrorInfo;
import com.kofax.kmc.kut.utilities.error.KmcException;
import com.kofax.kmc.kut.utilities.error.KmcRuntimeException;
import com.kofax.kmc.kut.utilities.error.NullPointerException;
import com.kofax.mobile.sdk._internal.k;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/* loaded from: classes3.dex */
public class OcrEngine {
private static boolean hE;
private static ArrayList<OcrWord> hz = new ArrayList<>();
private ImageProcessor bw;
private Image hA;
private AppStatsOCRType hD;
private ArrayList<OcrEventListener> hy = new ArrayList<>();
private String TAG = "OcrEngine";
ImagePerfectionProfile aP = new ImagePerfectionProfile();
private OcrResultData hB = null;
private OcrFailureData hC = null;
private void aa() {
}
public OcrEngine() {
a();
}
private void a() {
if (hE) {
return;
}
if (!IpLibUtil.isIpLicensed()) {
throw new KmcRuntimeException(ErrorInfo.KMC_EV_LICENSING);
}
hE = true;
}
public void performOCR(Image image, OcrParameters ocrParameters) throws KmcException {
a(image, "sourceImage");
a(ocrParameters, "parameters");
a(ocrParameters.getFont(), "OcrParameters font");
this.hA = image;
ImageProcessor imageProcessor = new ImageProcessor();
this.bw = imageProcessor;
imageProcessor.setProcessedImageRepresentation(Image.ImageRep.IMAGE_REP_NONE);
int i = AnonymousClass2.hG[ocrParameters.getFont().ordinal()];
if (i == 2) {
ab();
} else if (i == 3) {
ab();
} else if (i == 4) {
aa();
}
a(ocrParameters.getRegion());
this.bw.addImageOutEventListener(new ImageProcessor.ImageOutListener(this) { // from class: com.kofax.kmc.ken.engines.ocr.OcrEngine.1
final OcrEngine hF;
{
this.hF = this;
}
@Override // com.kofax.kmc.ken.engines.ImageProcessor.ImageOutListener
public void imageOut(ImageProcessor.ImageOutEvent imageOutEvent) {
ErrorInfo status = imageOutEvent.getStatus();
if (ErrorInfo.KMC_SUCCESS == status) {
k.c(this.hF.TAG, "Parsed Data = ".concat(String.valueOf(this.hF.f(imageOutEvent.getImage().getImageMetaData()))));
}
this.hF.c(status);
}
});
}
/* renamed from: com.kofax.kmc.ken.engines.ocr.OcrEngine$2, reason: invalid class name */
/* loaded from: classes3.dex */
static /* synthetic */ class AnonymousClass2 {
static final int[] hG;
static {
int[] iArr = new int[OcrParameters.Font.values().length];
hG = iArr;
try {
iArr[OcrParameters.Font.E13B_MICR.ordinal()] = 1;
} catch (NoSuchFieldError unused) {
}
try {
hG[OcrParameters.Font.DEFAULT.ordinal()] = 2;
} catch (NoSuchFieldError unused2) {
}
try {
hG[OcrParameters.Font.GENERIC_MACHINE_PRINT.ordinal()] = 3;
} catch (NoSuchFieldError unused3) {
}
try {
hG[OcrParameters.Font.FARRINGTON_CREDIT_CARD.ordinal()] = 4;
} catch (NoSuchFieldError unused4) {
}
}
}
private void a(OcrRegion ocrRegion) throws KmcException {
if (ocrRegion == null) {
this.hD = AppStatsOCRType.OCR_FULLPAGE;
}
if (ocrRegion != null) {
if (ocrRegion.getRegionType() != null) {
this.bw.processImage(a(ocrRegion, ocrRegion.getRegionType()));
this.hD = AppStatsOCRType.OCR_ZONAL;
return;
}
throw new KmcException(ErrorInfo.KMC_ED_NONEXISTENT_OCR_REGION_TYPE);
}
if (!s(this.hA)) {
this.bw.processImage(this.hA);
return;
}
OcrRegion ocrRegion2 = new OcrRegion();
ocrRegion2.setRegionType(OcrRegion.RegionType.REGION_PERCENT);
ocrRegion2.setRegionHeight(100);
ocrRegion2.setRegionWidth(100);
this.bw.processImage(a(ocrRegion2, ocrRegion2.getRegionType()));
}
private boolean s(Image image) {
return image.getImageRepresentation() == Image.ImageRep.IMAGE_REP_FILE && image.getImageFileRep() == Image.ImageFileRep.FILE_BUFFERED;
}
private Image a(OcrRegion ocrRegion, OcrRegion.RegionType regionType) {
int i = ocrRegion.hP;
int i2 = ocrRegion.hO;
int i3 = ocrRegion.hN;
int i4 = ocrRegion.hM;
Image ad = ad();
Bitmap t = ad != null ? t(ad) : null;
if (regionType == OcrRegion.RegionType.REGION_PERCENT) {
i = (int) (t.getHeight() * (i / 100.0d));
i2 = (int) (t.getWidth() * (i2 / 100.0d));
}
Image image = new Image(Bitmap.createBitmap(t, i3, i4, i2, i));
ad.imageClearBitmap();
t.recycle();
return image;
}
private Bitmap t(Image image) {
if (image.getImageRepresentation() == Image.ImageRep.IMAGE_REP_FILE) {
try {
if (image.getImageFileRep() == Image.ImageFileRep.FILE_STORED) {
if (image.imageReadFromFile() == ErrorInfo.KMC_SUCCESS) {
return image.getImageBitmap();
}
return null;
}
if (image.getImageFileRep() == Image.ImageFileRep.FILE_BUFFERED && image.imageReadFromFileBuffer() == ErrorInfo.KMC_SUCCESS) {
return image.getImageBitmap();
}
return null;
} catch (KmcException | KmcRuntimeException unused) {
return null;
}
}
if (image.getImageRepresentation() == Image.ImageRep.IMAGE_REP_BITMAP || image.getImageRepresentation() == Image.ImageRep.IMAGE_REP_BOTH) {
return image.getImageBitmap();
}
return null;
}
private void ab() {
this.aP.setIpOperations("_DeviceType_2_DoNoPageDetection__DoRecognizeTextMP__LoadInlineSetting_[CBinarize.Contrast_Slider_Pos.Int=4]__LoadInlineSetting_[CBinarize.Cleanup_Slider_Pos.Int=4]");
this.bw.setImagePerfectionProfile(this.aP);
}
/* JADX INFO: Access modifiers changed from: private */
public void c(ErrorInfo errorInfo) {
if (errorInfo == ErrorInfo.KMC_SUCCESS) {
this.hC = null;
OcrResultData ocrResultData = new OcrResultData();
this.hB = ocrResultData;
ocrResultData.resultCode = ErrorInfo.KMC_SUCCESS;
this.hB.imageID = this.hA.getImageID();
this.hB.getWords();
Iterator<OcrEventListener> it = this.hy.iterator();
while (it.hasNext()) {
it.next().ocrSucceeded(this.hB);
}
return;
}
this.hB = null;
OcrFailureData ocrFailureData = new OcrFailureData();
this.hC = ocrFailureData;
ocrFailureData.hH = errorInfo;
this.hC.imageID = this.hA.getImageID();
Iterator<OcrEventListener> it2 = this.hy.iterator();
while (it2.hasNext()) {
it2.next().ocrFailed(this.hC);
}
}
public void addOcrListener(OcrEventListener ocrEventListener) {
if (ocrEventListener == null) {
throw new NullPointerException("addOcrListener: listener parameter is null");
}
if (this.hy.contains(ocrEventListener)) {
return;
}
this.hy.add(ocrEventListener);
}
public void removeListener(OcrEventListener ocrEventListener) {
this.hy.remove(ocrEventListener);
}
private boolean ac() {
ArrayList<OcrWord> arrayList = hz;
return arrayList != null && arrayList.size() > 0;
}
/* JADX INFO: Access modifiers changed from: private */
public String f(String str) {
hz.clear();
StringBuilder sb = new StringBuilder();
try {
JSONArray jSONArray = new JSONObject(str).getJSONObject("Front Side").getJSONObject("Text Lines").getJSONArray("Lines");
for (int i = 0; i < jSONArray.length(); i++) {
JSONArray jSONArray2 = jSONArray.getJSONObject(i).getJSONArray("Words");
for (int i2 = 0; i2 < jSONArray2.length(); i2++) {
OcrWord ocrWord = new OcrWord();
JSONObject jSONObject = jSONArray2.getJSONObject(i2);
Iterator<String> keys = jSONObject.keys();
while (keys.hasNext()) {
String next = keys.next();
if (next.equalsIgnoreCase("Word")) {
String string = jSONObject.getString(next);
if (string != null) {
ocrWord.setText(string);
}
} else if (next.equalsIgnoreCase("TLx")) {
ocrWord.setX(jSONObject.getInt("TLx"));
} else if (next.equalsIgnoreCase("TLy")) {
ocrWord.setY(jSONObject.getInt("TLy"));
} else if (next.equalsIgnoreCase("BRy")) {
ocrWord.setBRy(jSONObject.getInt("BRy"));
} else if (next.equalsIgnoreCase("TRy")) {
ocrWord.setTRy(jSONObject.getInt("TRy"));
} else if (next.equalsIgnoreCase("TRx")) {
ocrWord.setTRx(jSONObject.getInt("TRx"));
} else if (next.equalsIgnoreCase("TLx")) {
ocrWord.setTRx(jSONObject.getInt("TLx"));
}
}
hz.add(ocrWord);
}
}
} catch (JSONException unused) {
}
return sb.toString();
}
private void a(Object obj, String str) {
if (obj == null) {
StringBuilder sb = new StringBuilder("'");
sb.append(str);
sb.append("' parameter is null");
throw new NullPointerException(sb.toString());
}
if ((!obj.getClass().getSimpleName().equals("Integer") || ((Integer) obj).intValue() >= 0) && (!obj.getClass().getSimpleName().equals("Float") || ((Float) obj).floatValue() >= BitmapDescriptorFactory.HUE_RED)) {
return;
}
ErrorInfo errorInfo = ErrorInfo.KMC_GN_PARAM_NEGATIVE;
StringBuilder sb2 = new StringBuilder("'");
sb2.append(str);
sb2.append("' parameter is negative");
errorInfo.setErrCause(sb2.toString());
throw new KmcRuntimeException(errorInfo);
}
private void e(Bitmap bitmap) {
try {
StringBuilder sb = new StringBuilder();
sb.append(Environment.getExternalStorageDirectory().getAbsolutePath());
sb.append("/Archana/");
String obj = sb.toString();
StringBuilder sb2 = new StringBuilder();
sb2.append(obj);
sb2.append("abcd.jpg");
FileOutputStream fileOutputStream = new FileOutputStream(sb2.toString());
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
k.e("Could not save", e.toString());
}
}
/* JADX INFO: Access modifiers changed from: protected */
public static ArrayList<OcrWord> ocrWords() {
return hz;
}
private Image ad() {
return this.hA;
}
}