so i have this button to take a picture contains Text :
takePic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,100);
}
});
then i get the image from the onActivityResult :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 100) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
}
}
the problem now is when i pass this bitmap to textRecognizer detector i get no result:
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
textRecognizer = new TextRecognizer.Builder(this).build();
SparseArray<TextBlock> item = textRecognizer.detect(frame);
if the image was stored in drawable folder i can easily do this:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.myImage);
and i'm good to go with TextRecognizer ..
but i can't figure out how to deal with an image taken by the camera.
EDIT :
full example:
public class MainActivity extends AppCompatActivity {
private TextRecognizer textRecognizer;
private ImageView imageView;
private TextView Result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
Result = findViewById(R.id.tvResult);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.image);
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
textRecognizer = new TextRecognizer.Builder(this).build();
SparseArray<TextBlock> item = textRecognizer.detect(frame);
StringBuilder stringBuilder = new StringBuilder();
for (int i=0; i<item.size(); i++){
stringBuilder.append(item.valueAt(i).getValue());
}
Result.setText(stringBuilder.toString());
}
}
activity_main.xml:
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/image" />
<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="96dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="result"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />