1

I am using 2 EditText, one for title and other for body text. i am getting the body text from my image through text recognizer and title from just simply typing the title. But when i call the function for text recognizer and then saving the data, my title text shows a strange behavior and give me the alert dialogue of missing title even i type the title. I don't know why with text recognizer function the Title edit text is showing me alert dialog of missing title. here is my code

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();
                }
            }

my text recognizer for the body text only

 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());
    }
  • I notice that you have (hasBody() || hasImage() && !hasTitle()). The && is evaluated before the ||. – NomadMaker Feb 16 '20 at 06:02
  • sorry did not understand? can you please explain more. if i not run the text recognition then it is working fine and not asking me for the missing title. – Galaxy Apps Feb 16 '20 at 06:10
  • This is why I always program with parentheses. Your expression is evaluated as (hasBody || (hasImage() && !hasTitle()). – NomadMaker Feb 16 '20 at 06:13
  • my problem is that -- "private boolean hasTitle() { return !titleText.getText().toString().trim().isEmpty(); }" this returns a value and not 0 or empty but when it comes to this !hasTitle(), it is read as !hastTitle which is totally opposite. I really dont understand – Galaxy Apps Feb 16 '20 at 06:18
  • According to your code, hasTitle() should return true if there is title text. And shouldn't getText() return a String, which makes the toString() call useless. You might want to print out these values. – NomadMaker Feb 16 '20 at 06:54

1 Answers1

1

try this

if ((hasImage() && !hasTitle()) || (hasBody() && !hasTitle()))

user2592807
  • 374
  • 1
  • 2
  • 14