Loving Android
Loving Android

Reputation: 263

How to Take a picture and then read its Text using TextRecognizer

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" />

Upvotes: 1

Views: 2107

Answers (3)

Dumbo
Dumbo

Reputation: 1837

That is one of the simplest ways to get Bitmap from taken image by using InputStream and BitmapFactory.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    InputStream stream;
    if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
        try {
            stream = getContentResolver().openInputStream(data.getData());
            Bitmap bitmap = BitmapFactory.decodeStream(stream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 0

gokhan
gokhan

Reputation: 667

First you can convert bitmap to byte array like that

 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
 byte[] byteArray = outputStream.toByteArray();

After that you can use BitmapFactory' s decodeByteArray() function with option

BitmapFactory.decodeByteArray(byteArray , 0 ,byteArray.length,*option)

*option is your bitmap factory option

Upvotes: 0

Adib Faramarzi
Adib Faramarzi

Reputation: 4060

It's better to give a file to the camera intent, so It saves the file for you.

According to This Answer:

private static final int CAMERA_PHOTO = 111;
private Uri imageToUploadUri;

private void captureCameraImage() {
        Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File f = new File(Environment.getExternalStorageDirectory(), "POST_IMAGE.jpg");
        chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
        imageToUploadUri = Uri.fromFile(f);
        startActivityForResult(chooserIntent, CAMERA_PHOTO);
    }

@Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == CAMERA_PHOTO && resultCode == Activity.RESULT_OK) {
                if(imageToUploadUri != null){
                    Uri selectedImage = imageToUploadUri;
                    getContentResolver().notifyChange(selectedImage, null);
                    Bitmap reducedSizeBitmap = getBitmap(imageToUploadUri.getPath());
                    if(reducedSizeBitmap != null){
                        ImgPhoto.setImageBitmap(reducedSizeBitmap);
                        Button uploadImageButton = (Button) findViewById(R.id.uploadUserImageButton);
                          uploadImageButton.setVisibility(View.VISIBLE);                
                    }else{
                        Toast.makeText(this,"Error while capturing Image",Toast.LENGTH_LONG).show();
                    }
                }else{
                    Toast.makeText(this,"Error while capturing Image",Toast.LENGTH_LONG).show();
                }
            } 
        }

private Bitmap getBitmap(String path) {

        Uri uri = Uri.fromFile(new File(path));
        InputStream in = null;
        try {
            final int IMAGE_MAX_SIZE = 1200000; // 1.2MP
            in = getContentResolver().openInputStream(uri);

            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(in, null, o);
            in.close();


            int scale = 1;
            while ((o.outWidth * o.outHeight) * (1 / Math.pow(scale, 2)) >
                    IMAGE_MAX_SIZE) {
                scale++;
            }
            Log.d("", "scale = " + scale + ", orig-width: " + o.outWidth + ", orig-height: " + o.outHeight);

            Bitmap b = null;
            in = getContentResolver().openInputStream(uri);
            if (scale > 1) {
                scale--;
                // scale to max possible inSampleSize that still yields an image
                // larger than target
                o = new BitmapFactory.Options();
                o.inSampleSize = scale;
                b = BitmapFactory.decodeStream(in, null, o);

                // resize to desired dimensions
                int height = b.getHeight();
                int width = b.getWidth();
                Log.d("", "1th scale operation dimenions - width: " + width + ", height: " + height);

                double y = Math.sqrt(IMAGE_MAX_SIZE
                        / (((double) width) / height));
                double x = (y / height) * width;

                Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x,
                        (int) y, true);
                b.recycle();
                b = scaledBitmap;

                System.gc();
            } else {
                b = BitmapFactory.decodeStream(in);
            }
            in.close();

            Log.d("", "bitmap size - width: " + b.getWidth() + ", height: " +
                    b.getHeight());
            return b;
        } catch (IOException e) {
            Log.e("", e.getMessage(), e);
            return null;
        }
    }

Upvotes: 1

Related Questions