本地雙錄
更新時間:
通過閱讀本文,您可以快速了解 IDRS Android SDK 本地雙錄的相關功能及調(diào)用方法。
創(chuàng)建項目工程
打開 Android Studio IDE,用 Android Studio 創(chuàng)建一個項目工程。
說明
Android Studio 下載地址請參見 Android Studio 下載文件歸檔。
引入 SDK
在 build.gradle 中添加依賴。
dependencies {
implementation ('com.mpaas.android:idrs-process:cp_change_23596.25')//本地雙錄能力庫
implementation 'com.aliyun.openservices:aliyun-log-android-sdk:2.6.2'//日志庫
implementation 'com.squareup.okhttp3:okhttp:3.11.0'//網(wǎng)絡庫
implementation 'com.squareup.okhttp3:logging-interceptor:3.5.0'//網(wǎng)絡庫
implementation "com.alibaba:fastjson:1.2.83"http://json庫
}
ProcessSDK 功能
初始化 ProcessSDK
初始化方法僅對 ProcessSDK 初始化所在的 activity 進行前臺監(jiān)聽,因此為了精確地計算使用時長,您可以在 Application onCreate 中調(diào)用初始化方法。
public class ApplicationDemo extends Application {
@Override
public void onCreate() {
super.onCreate();
//****
IdrsAppLifecycle.init(this);
//****
}
}
在使用流程場景中初始化 ProcessSDK 的方法如下:
ProcessInitConfig config = new ProcessInitConfig();
config.processId = processId;
/**
* 此處以把 AccessKey 和 AccessKeySecret 保存在環(huán)境變量為例說明。您也可以根據(jù)業(yè)務需要,保存到配置文件里
* 強烈建議不要把 AccessKey 和 AccessKeySecret 保存到代碼里,會存在密鑰泄漏風險
*/
String appId = intent.getConfigStringExtra("appid"); // appId,在控制臺申請的,和包名綁定
String ak = intent.getConfigStringExtra("securityAK");
String sk = intent.getConfigStringExtra("securitySK");
config.appId = appId;
config.ak = ak;
config.sk = sk;
config.uid = "test123"; // 賬戶系統(tǒng)的uid
config.fileDir = getExternalCacheDir().getAbsolutePath(); // 可選,用于指定緩存目錄
File file = new File(config.fileDir);
if (!file.exists()) {
file.mkdirs();
}
ProcessSdk.getInstance().init(RecorderActivity.this, config, new ProcessSdkCallback())
開啟攝像頭
MPCameraView cameraView = mProcessSdk.startCamera(this, new InitCallback() {
@Override
public void initSuccess(int i, int i1) {
LogUtils.getInstance().debug(TAG, "startCamera success");
}
});
// 添加到布局中
cameraParent.addView(mMPCameraView, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
切換攝像頭
mProcessSdk.switchCamera();
開始流程
mProcessSdk.startFlow();
開始下一個檢測項
mProcessSdk.startNextDetection();
上一章
mProcessSdk.executePrePhases();
重新檢測當前項
mProcessSdk.repeatCurrentDetection();
重新播放當前章節(jié)
mProcessSdk.retryCurrenPhases();
暫停/恢復播放
mProcessSdk.flowPause();//播放暫停
mProcessSdk.flowResume();//恢復播放
清空默認路徑下的視頻及 meta/result 文件
mProcessSdk.clearCache();
銷毀
請及時使用銷毀、否則可能會影響下次初始化。
mProcessSdk.release();
ProcessSDK 回調(diào)
// 流程拉取回調(diào),用于更新流程中的配置信息,包括名稱、角色等
@Override
public String onEditFlowJson(String json) {
Log.i(TAG, "json: " + json);
try {
JSONObject jsonObject = new JSONObject(json);
JSONObject dataObject = jsonObject.optJSONObject("Data");
if (dataObject != null) {
String contentStr = dataObject.optString("Content");
if (contentStr != null) {
JSONObject contentJsonO = new JSONObject(contentStr);
JSONArray roleJsonArray = contentJsonO.optJSONArray("roles");
if (roleJsonArray != null) {
int size = roleJsonArray.length();
for (int index = size - 1; index >= 0; index--) {
roleJsonArray.remove(index);
}
List<RoleBean> roleBeanList = MainActivity.rolesCache;
for (RoleBean roleBean : roleBeanList) {
JSONObject roleItem = new JSONObject();
roleItem.put("id", roleBean.id);
roleItem.put("photo", roleBean.base64);
roleJsonArray.put(roleItem);
}
}
JSONArray phasesJsonArray = contentJsonO.optJSONArray("phases");
if (phasesJsonArray != null) {
for (int index = 0; index < phasesJsonArray.length(); index++) {
JSONObject itemJson = phasesJsonArray.optJSONObject(index);
if (itemJson != null) {
JSONObject voiceJson = itemJson.optJSONObject("voice");
if (voiceJson != null) {
JSONObject ttsJSON = voiceJson.optJSONObject("tts");
if (ttsJSON != null) {
String ttsText = ttsJSON.optString("ttsText");
if (!TextUtils.isEmpty(ttsText)) {
// 示例,根據(jù)后臺實際配置
ttsText = ttsText.replaceAll("#twoName#", "");
ttsText = ttsText.replaceAll("#oneName#",
"");
ttsJSON.put("ttsText", ttsText);
}
}
}
}
}
}
dataObject.put("Content", contentJsonO.toString());
}
}
return jsonObject.toString();
} catch (Exception e) {
}
return null;
}
// 檢測結(jié)果回調(diào)
@Override
public void onDetectionFail(AllDataBean.PhasesBean.DetectionsBean currenDetection) {
Log.d("mmm", currenDetection.getId() + "/" + "超時");
setSkipVISIBLE();
switch (currenDetection.getId()) {
case "id_card_recognize":
setCompleteViewError("識別身份證超時");
break;
case "sign_action_recognize":
setCompleteViewError("識別簽字動作超時");
break;
case "sign_classify_recognize":
setCompleteViewError("識別手寫簽名超時");
break;
case "speech_word_detect":
setCompleteViewError("識別激活詞超時");
break;
}
}
// 章節(jié)完成
@Override
public void onPhaseCompleted(AllDataBean.PhasesBean currenPhases) {
Log.d("mmm", currenPhases.getId() + "/" + "章節(jié)完成");
}
// 章節(jié)開始
@Override
public void onPhaseStarted(final AllDataBean.PhasesBean currenPhases) {
setCompleteViewINVISIBLE();
clearSurface();
setSkipINVISIBLE();
runOnUiThread(new Runnable() {
@Override
public void run() {
text.setText(currenPhases.getVoice().tts.ttsText);
}
});
}
// 整體流程完成
@Override
public void onFlowCompleted(List<UploadFileBean> allResultFiles) {
Log.d("mmm", "最后一個階段");
runOnUiThread(new Runnable() {
@Override
public void run() {
mLoadingView.setVisibility(View.GONE);
findViewById(R.id.reupload).setVisibility(View.GONE);
finishFlow();
Toast.makeText(RecorderActivity.this, "流程結(jié)束", Toast.LENGTH_SHORT).show();
finish();
}
});
}
// 錯誤回調(diào),錯誤碼如下:
@Override
public void onError(final int code, final String msg) {
LogUtils.getInstance().debug("mmmerror", "error: " + code + " " + msg);
runOnUiThread(new Runnable() {
@Override
public void run() {
mLoadingView.setVisibility(View.GONE);
if (code == ProcessSdk.ERROR_TYPE_UPLOAD) { // 結(jié)果上傳失敗
findViewById(R.id.reupload).setVisibility(View.VISIBLE);
} else if (code == ProcessSdk.ERROR_TYPE_IDRS // idrs初始化失敗
|| code == ProcessSdk.ERROR_TYPE_PROCESS_CONTENT // 流程內(nèi)容獲取失敗
|| code == ProcessSdk.ERROR_TYPE_RECORD_ERROR) { // 錄制失敗
disableAllBtn();
}
errorTextV.setText(errorTextV.getText() + "\n 錯誤:" + code + " " + msg);
}
});
}
// 結(jié)果上傳開始
@Override
public void onUploadStart() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLoadingView.setVisibility(View.VISIBLE);
errorTextV.setText(errorTextV.getText() + "\n 開始上傳");
}
});
}
// 音頻文件下載回調(diào)
@Override
public void onAudioDownloadProcess(final int process, final int index, final int size) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mDownloadBeforeText == null) {
mDownloadBeforeText = errorTextV.getText().toString();
}
errorTextV.setText(mDownloadBeforeText + "\n音頻下載進度:" + process + "% " + (index + 1) + "/" + size);
}
});
}
// 檢測成功回調(diào)
@Override
public boolean onDetectionSuccess(AllDataBean.PhasesBean.DetectionsBean currenDetection, final String result) {
Log.d("mmm檢測結(jié)果", currenDetection.getId() + "/" + result);
switch (currenDetection.getId()) {
case "id_card_recognize":
setCompleteView(result);
break;
case "sign_action_recognize":
setCompleteView("識別手寫簽字");
break;
case "sign_classify_recognize":
setCompleteView("識別手寫簽名");
break;
case "speech_word_detect":
setCompleteView(result);
break;
case "static_hand_recognize":
if (!TextUtils.isEmpty(result)) {
if (result.contains("5")) {
runOnUiThread(new Runnable() {
@Override
public void run() {
errorTextV.setText(errorTextV.getText() + "\n 手勢結(jié)果:" + result);
clearSurface();
}
});
return true;
}
}
break;
}
return false;
}
// 開始檢測回調(diào)
@Override
public void onStartDetection(AllDataBean.PhasesBean.DetectionsBean currenDetection) {
// Log.d("mmm開始檢測", currenDetection.getId());
setCompleteViewINVISIBLE();
setSkipINVISIBLE();
setErrorINVISIBLE();
clearSurface();
mCurrenDetection = currenDetection;
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mCurrenDetection != null) {
errorTextV.setText(errorTextV.getText() + "\n 開始檢測:" + mCurrenDetection.getId());
}
}
});
}
// 手勢實時結(jié)果
@Override
public void onHandDetectUi(ArrayList<HandDetectionResult> results, int angle, int degree, boolean isstatic) {
if (results != null) {
Facemanager.getInstance().drawLines(results, angle, degree, isstatic);
}
}
// 人臉實時結(jié)果
@Override
public void onFaceDetectUi(DectetResult[] dectetResults) {
Facemanager.getInstance().drawRect(dectetResults);
}
// 人臉實時結(jié)果
@Override
public void onFaceRecognizeUi(ArrayList<DectetResult> dectetResults) {
Facemanager.getInstance().drawResult(dectetResults, angle, degree, isAlive);
}
// 活體實時結(jié)果
@Override
public void onFaceLiveStatusChange(boolean isError, String message) {
Log.d("mmmcahgnge", isError + "/" + message);
isAlive = !isError;
}
@Override
public void onFaceNumChange(boolean isError, String message) {
Log.d("mmmcahgnge", isError + "/" + message);
//if (isError) {
// setErrorVISIBLE(message);
//} else {
// setErrorINVISIBLE();
//}
}
// tts識別過程回調(diào)
@Override
public void onTTSProcess(final String text, final int curIndex, final String currentText) {
runOnUiThread(new Runnable() {
@Override
public void run() {
int index = curIndex;
LogUtils.getInstance().debug(TAG, "ttsprocess: " + index);
String textAll = text;
if (!TextUtils.isEmpty(textAll)) {
SpannableString spannableString = new SpannableString(textAll);
if (index >= textAll.length() - 1) {
index = textAll.length();
}
spannableString.setSpan(new ForegroundColorSpan(Color.RED), 0, index, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
if (index < textAll.length() - 1) {
spannableString.setSpan(new ForegroundColorSpan(Color.WHITE), index + 1,
textAll.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
LogUtils.getInstance().debug(TAG, "ttsprocess: " + index + " " + spannableString.toString());
RecorderActivity.this.text.setText(spannableString);
}
}
});
}
// idrs sdk初始化成功回調(diào)
@Override
public void onInitSuccess() {
runOnUiThread(new Runnable() {
@Override
public void run() {
errorTextV.setText(errorTextV.getText() + "\n 初始化成功");
}
});
}
混淆配置
ProcessSDK 中的混淆配置如下:
-dontoptimize
-keep class com.mpaas.idrs.** { *; }
-keep class com.mpaas.idrs.process.** { *; }
## alinn
-keep class com.taobao.android.alinn.** { *; }
-keep class com.taobao.android.alinnkit.net.**{*;}
-keep class com.taobao.android.alinnkit.core.**{*;}
-keep class com.taobao.android.alinnkit.entity.**{*;}
-keep class com.taobao.android.alinnkit.json.**{*;}
## ocr
-keep class com.ant.phone.xmedia.**{*;}
-keep class com.alipay.xmedia.cache.**{*;}
-keep class com.alipay.xmedia.common.**{*;}
-keep class com.alipay.xmedia.gles.**{*;}
-keep class com.alipay.xmedia.serviceapi.**{*;}
-keep class com.alipay.xmedia.video.**{*;}
## 激活詞
-keep class com.alibaba.idst.nui.**{*;}
-keep class com.aliyun.sls.android.producer.* { *; }
-keep interface com.aliyun.sls.android.producer.* { *; }
-keep class com.mpaas.idrs.** { *;}
## alinn
-keep class com.taobao.** { *; }
## ocr
-keep class com.ant.phone.xmedia.**{*;}
-keep class com.alipay.xmedia.**{*;}
-keep class com.mpaas.ocr.** {*;}
-keep class com.mpaas.**{*;}
-keep class com.alibaba.** {*;}
-keep class okhttp3.** {*;}
-keep class xnn.** {*;}
-ignorewarnings
文檔內(nèi)容是否對您有幫助?