FirebaseTextRecognition с несколькими Textview - PullRequest
0 голосов
/ 01 октября 2019

Я изучаю, как использовать FirebaseVisionTextRecognizer, нельзя ли вставить несколько блоков текста в несколько TextView?

По этой ссылке я начинаю и учусь. Это сработало и отображает весь текст в 1 Textview. Тогда что, если в изображении 4 блока текста, невозможно разделить эти 4 блока текста на 4 разных Textview? большая помощь приветствуется :) спасибо. https://www.youtube.com/watch?v=T0273WiUQPI

       private void processText(FirebaseVisionText text) {
        List<FirebaseVisionText.TextBlock> blocks = text.getTextBlocks();
    //check if list is empty            
       if (blocks.size() == 0) {
        Toast.makeText(this, "empty text", Toast.LENGTH_SHORT).show();
        return;
    }
     //for loop the Textblock and insert text to textview
    for (FirebaseVisionText.TextBlock textBlock : text.getTextBlocks()) {
        String sText = textBlock.getText();
        textView.setText(sText);
    }
}

****Update****

**XML Code** 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl_ml_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".FirebaseTextML">

    <LinearLayout
        android:id="@+id/ll_buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/cameraButton"
            android:layout_width="0dp"
            android:layout_height="72dp"
            android:layout_marginStart="44dp"
            android:layout_weight="1"
            android:background="@color/colorPrimary"
            android:gravity="center"
            android:text="camera" />

        <TextView
            android:id="@+id/detectButton"
            android:layout_width="0dp"
            android:layout_height="72dp"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="44dp"
            android:layout_weight="1"
            android:background="@color/colorPrimary"
            android:gravity="center"
            android:text="detec" />
    </LinearLayout>

    <ImageView
        android:id="@+id/iv_img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/ll_text"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:scaleType="fitXY" />

    <LinearLayout
        android:id="@+id/ll_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/ll_buttons"
        android:layout_centerVertical="true"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="8dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_show_text_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/tv_show_text_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/tv_show_text_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:textSize="20dp" />
    </LinearLayout>
</RelativeLayout>


**Java code**

Декларация

LinearLayout mLlView;
Context mContext;


private void processText(FirebaseVisionText text) {
    mLlView.removeAllViews();
//        create a list to store the textblocks
    List<FirebaseVisionText.TextBlock> blocks = text.getTextBlocks();
    if (blocks.size() == 0) {
        Toast.makeText(this, "empty text", Toast.LENGTH_SHORT).show();
        return;
    }

    mContext  = this;
    for (FirebaseVisionText.TextBlock textBlock : text.getTextBlocks()) {
        String sText = textBlock.getText();
        TextView tv = new TextView(mContext);
        tv.setText(sText);
        mLlView.addView(tv);
    }
}

Результат Я могу разбить текст по блокам, но как мне вставить их в несколько текстовых представлений?

Ожидаемый результат:

enter image description here

1 Ответ

1 голос
/ 01 октября 2019

Вам просто нужно создать макет, в котором эти TextViews должны быть уложены, а затем вставить их в цикл следующим образом:

linearLayout.removeAllViews();
for (FirebaseVisionText.TextBlock textBlock : text.getTextBlocks()) {
    TextView textView = TextView(context);
    textView.setText(textBlock.getText());
    linearLayout.addView(textView);
}
...