我正在使用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);
}
}
}
这是我的主要活动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大小的数组
答案 0 :(得分:0)
为什么不将位图存储在全局变量中?您当然可以将ImageView的可绘制对象转换回位图,但这需要额外的资源。
无论如何,您的问题的答案将是:
if(imgPic.getDrawable() != null && imgPic.getDrawable() instanceof BitmapDrawable) {
Bitmap bitmap = ((BitmapDrawable)imgPic.getDrawable()).getBitmap();
}
您应该检查可绘制对象是否不为null,以及可绘制对象内部存储的位图是否确实为BitmapDrawable
类型。