I am using google vision API and trying to get the text from the captured image. I have set the captured image in an image view and then I am trying to get the text from the image. but I am getting SparseArray of size 0. what can be the problem. Here is my java code.
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);
}
}
}
here is my main activity xml file.
<?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>
The main thing is when i set image manually in an imageView it shows the result but when i capture the image by my self and then i try to get the text i am not getting the results i am always gets a 0 sized array.
Why don't you store the bitmap in a global variable? You can of course convert the drawable of an ImageView back to a bitmap but this requires extra ressources.
Anyway the answer to your question would be:
if(imgPic.getDrawable() != null && imgPic.getDrawable() instanceof BitmapDrawable) {
Bitmap bitmap = ((BitmapDrawable)imgPic.getDrawable()).getBitmap();
}
You should check if the drawable is not null and if the bitmap stored inside the drawable is really of type BitmapDrawable
.