textRecognizer.detect(frame);它返回大小为 0



>我正在使用谷歌视觉API,并试图从捕获的图像中获取文本。 我已经在图像视图中设置了捕获的图像,然后我正在尝试从图像中获取文本。但我得到大小为 0 的稀疏数组。可能是什么问题。这是我的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);

}
}
}

这是我的主要活动 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();
}

您应该检查可绘制对象是否不为 null,以及存储在可绘制对象中的位图是否确实属于BitmapDrawable类型。

最新更新