思特奇智慧酒店云平台 v0.0.5版本的 MD5 值为:dedb172bb322639d79d7e0e26262fb5c
以下内容为反编译后的 OCR.java 源代码,内容仅作参考
package com.baidu.ocr.sdk;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;
import com.baidu.ocr.sdk.exception.OCRError;
import com.baidu.ocr.sdk.exception.SDKError;
import com.baidu.ocr.sdk.jni.JniInterface;
import com.baidu.ocr.sdk.model.AccessToken;
import com.baidu.ocr.sdk.model.BankCardParams;
import com.baidu.ocr.sdk.model.BankCardResult;
import com.baidu.ocr.sdk.model.GeneralBasicParams;
import com.baidu.ocr.sdk.model.GeneralParams;
import com.baidu.ocr.sdk.model.GeneralResult;
import com.baidu.ocr.sdk.model.IDCardParams;
import com.baidu.ocr.sdk.model.IDCardResult;
import com.baidu.ocr.sdk.model.OcrRequestParams;
import com.baidu.ocr.sdk.model.OcrResponseResult;
import com.baidu.ocr.sdk.utils.AccessTokenParser;
import com.baidu.ocr.sdk.utils.BankCardResultParser;
import com.baidu.ocr.sdk.utils.CrashReporterHandler;
import com.baidu.ocr.sdk.utils.DeviceUtil;
import com.baidu.ocr.sdk.utils.GeneralResultParser;
import com.baidu.ocr.sdk.utils.GeneralSimpleResultParser;
import com.baidu.ocr.sdk.utils.HttpUtil;
import com.baidu.ocr.sdk.utils.IDCardResultParser;
import com.baidu.ocr.sdk.utils.ImageUtil;
import com.baidu.ocr.sdk.utils.OcrResultParser;
import com.baidu.ocr.sdk.utils.Util;
import java.io.File;
public class OCR {
public static final int AUTHWITH_AKSK = 2;
public static final int AUTHWITH_LICENSE = 1;
public static final int AUTHWITH_NOTYET = 0;
public static final int AUTHWITH_TOKEN = 3;
public static final String BANK_CARD_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard?";
public static final String ID_CARD_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?";
public static final int IMAGE_MAX_HEIGHT = 1280;
public static final int IMAGE_MAX_WIDTH = 1280;
public static final String OCR_SDK_VERSION = "1_4_4";
public static final String PREFRENCE_AUTH_TYPE = "token_auth_type";
public static final String PREFRENCE_EXPIRETIME_KEY = "token_expire_time";
public static final String PREFRENCE_FILE_KEY = "com.baidu.ocr.sdk";
public static final String PREFRENCE_TOKENJSON_KEY = "token_json";
public static final String QUERY_TOKEN = "https://verify.baidubce.com/verify/1.0/token/sk?sdkVersion=1_4_4";
public static final String QUERY_TOKEN_BIN = "https://verify.baidubce.com/verify/1.0/token/bin?sdkVersion=1_4_4";
public static final String RECOGNIZE_ACCURATE_BASIC_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?";
public static final String RECOGNIZE_ACCURATE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate?";
public static final String RECOGNIZE_BUSINESS_CARD_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/business_card?";
public static final String RECOGNIZE_BUSINESS_LICENSE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/business_license?";
public static final String RECOGNIZE_CUSTOM = "https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise?";
public static final String RECOGNIZE_DRIVING_LICENSE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/driving_license?";
public static final String RECOGNIZE_GENERAL_BASIC_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?";
public static final String RECOGNIZE_GENERAL_ENHANCE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_enhanced?";
public static final String RECOGNIZE_GENERAL_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/general?";
public static final String RECOGNIZE_GENERAL_WEBIMAGE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/webimage?";
public static final String RECOGNIZE_HANDWRITING_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting?";
public static final String RECOGNIZE_LICENSE_PLATE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate?";
public static final String RECOGNIZE_LOTTERY_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/lottery?";
public static final String RECOGNIZE_NUMBERS_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/numbers?";
public static final String RECOGNIZE_PASSPORT_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/passport?";
public static final String RECOGNIZE_QRCODE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode?";
public static final String RECOGNIZE_RECEIPT_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/receipt?";
public static final String RECOGNIZE_VAT_INVOICE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice?";
public static final String RECOGNIZE_VEHICLE_LICENSE_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/vehicle_license?";
public static volatile OCR instance;
@SuppressLint({"StaticFieldLeak"})
public Context context;
public CrashReporterHandler crInst;
public AccessToken accessToken = null;
public int authStatus = 0;
public String ak = null;
public String sk = null;
public boolean isAutoCacheToken = false;
public String license = null;
public OCR(Context context) {
if (context != null) {
this.context = context;
}
}
private AccessToken getByCache() {
if (this.isAutoCacheToken) {
SharedPreferences sharedPreferences = this.context.getSharedPreferences("com.baidu.ocr.sdk", 0);
String string = sharedPreferences.getString(PREFRENCE_TOKENJSON_KEY, "");
int i = sharedPreferences.getInt(PREFRENCE_AUTH_TYPE, 0);
if (i != this.authStatus) {
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.clear();
edit.commit();
return null;
}
try {
AccessToken parse = new AccessTokenParser().parse(string);
parse.setExpireTime(sharedPreferences.getLong(PREFRENCE_EXPIRETIME_KEY, 0L));
this.authStatus = i;
return parse;
} catch (SDKError unused) {
return null;
}
}
return null;
}
public static OCR getInstance(Context context) {
if (instance == null) {
synchronized (OCR.class) {
if (instance == null) {
instance = new OCR(context);
}
}
}
return instance;
}
private void getToken(final OnResultListener onResultListener) {
if (isTokenInvalid()) {
if (this.authStatus == 2) {
initAccessTokenWithAkSk(new OnResultListener<AccessToken>() {
@Override
public void onError(OCRError oCRError) {
onResultListener.onError(oCRError);
}
@Override
public void onResult(AccessToken accessToken) {
OCR.this.setAccessToken(accessToken);
onResultListener.onResult(accessToken);
}
}, this.context, this.ak, this.sk);
}
if (this.authStatus == 1) {
initAccessToken(new OnResultListener<AccessToken>() {
@Override
public void onError(OCRError oCRError) {
onResultListener.onError(oCRError);
}
@Override
public void onResult(AccessToken accessToken) {
OCR.this.setAccessToken(accessToken);
onResultListener.onResult(accessToken);
}
}, this.context);
return;
}
return;
}
onResultListener.onResult(this.accessToken);
}
private void initAccessTokenImpl(OnResultListener<AccessToken> onResultListener, String str, Context context) {
byte[] initWithBinLic;
this.authStatus = 1;
init(context);
Throwable loadLibraryError = JniInterface.getLoadLibraryError();
if (loadLibraryError != null) {
onResultListener.onError(new SDKError(SDKError.ErrorCode.LOAD_JNI_LIBRARY_ERROR, "Load jni so library error", loadLibraryError));
return;
}
JniInterface jniInterface = new JniInterface();
try {
if (str == null) {
initWithBinLic = jniInterface.initWithBin(context, DeviceUtil.getDeviceInfo(context));
} else {
initWithBinLic = jniInterface.initWithBinLic(context, DeviceUtil.getDeviceInfo(context), str);
}
String encodeToString = Base64.encodeToString(initWithBinLic, 2);
AccessToken byCache = getByCache();
if (byCache != null) {
this.accessToken = byCache;
onResultListener.onResult(byCache);
return;
}
HttpUtil.getInstance().getAccessToken(onResultListener, QUERY_TOKEN_BIN, encodeToString);
} catch (OCRError e) {
onResultListener.onError(e);
}
}
private synchronized boolean isTokenInvalid() {
boolean z;
if (this.accessToken != null) {
z = this.accessToken.hasExpired();
}
return z;
}
private void recognizeLocation(final GeneralParams generalParams, final OnResultListener<GeneralResult> onResultListener, final String str) {
File imageFile = generalParams.getImageFile();
final File file = new File(this.context.getCacheDir(), String.valueOf(System.currentTimeMillis()));
ImageUtil.resize(imageFile.getAbsolutePath(), file.getAbsolutePath(), 1280, 1280);
generalParams.setImageFile(file);
final GeneralResultParser generalResultParser = new GeneralResultParser();
getToken(new OnResultListener() {
@Override
public void onError(OCRError oCRError) {
onResultListener.onError(oCRError);
}
@Override
public void onResult(Object obj) {
HttpUtil.getInstance().post(OCR.this.urlAppendCommonParams(str), generalParams, generalResultParser, new OnResultListener<GeneralResult>() {
@Override
public void onError(OCRError oCRError) {
file.delete();
OnResultListener onResultListener2 = onResultListener;
if (onResultListener2 != null) {
onResultListener2.onError(oCRError);
}
}
@Override
public void onResult(GeneralResult generalResult) {
file.delete();
OnResultListener onResultListener2 = onResultListener;
if (onResultListener2 != null) {
onResultListener2.onResult(generalResult);
}
}
});
}
});
}
private void recognizeNoLocation(final GeneralBasicParams generalBasicParams, final OnResultListener<GeneralResult> onResultListener, final String str) {
File imageFile = generalBasicParams.getImageFile();
final File file = new File(this.context.getCacheDir(), String.valueOf(System.currentTimeMillis()));
ImageUtil.resize(imageFile.getAbsolutePath(), file.getAbsolutePath(), 1280, 1280);
generalBasicParams.setImageFile(file);
final GeneralSimpleResultParser generalSimpleResultParser = new GeneralSimpleResultParser();
getToken(new OnResultListener() {
@Override
public void onError(OCRError oCRError) {
onResultListener.onError(oCRError);
}
@Override
public void onResult(Object obj) {
HttpUtil.getInstance().post(OCR.this.urlAppendCommonParams(str), generalBasicParams, generalSimpleResultParser, new OnResultListener<GeneralResult>() {
@Override
public void onError(OCRError oCRError) {
file.delete();
OnResultListener onResultListener2 = onResultListener;
if (onResultListener2 != null) {
onResultListener2.onError(oCRError);
}
}
@Override
public void onResult(GeneralResult generalResult) {
file.delete();
OnResultListener onResultListener2 = onResultListener;
if (onResultListener2 != null) {
onResultListener2.onResult(generalResult);
}
}
});
}
});
}
public String urlAppendCommonParams(String str) {
return str + "access_token=" + getAccessToken().getAccessToken() + "&aipSdk=Android&aipSdkVersion=" + OCR_SDK_VERSION + "&aipDevid=" + DeviceUtil.getDeviceId(this.context);
}
public synchronized AccessToken getAccessToken() {
return this.accessToken;
}
public String getLicense() {
String str;
JniInterface jniInterface = new JniInterface();
int i = this.authStatus;
if (i == 1) {
return jniInterface.getToken(this.context);
}
if (i == 2 && (str = this.license) != null) {
try {
byte[] decode = Base64.decode(str, 0);
return jniInterface.getTokenFromLicense(this.context, decode, decode.length);
} catch (Throwable unused) {
}
}
return null;
}
public void init(Context context) {
this.context = context;
this.crInst = CrashReporterHandler.init(context).addSourceClass(OCR.class);
try {
this.crInst.addSourceClass(Class.forName("com.baidu.ocr.ui.camera.CameraActivity"));
} catch (Throwable unused) {
}
HttpUtil.getInstance().init();
}
public void initAccessToken(OnResultListener<AccessToken> onResultListener, Context context) {
initAccessTokenImpl(onResultListener, null, context);
}
public void initAccessTokenWithAkSk(OnResultListener<AccessToken> onResultListener, Context context, String str, String str2) {
this.authStatus = 2;
this.ak = str;
this.sk = str2;
init(context);
AccessToken byCache = getByCache();
if (byCache != null) {
this.accessToken = byCache;
onResultListener.onResult(byCache);
setLicense(byCache.getLic());
return;
}
Throwable loadLibraryError = JniInterface.getLoadLibraryError();
if (loadLibraryError != null) {
onResultListener.onError(new SDKError(SDKError.ErrorCode.LOAD_JNI_LIBRARY_ERROR, "Load jni so library error", loadLibraryError));
return;
}
JniInterface jniInterface = new JniInterface();
String md5 = Util.md5(str2);
byte[] init = jniInterface.init(context, DeviceUtil.getDeviceInfo(context));
HttpUtil.getInstance().getAccessToken(onResultListener, QUERY_TOKEN, str + ";" + md5 + Base64.encodeToString(init, 2));
}
@Deprecated
public void initWithToken(Context context, AccessToken accessToken) {
init(context);
setAccessToken(accessToken);
}
public boolean isAutoCacheToken() {
return this.isAutoCacheToken;
}
public void recognizeAccurate(GeneralParams generalParams, OnResultListener<GeneralResult> onResultListener) {
recognizeLocation(generalParams, onResultListener, RECOGNIZE_ACCURATE_URL);
}
public void recognizeAccurateBasic(GeneralBasicParams generalBasicParams, OnResultListener<GeneralResult> onResultListener) {
recognizeNoLocation(generalBasicParams, onResultListener, RECOGNIZE_ACCURATE_BASIC_URL);
}
public void recognizeBankCard(final BankCardParams bankCardParams, final OnResultListener<BankCardResult> onResultListener) {
File imageFile = bankCardParams.getImageFile();
final File file = new File(this.context.getCacheDir(), String.valueOf(System.currentTimeMillis()));
ImageUtil.resize(imageFile.getAbsolutePath(), file.getAbsolutePath(), 1280, 1280);
bankCardParams.setImageFile(file);
final BankCardResultParser bankCardResultParser = new BankCardResultParser();
getToken(new OnResultListener() {
@Override
public void onError(OCRError oCRError) {
onResultListener.onError(oCRError);
}
@Override
public void onResult(Object obj) {
HttpUtil.getInstance().post(OCR.this.urlAppendCommonParams(OCR.BANK_CARD_URL), bankCardParams, bankCardResultParser, new OnResultListener<BankCardResult>() {
@Override
public void onError(OCRError oCRError) {
file.delete();
OnResultListener onResultListener2 = onResultListener;
if (onResultListener2 != null) {
onResultListener2.onError(oCRError);
}
}
@Override
public void onResult(BankCardResult bankCardResult) {
file.delete();
OnResultListener onResultListener2 = onResultListener;
if (onResultListener2 != null) {
onResultListener2.onResult(bankCardResult);
}
}
});
}
});
}
public void recognizeBusinessCard(OcrRequestParams ocrRequestParams, OnResultListener<OcrResponseResult> onResultListener) {
recognizeCommon(ocrRequestParams, onResultListener, RECOGNIZE_BUSINESS_CARD_URL);
}
public void recognizeBusinessLicense(OcrRequestParams ocrRequestParams, OnResultListener<OcrResponseResult> onResultListener) {
recognizeCommon(ocrRequestParams, onResultListener, RECOGNIZE_BUSINESS_LICENSE_URL);
}
public void recognizeCommon(final OcrRequestParams ocrRequestParams, final OnResultListener<OcrResponseResult> onResultListener, final String str) {
File imageFile = ocrRequestParams.getImageFile();
final File file = new File(this.context.getCacheDir(), String.valueOf(System.currentTimeMillis()));
ImageUtil.resize(imageFile.getAbsolutePath(), file.getAbsolutePath(), 1280, 1280);
ocrRequestParams.setImageFile(file);
final OcrResultParser ocrResultParser = new OcrResultParser();
getToken(new OnResultListener() {
@Override
public void onError(OCRError oCRError) {
onResultListener.onError(oCRError);
}
@Override
public void onResult(Object obj) {
HttpUtil.getInstance().post(OCR.this.urlAppendCommonParams(str), ocrRequestParams, ocrResultParser, new OnResultListener<OcrResponseResult>() {
@Override
public void onError(OCRError oCRError) {
file.delete();
OnResultListener onResultListener2 = onResultListener;
if (onResultListener2 != null) {
onResultListener2.onError(oCRError);
}
}
@Override
public void onResult(OcrResponseResult ocrResponseResult) {
file.delete();
OnResultListener onResultListener2 = onResultListener;
if (onResultListener2 != null) {
onResultListener2.onResult(ocrResponseResult);
}
}
});
}
});
}
public void recognizeCustom(OcrRequestParams ocrRequestParams, OnResultListener<OcrResponseResult> onResultListener) {
recognizeCommon(ocrRequestParams, onResultListener, RECOGNIZE_CUSTOM);
}
public void recognizeDrivingLicense(OcrRequestParams ocrRequestParams, OnResultListener<OcrResponseResult> onResultListener) {
recognizeCommon(ocrRequestParams, onResultListener, RECOGNIZE_DRIVING_LICENSE_URL);
}
public void recognizeGeneral(GeneralParams generalParams, OnResultListener<GeneralResult> onResultListener) {
recognizeLocation(generalParams, onResultListener, RECOGNIZE_GENERAL_URL);
}
public void recognizeGeneralBasic(GeneralBasicParams generalBasicParams, OnResultListener<GeneralResult> onResultListener) {
recognizeNoLocation(generalBasicParams, onResultListener, RECOGNIZE_GENERAL_BASIC_URL);
}
public void recognizeGeneralEnhanced(GeneralBasicParams generalBasicParams, OnResultListener<GeneralResult> onResultListener) {
recognizeNoLocation(generalBasicParams, onResultListener, RECOGNIZE_GENERAL_ENHANCE_URL);
}
public void recognizeHandwriting(OcrRequestParams ocrRequestParams, OnResultListener<OcrResponseResult> onResultListener) {
recognizeCommon(ocrRequestParams, onResultListener, RECOGNIZE_HANDWRITING_URL);
}
public void recognizeIDCard(final IDCardParams iDCardParams, final OnResultListener<IDCardResult> onResultListener) {
File imageFile = iDCardParams.getImageFile();
final File file = new File(this.context.getCacheDir(), String.valueOf(System.currentTimeMillis()));
ImageUtil.resize(imageFile.getAbsolutePath(), file.getAbsolutePath(), 1280, 1280, iDCardParams.getImageQuality());
iDCardParams.setImageFile(file);
final IDCardResultParser iDCardResultParser = new IDCardResultParser(iDCardParams.getIdCardSide());
getToken(new OnResultListener() {
@Override
public void onError(OCRError oCRError) {
onResultListener.onError(oCRError);
}
@Override
public void onResult(Object obj) {
HttpUtil.getInstance().post(OCR.this.urlAppendCommonParams(OCR.ID_CARD_URL), iDCardParams, iDCardResultParser, new OnResultListener<IDCardResult>() {
@Override
public void onError(OCRError oCRError) {
file.delete();
OnResultListener onResultListener2 = onResultListener;
if (onResultListener2 != null) {
onResultListener2.onError(oCRError);
}
}
@Override
public void onResult(IDCardResult iDCardResult) {
file.delete();
OnResultListener onResultListener2 = onResultListener;
if (onResultListener2 != null) {
onResultListener2.onResult(iDCardResult);
}
}
});
}
});
}
public void recognizeLicensePlate(OcrRequestParams ocrRequestParams, OnResultListener<OcrResponseResult> onResultListener) {
recognizeCommon(ocrRequestParams, onResultListener, RECOGNIZE_LICENSE_PLATE_URL);
}
public void recognizeLottery(OcrRequestParams ocrRequestParams, OnResultListener<OcrResponseResult> onResultListener) {
recognizeCommon(ocrRequestParams, onResultListener, RECOGNIZE_LOTTERY_URL);
}
public void recognizeNumbers(OcrRequestParams ocrRequestParams, OnResultListener<OcrResponseResult> onResultListener) {
recognizeCommon(ocrRequestParams, onResultListener, RECOGNIZE_NUMBERS_URL);
}
public void recognizePassport(OcrRequestParams ocrRequestParams, OnResultListener<OcrResponseResult> onResultListener) {
recognizeCommon(ocrRequestParams, onResultListener, RECOGNIZE_PASSPORT_URL);
}
public void recognizeQrcode(OcrRequestParams ocrRequestParams, OnResultListener<OcrResponseResult> onResultListener) {
recognizeCommon(ocrRequestParams, onResultListener, RECOGNIZE_QRCODE_URL);
}
public void recognizeReceipt(OcrRequestParams ocrRequestParams, OnResultListener<OcrResponseResult> onResultListener) {
recognizeCommon(ocrRequestParams, onResultListener, RECOGNIZE_RECEIPT_URL);
}
public void recognizeVatInvoice(OcrRequestParams ocrRequestParams, OnResultListener<OcrResponseResult> onResultListener) {
recognizeCommon(ocrRequestParams, onResultListener, RECOGNIZE_VAT_INVOICE_URL);
}
public void recognizeVehicleLicense(OcrRequestParams ocrRequestParams, OnResultListener<OcrResponseResult> onResultListener) {
recognizeCommon(ocrRequestParams, onResultListener, RECOGNIZE_VEHICLE_LICENSE_URL);
}
public void recognizeWebimage(GeneralBasicParams generalBasicParams, OnResultListener<GeneralResult> onResultListener) {
recognizeNoLocation(generalBasicParams, onResultListener, RECOGNIZE_GENERAL_WEBIMAGE_URL);
}
public void release() {
HttpUtil.getInstance().release();
this.crInst.release();
this.crInst = null;
this.context = null;
if (instance != null) {
instance = null;
}
}
public synchronized void setAccessToken(AccessToken accessToken) {
if (accessToken.getTokenJson() != null) {
SharedPreferences.Editor edit = this.context.getSharedPreferences("com.baidu.ocr.sdk", 0).edit();
edit.putString(PREFRENCE_TOKENJSON_KEY, accessToken.getTokenJson());
edit.putLong(PREFRENCE_EXPIRETIME_KEY, accessToken.getExpiresTime());
edit.putInt(PREFRENCE_AUTH_TYPE, this.authStatus);
edit.apply();
}
this.accessToken = accessToken;
}
public void setAutoCacheToken(boolean z) {
this.isAutoCacheToken = z;
}
public void setLicense(String str) {
this.license = str;
}
public void initAccessToken(OnResultListener<AccessToken> onResultListener, String str, Context context) {
initAccessTokenImpl(onResultListener, str, context);
}
}