0

I am writing a camera module, which use to take picture.

As the preview's width/height is different from picture's width/height, I need to clip the picture when taken picture, to make it same as what you see in screen.

I use this:

mCamera.takePicture(null, null, mJpegCallback);

to set the picture callback, and then decode and clip the bitmap in callback:

Bitmap originBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
...
Bitmap bitmap = Bitmap.createBitmap(originBitmap, 0, 0, clipWidth, clipHeight);

However, an OutOfMemoryError happen when create the clip bitmap.I have tried many ways to reuse the data instead of copying a new bitmap, but it doesn't work. So, please anybody can hep me?

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
Joe
  • 76
  • 4
  • Is there any limitation/requirement that do not allow you to use the camera intent api? – nikvs Jan 09 '14 at 09:17
  • @nikvs, Yes, this app will be bind to a custom hardware, we want to take picture when press the specified physical button on that device, the camera inten api doesn't meet our need, so we have to write it ourselves. – Joe Jan 10 '14 at 06:05

3 Answers3

1

Add android:LargeHeap=true in manifest file under application tag.

anjaneya
  • 708
  • 9
  • 11
  • Here is why you should NOT go with largeHeap as the first resort to OOM problems: [largeHeap pitfalls](http://stackoverflow.com/a/11275704/2246121) – Magnus Jan 09 '14 at 22:57
1

Thandk you all above!! I solve the problem, android has a class named BitmapRegionDecoder can meet my need. I can only decode the specified region instead of decoding the whole bitmap, and the create a new clipped bitmap, which the latter will easily cause OOM.

Joe
  • 76
  • 4
  • 1
    Hi @Joe I am also stuck in same issue. Can you please post your solution.Because it's use full to others. – user123456 Jun 20 '14 at 04:48
0

Lot of OutOfMemory exceptions on images, search for it. It's probably because the image is too large for memory. A good place to start when you receive the image is:

    BitmapFactory.options options = new BitmapFactory.options();
    options.inJustDecodeBound = true;
    //this allows you to edit the size of the image before trying to load it into memory
Pontus Backlund
  • 1,017
  • 1
  • 10
  • 17