-1

please read the full question before marking it as duplicate or down-vote it.

i am developing an app what can slice through a picture and run google vision to recognize text in each chunk or slice of picture and run OCR to detect that the circle bubble is filled or not in the chunk. but when i am slicing the Bitmap image in an array and pass it to other activity for the process it crashes for over use of memory. I know i can compress it but i tried that already (though i did not wanted to compress it since i need to run google vision and may not able to extract text accurately) but it did not work since there are 46 slices of image. How can i do so without uploading on cloud fetch it again for process since it might take long. any alternative solution is very welcome as well. i am stuck on this for quite a while.

import android.content.Intent;.....

public class ProcessesdResult extends AppCompatActivity {

TextView tvProcessedText;
Button btnImageSlice;
Bitmap image;
int chunkNumbers =46;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_processesd_result);

    Intent intenttakeattendance = getIntent();
    String fname = intenttakeattendance.getStringExtra("fname");

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root);

    String photoPath = myDir+"/sams_images/"+ fname;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    image = BitmapFactory.decodeFile(photoPath, options);


    btnImageSlice=findViewById(R.id.btnimageslice);
    btnImageSlice.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            splitImage(image, chunkNumbers) ;
        }
    });

}


    private void splitImage(Bitmap image, int chunkNumbers) {

        //For the number of rows and columns of the grid to be displayed
        int rows =23;
        int cols =2;

        //For height and width of the small image chunks
        int chunkHeight,chunkWidth;

        //To store all the small image chunks in bitmap format in this list
        ArrayList<Bitmap> chunkedImages = new ArrayList<Bitmap>(chunkNumbers);

        //Getting the scaled bitmap of the source image


        Bitmap scaledBitmap = Bitmap.createScaledBitmap(image, image.getWidth(), image.getHeight(), true);

        chunkHeight = image.getHeight()/rows;
        chunkWidth = image.getWidth()/cols;

        //xCoord and yCoord are the pixel positions of the image chunks
        int yCoord = 0;
        for(int x=0; x<rows; x++){
            int xCoord = 0;
            for(int y=0; y<cols; y++){
                chunkedImages.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
                xCoord += chunkWidth;
            }
            yCoord += chunkHeight;
        }

        //Start a new activity to show these chunks into a grid
        Intent intent = new Intent(ProcessesdResult.this, ChunkedImageActivity.class);
        intent.putParcelableArrayListExtra("image chunks", chunkedImages);
        startActivity(intent);
    }


}

This is the image type i want to slice in pieces This is the image type i want to slice in pieces

karthik
  • 528
  • 4
  • 19
MEHEDI HASAN
  • 149
  • 4
  • 11

3 Answers3

0

You dont want to pass objects between activities, especially not huge objects like bitmaps. I would suggest saving your bitmaps in the devices file system and then passing a list of URI's. Saving the bitmaps like this and recycling your bitmaps after you are done using them should also reduce the RAM usage during your loop where you slice up the image.

For saving bitmaps as files i would refer to this question: Saving and Reading Bitmaps/Images from Internal memory in Android

So basically your loop should look like this:

for(int x=0; x<rows; x++){
        int xCoord = 0;
        for(int y=0; y<cols; y++){
            Bitmap image = Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight);
            Uri uri = saveBitmapAsFile(image);
            image.recycle();
            xCoord += chunkWidth;
        }
        yCoord += chunkHeight;
    }
StarterPack
  • 498
  • 1
  • 7
  • 28
0

use android:largeHeap="true" in manifest if you still get the same error then try this : instead of sending "intent.putParcelableArrayListExtra("image chunks", chunkedImages);" bitmap to another activity, save that image to local storage and use path wherever you want.

Rahul Singh
  • 161
  • 2
  • 9
0

I recommended for create another data(Bitmap) store class with static. Use this class for save bitmaps and call another activity for read.

This link helpful.

seokrae.kim
  • 304
  • 3
  • 9