textRecognizer.detect(帧);它 returns 大小为 0

textRecognizer.detect(frame); it returns size as 0

我正在使用 google 视觉 API 并尝试从捕获的图像中获取文本。 我已经在图像视图中设置了捕获的图像,然后我试图从图像中获取文本。但我得到大小为 0 的 SparseArray。可能是什么问题。这是我的 java 代码。

    public class MainActivity extends AppCompatActivity {


    ImageView imgPic;
    TextView tvText;
    Button btnClick, btnCapture;
    private int REQUEST_IMAGE_CAPTURE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imgPic = findViewById(R.id.img_pic);
        tvText = findViewById(R.id.tv_text);
        btnClick = findViewById(R.id.btn_click);
        btnCapture = findViewById(R.id.btn_capture);

        btnCapture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
                }


            }
        });


        btnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

Bitmap bitmap;
 if (imgPic.getDrawable() != null && imgPic.getDrawable() instanceof BitmapDrawable) {
                bitmap = ((BitmapDrawable) imgPic.getDrawable()).getBitmap();
            }

                TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
                if (!textRecognizer.isOperational()) {

                    Toast.makeText(MainActivity.this, "could not get the text", Toast.LENGTH_SHORT).show();
                } else {
                    if (bitmap != null) {
                        Frame frame = new Frame.Builder().setBitmap(bitmap).build();
                        SparseArray<TextBlock> items = textRecognizer.detect(frame);
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < items.size(); i++) {
                            TextBlock myItem = items.valueAt(i);
                            Log.e("hello", (String) myItem.getValue());
                            sb.append(myItem.getValue());
                            sb.append("\n");

                        }

                        tvText.setText(sb.toString());
                    } else {
                        Toast.makeText(MainActivity.this, "returned null", Toast.LENGTH_SHORT).show();

                    }

                }
            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            imgPic.setImageBitmap(imageBitmap);


        }

    }
}

这是我的主要 activity xml 文件。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btn_capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="capture"
        />


    <ImageView
        android:id="@+id/img_pic"
        android:layout_width="370dp"
        android:layout_height="300dp" />

    <TextView
        android:textColor="@color/colorAccent"
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="25sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click here"
        android:id="@+id/btn_click"/>
</LinearLayout>

主要是当我在 imageView 中手动设置图像时它显示结果但是当我自己捕获图像然后我尝试获取文本时我没有得到结果我总是得到 0大小数组。

为什么不将位图存储在全局变量中?您当然可以将 ImageView 的可绘制对象转换回位图,但这需要额外的资源。

无论如何,你的问题的答案是:

if(imgPic.getDrawable() != null && imgPic.getDrawable() instanceof BitmapDrawable) {
    Bitmap bitmap = ((BitmapDrawable)imgPic.getDrawable()).getBitmap();
}

您应该检查 drawable 是否不为 null,以及存储在 drawable 中的位图是否真的是 BitmapDrawable.

类型