1

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" />
Loving Android
  • 253
  • 5
  • 15
  • when i use that bitmap from the request with textRecognizer, it returns null – Loving Android Mar 16 '18 at 07:19
  • `now i want to decode this image using BitmapFactory` ??? What do you mean with 'this image'? You have already a bitmap. There is nothing else. And whatever you would let BitmapFactory decode you would get a bitmap too. – greenapps Mar 16 '18 at 08:54
  • i didn't use proper words to explain my point so i put an example that works perfectly with images from drawable. – Loving Android Mar 16 '18 at 09:00
  • Your example does not explain what you want or need. You should start writing a better post and question. – greenapps Mar 16 '18 at 09:02
  • You can use the bitmap you obtained from the camera in `Frame frame = new Frame.Builder().setBitmap(bitmap).build();` too. What is the problem doing so? – greenapps Mar 16 '18 at 09:07
  • the problem is when i do that , i get no result from TextRec detector. – Loving Android Mar 16 '18 at 09:23
  • Well why cannot i read that in your post? And second why do you think you would get a result if you could use bitmap factory? – greenapps Mar 16 '18 at 09:25
  • It would also have been nice if you had started your post telling that you use the camera to take a picture of a text. And that you want to decode a text. OCR. – greenapps Mar 16 '18 at 09:29
  • ok i just updated my post again , for some reason i thougt the main problem was with the bitmapFactory , anyway thanks for your time and questions – Loving Android Mar 16 '18 at 09:32
  • You still do not start your post telling that you take a picture of a text. – greenapps Mar 16 '18 at 09:34
  • `i thougt the main problem was with the bitmapFactory , `. Nonsense. You are not using that. So how could it? – greenapps Mar 16 '18 at 09:35
  • You have two bitmaps. Both contain text i presume. One text is readable at ocr and the other not. So why can one bitmap not be used? Display both in an ImageView and compare. Then you will realise the difference. – greenapps Mar 16 '18 at 09:37
  • have you tried yourself to make this work? without using picture from the drawable! i mean taking a picture then read it with ocr – Loving Android Mar 16 '18 at 09:41
  • Yes i know exactly what is going on and why you cant use that bitmap. Im trying to let you realise and formulate your problem. – greenapps Mar 16 '18 at 09:44
  • The funny thing here is that indeed to solve your problem BitmapFactory has to be used. And code to do so you can read in the answers. But. Why that bitmap is not usable and why answer would work has not been explained yet. Moreover the answers are not complete. – greenapps Mar 16 '18 at 09:47
  • And no. I have not used ocr. But the reason your bitmap from the camera is not usable is obvious. The reason has been published a hundred times here on stackoverflow. – greenapps Mar 16 '18 at 09:50
  • then keep your secret answer for yourself – Loving Android Mar 16 '18 at 11:21

3 Answers3

1

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;
        }
    }
Adib Faramarzi
  • 3,798
  • 3
  • 29
  • 44
0

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

gokhan
  • 627
  • 1
  • 6
  • 16
0

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();
        }
    }
}
Dumbo
  • 1,630
  • 18
  • 33
  • looks good and easy but still getting null when i try to use the bitmap with TextRecognizer – Loving Android Mar 16 '18 at 07:58
  • That's strange because that works for me (without `TextRecognizer`). Could it be something else null not `Bitmap`? It will be better that you would show your code where you getting `Bitmap` and where/how you using it – Dumbo Mar 16 '18 at 08:25
  • i know it's confusing , i will update my question with full example to show you how i use it with TextRec. it works perfectly if the image were in drawable folder. – Loving Android Mar 16 '18 at 08:40
  • @LovingAndroid On which of these lines you're getting a null pointer exception? – Dumbo Mar 16 '18 at 11:46
  • not your code , just the TextRec detector doesn't read anything and returns null when i pass the bitmap to it. – Loving Android Mar 16 '18 at 12:34
  • i think i found the issue , probably because the image becomes very small when i get it from onActivityResult – Loving Android Mar 16 '18 at 12:36
  • Try to use `Bitmap bigBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth()*2, bitmap.getHeight()*2, true);` to resize your `Bitmap` to bigger. @LovingAndroid – Dumbo Mar 16 '18 at 12:46
  • good idea, but it looks like a 400 years old pic lol , me myself can't read it :) – Loving Android Mar 16 '18 at 13:00
  • Have you tried with other picture? Maybe it's because of the picture. :D – Dumbo Mar 16 '18 at 13:58
  • i did and still the same :( , but i found a way to get rid of this issue , by getting the file name and path of the pic and view it – Loving Android Mar 16 '18 at 21:06
  • but i can't change the orientation to portrait when i view it – Loving Android Mar 16 '18 at 21:08