我正在构建一个OCR Android应用程序,该应用程序在后台执行许多图像处理任务,需要一些时间才能完成。它执行的步骤如下:
- 捕获图像
- 向用户显示图像,提供重新捕获图像或继续的选项
- 显示已处理的图像,提供重新捕获图像或继续的选项。
- 提取文本
这些任务非常耗时,我想通过在上一个任务完成后立即启动下一个任务来减少一些时间,同时仅在用户单击继续按钮且任务尚未完成时才显示进度对话框"请稍候"。
我想知道这是否可能,如果是,我该如何实现这一目标?
以下是我用于 OCR 任务的代码:
private class OCRTask extends AsyncTask<Void, Void, String> {
ProgressDialog mProgressDialog;
public OCRTask(PreviewActivity activity) {
mProgressDialog = new ProgressDialog(activity);
}
@Override
protected String doInBackground(Void... params) {
String path = previewFilePath;
String ocrText;
OCR ocr = new OCR();
ocrText = ocr.OCRImage(path, PreviewActivity.this);
// Write the result to a txt file and store it in the same dir as the temp img
// find the last occurence of '/'
int p=previewFilePath.lastIndexOf("/");
// e is the string value after the last occurence of '/'
String e=previewFilePath.substring(p+1);
// split the string at the value of e to remove the it from the string and get the dir path
String[] a = previewFilePath.split(e);
String dirPath = a[0];
String fileString = dirPath + "ocrtext.txt";
File file = new File(fileString);
try {
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(ocrText);
bw.close();
System.out.println("done!!");
} catch (IOException i) {
i.printStackTrace();
}
new WordCorrect(fileString);
return ocrText;
}
@Override
protected void onPreExecute() {
// Set the progress dialog attributes
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setMessage("Extracting text...");
mProgressDialog.show();
}
@Override
protected void onPostExecute(String result) {
// dismiss the progress dialog
mProgressDialog.dismiss();
Intent i;
i = new Intent(PreviewActivity.this, ReceiptEditActivity.class);
// Pass the file path and text result to the receipt edit activity
i.putExtra(FILE_PATH, previewFilePath);
Log.e("OCR TEXT: ", result);
i.putExtra(OCR_TEXT, result);
// Start receipt edit activity
PreviewActivity.this.startActivityForResult(i, 111);
finish();
}
@Override
protected void onProgressUpdate(Void... values) {}
}
任何帮助或指导将不胜感激!
只需在活动或片段中选取一个布尔变量,例如
public static boolean isTaskRunning = false;
在 AsyncTask 的 onPreExecute(( 中,将其值更改为 true。
YourActivity.isTaskRunning = true;
我正在考虑您在活动类中采用了此变量。在 onPostExecute(字符串结果(中,将其值恢复为 false
YourActivity.isTaskRunning = false;
现在,在单击按钮时,检查此变量值,如果为真,则显示您的进度对话框,否则不显示。