Search code examples
javatext-recognition

Text recognizer changes the behavior of saving data edit text


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

Solution

  • try this

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