How to make Image to text converter application
Set up your Android project in Android Studio.
Add the necessary dependencies in your app's
build.gradle
file:
implementation 'com.google.android.gms:play-services-vision:20.1.3'
Create an activity or fragment in your app where you want to implement the image-to-text functionality.
In the layout XML file for the activity or fragment, add a Button and an ImageView element to allow the user to select an image and display the selected image.
In your activity or fragment class, declare the necessary variables:
private static final int PICK_IMAGE_REQUEST = 1;
private Bitmap selectedImage;
private TextView textView;
ImageView imageView;
- Implement the image selection functionality by handling the button click event and launching the image picker:
private void selectImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri imageUri = data.getData();
try {
selectedImage = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
imageView.setImageBitmap(selectedImage);
} catch (IOException e) {
e.printStackTrace();
}
}
}
- Next, implement the image-to-text conversion using the Text Recognition API:
// Inside your activity or fragment class
// ...
private void convertImageToText() {
if (selectedImage != null) {
TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
if (!textRecognizer.isOperational()) {
Toast.makeText(this, "Text recognizer could not be set up on your device", Toast.LENGTH_SHORT).show();
return;
}
Frame imageFrame = new Frame.Builder()
.setBitmap(selectedImage)
.build();
SparseArray<TextBlock> textBlocks = textRecognizer.detect(imageFrame);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < textBlocks.size(); i++) {
TextBlock textBlock = textBlocks.valueAt(i);
stringBuilder.append(textBlock.getValue());
stringBuilder.append("\n");
}
String extractedText = stringBuilder.toString();
// Do something with the extracted text, such as displaying it in a TextView
textView.setText(extractedText);
}
}
- Finally, wire up the button click events to call the respective methods:
// Inside your activity or fragment class
// ...
textView = findViewById(R.id.textView);
imageView = findViewById(R.id.imageView);
Button buttonConvertToText = findViewById(R.id.button);
Button buttonSelectImage = findViewById(R.id.selectimg);
buttonSelectImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
}
});
buttonConvertToText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
convertImageToText();
}
});
Full code
That's it! You should now have a basic image-to-text functionality in your Android app using the Text Recognition API. Remember to handle any necessary permissions in your app's manifest file, such as READ_EXTERNAL_STORAGE for accessing the image from the gallery.
package com.devstudio.imagetotext;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.vision.Frame;
import com.google.android.gms.vision.text.TextBlock;
import com.google.android.gms.vision.text.TextRecognizer;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private static final int PICK_IMAGE_REQUEST = 1;
private Bitmap selectedImage;
private TextView textView;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Inside your activity or fragment class
// ...
textView = findViewById(R.id.textView);
imageView = findViewById(R.id.imageView);
Button buttonConvertToText = findViewById(R.id.button);
Button buttonSelectImage = findViewById(R.id.selectimg);
buttonSelectImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
}
});
buttonConvertToText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
convertImageToText();
}
});
}
private void selectImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri imageUri = data.getData();
try {
selectedImage = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
imageView.setImageBitmap(selectedImage);
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Inside your activity or fragment class
// ...
private void convertImageToText() {
if (selectedImage != null) {
TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
if (!textRecognizer.isOperational()) {
Toast.makeText(this, "Text recognizer could not be set up on your device", Toast.LENGTH_SHORT).show();
return;
}
Frame imageFrame = new Frame.Builder()
.setBitmap(selectedImage)
.build();
SparseArray<TextBlock> textBlocks = textRecognizer.detect(imageFrame);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < textBlocks.size(); i++) {
TextBlock textBlock = textBlocks.valueAt(i);
stringBuilder.append(textBlock.getValue());
stringBuilder.append("\n");
}
String extractedText = stringBuilder.toString();
// Do something with the extracted text, such as displaying it in a TextView
textView.setText(extractedText);
}
}
}
No comments
Wellcome to Bengalitutorial..