【发布时间】:2020-02-16 04:56:35
【问题描述】:
我正在使用 2 个 EditText,一个用于标题,另一个用于正文。我通过文本识别器和标题从我的图像中获取正文文本,只需输入标题即可。但是当我调用文本识别器的函数然后保存数据时,我的标题文本显示出一种奇怪的行为,即使我输入标题,也会给我缺少标题的警告对话框。我不知道为什么使用文本识别器功能,标题编辑文本会向我显示缺少标题的警报对话框。这是我的代码
private boolean hasTitle() {
return !titleText.getText().toString().trim().isEmpty();
}
if (hasBody() || hasImage() && !hasTitle()) { // if user missing title
AlertDialogNoTitle(); // it should not enter here while i type the title but with recognizer it show alertdialogue. By Log, i am getting the correct text for the title which is not zero
// setResult(RESULT_CANCELED, data);
// finish();
} else if (hasTitle()) { // if they have title
data.putExtra("USER TITLE", titleText.getText().toString());
data.putExtra("USER TEXT", bodyText.getText().toString());
if (cardsNamesSingleString != null) {
data.putExtra("USER CARDS", cardsNamesSingleString);
Util.saveToInternalStorageCard(ArrayImageName, bitmaps);
}
setResult(RESULT_OK, data);
finish();
} else { // no entry
setResult(RESULT_CANCELED, data);
finish();
}
}
我的文本识别器仅用于正文
private void detect() {
//perform text detection here
//TODO 1. define TextRecognizer
TextRecognizer recognizer = new TextRecognizer.Builder(NoteActivity.this).build();
//TODO 2. Get bitmap from imageview
Bitmap bitmap = ((BitmapDrawable)frontCard.getDrawable()).getBitmap();
//TODO 3. get frame from bitmap
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
//TODO 4. get data from frame
SparseArray<TextBlock> sparseArray = recognizer.detect(frame);
//TODO 5. set data on textview
StringBuilder stringBuilder = new StringBuilder();
for(int i=0;i < sparseArray.size(); i++){
TextBlock tx = sparseArray.get(i);
stringBuilder.append(tx.getValue());
stringBuilder.append("\n");
}
bodyText.setText(stringBuilder.toString());
}
【问题讨论】:
-
我注意到你有 (hasBody() || hasImage() && !hasTitle())。 && 在 || 之前计算。
-
抱歉没听懂?你能解释一下吗?如果我没有运行文本识别,那么它工作正常并且不会询问我缺少的标题。
-
这就是为什么我总是用括号编程。您的表达式被评估为 (hasBody || (hasImage() && !hasTitle())。
-
我的问题是 -- "private boolean hasTitle() { return !titleText.getText().toString().trim().isEmpty(); }" 这返回一个值而不是 0或为空,但当涉及到这个 !hasTitle() 时,它被读作 !hastTitle ,这是完全相反的。我真的不明白
-
根据您的代码,如果有标题文本,hasTitle() 应该返回 true。并且 getText() 不应该返回一个字符串,这使得 toString() 调用无用。您可能想打印出这些值。
标签: java text-recognition