определить положение и размер textView с помощью Rect или Point [] - PullRequest
0 голосов
/ 16 октября 2019

Я пытаюсь охватить написание изображения с помощью firebase. он возвращает Point [] или Rect ( здесь - документация ) блока текста, который он обнаруживает, и я хотел бы определить положение и размер этих текстовых представлений, чтобы они покрывали текстизображение.

Я пытаюсь «поиграть» со значениями Point и Rect, но текстовые просмотры все время не совпадают с текстом изображения. результат: здесь

Может кто-нибудь объяснить мне, в чем проблема, где я ошибаюсь и более или менее, что мне нужно сделать, чтобы решить?

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/ConstraintLayout"
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scrollbarAlwaysDrawHorizontalTrack="true"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_launcher_background" />
    </androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
            immagine = (ImageView) findViewById(R.id.imageView);
            bitmap =getBitmapFromURL("img_url");
            immagine.setImageBitmap(bitmap);
            BitmapDrawable drawable = (BitmapDrawable) immagine.getDrawable();
            imgv = drawable.getBitmap();
            vpp = (ConstraintLayout) findViewById(R.id.ConstraintLayout);
    }
     protected void onStart() {
            super.onStart();
            FirebaseVisionImage image2 = FirebaseVisionImage.fromBitmap(imgv);
            FirebaseVisionTextRecognizer textRecognizer = FirebaseVision.getInstance()
                    .getOnDeviceTextRecognizer();

        final Task<FirebaseVisionText> result2 =
                textRecognizer.processImage(image2)
                        .addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
                            @Override
                            public void onSuccess(FirebaseVisionText firebaseVisionText) {
                                // Task completed successfully
                                Integer i = 1;
                                resultText = firebaseVisionText.getText();
                                output = resultText;
                                tvResult.setText(output);
                                for (FirebaseVisionText.TextBlock block: firebaseVisionText.getTextBlocks()) {
                                    String blockText = block.getText();
                                    Float blockConfidence = block.getConfidence();
                                    List<RecognizedLanguage> blockLanguages = block.getRecognizedLanguages();

                                    Point[] blockCornerPoints = block.getCornerPoints();
                                    Rect blockFrame = block.getBoundingBox();

                                    createCover(blockCornerPoints,blockFrame,i,blockText);

                                     i++;
                                }
                            }
                        })
                            .addOnFailureListener(
                                    new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception e) {
                                            // Task failed with an exception
                                        }
                                    });


        }

    public void createCover(Point[] CornerPoint,Rect blockFrame, int i,String blockText){

        textViewArray = new TextView[500];
        textViewArray[i] = new TextView(context);
        textViewArray[i].setText(blockText);
        textViewArray[i].setTextSize(8);
        textViewArray[i].setBackgroundColor(Color.rgb(0, 255, 0));

        textViewArray[i].setHeight(CornerPoint[3].y - CornerPoint[0].y);
        textViewArray[i].setWidth(CornerPoint[1].x - CornerPoint[0].x);

        //textViewArray[i].setX(blockFrame.left+(blockFrame.right - blockFrame.left) /2);
        //textViewArray[i].setY(blockFrame.top+(blockFrame.bottom - blockFrame.top) /2);
        textViewArray[i].setX(CornerPoint[0].x);
        textViewArray[i].setY(CornerPoint[0].y);



        vpp.addView(textViewArray[i]);
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...