8

I am making the camer application which can composite the camera preview and png.

I would like to save the images in onPictureTaken callback

my source is below

    public void onPictureTaken(byte[] data, Camera camera) {

            //preview from camera
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); 

            // overlay image 
            Bitmap overlayBmp = overlay.getDrawingCache(); 

            //blank beatmap 
            Bitmap blankBitmap = 
            Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), 
            Bitmap.Config.ARGB_8888); 

            //make canvas
            Canvas canvas = new Canvas(blankBitmap); 

            //composite image
            canvas.drawBitmap(bitmap, null,new Rect(0, 0, bitmap.getWidth(), 
    bitmap.getHeight()), null); 
            canvas.drawBitmap(overlayBmp, null,new Rect(0, 0, bitmap.getWidth(), 
    bitmap.getHeight()), null); 

However it shows outofmemoryerror,how can I solve this problem..?

05-20 15:13:49.114: E/AndroidRuntime(31647): FATAL EXCEPTION: main
05-20 15:13:49.114: E/AndroidRuntime(31647): java.lang.OutOfMemoryError
05-20 15:13:49.114: E/AndroidRuntime(31647):    at android.graphics.Bitmap.nativeCreate(Native Method)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at android.graphics.Bitmap.createBitmap(Bitmap.java:585)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at com.example.firstcameraappli.MainActivity$2.onPictureTaken(MainActivity.java:103)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:750)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at      android.os.Looper.loop(Looper.java:137)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at android.app.ActivityThread.main(ActivityThread.java:4514)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at java.lang.reflect.Method.invokeNative(Native Method)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at java.lang.reflect.Method.invoke(Method.java:511)
05-20 15:13:49.114: E/AndroidRuntime(31647):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54
whitebear
  • 11,200
  • 24
  • 114
  • 237

3 Answers3

15

Try this code....

PictureCallback myPictureCallback_JPG = new PictureCallback()
{

    @Override
    public void onPictureTaken(byte[] data, Camera arg1) {

        BitmapFactory.Options opt;

        opt = new BitmapFactory.Options();
        opt.inTempStorage = new byte[16 * 1024];
        Parameters parameters = arg1.getParameters();
        Size size = parameters.getPictureSize();

        int height11 = size.height;
        int width11 = size.width;
        float mb = (width11 * height11) / 1024000;

        if (mb > 4f)
            opt.inSampleSize = 4;
        else if (mb > 3f)
            opt.inSampleSize = 2;

        //preview from camera
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,opt); 


    }
}

get image in bitmap then you use as per your requirements.

Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54
  • Thanks Sandip! inSampleSize is good hint for me,but I think problem does not happen only when 'BitmapFactory.decodeByteArray'. There are outofmemoryerror in canvas.drawBitmap(bitmap, null,new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()), null); how can I resolve this? – whitebear May 20 '13 at 07:24
  • I think drawBitmap() is your custom method. – Mr.Sandy May 20 '13 at 07:46
  • createBitmap still shows outofmemoryerror Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888); – whitebear May 20 '13 at 08:28
  • When I use small number ((ex)100,100) instead of (v.getLayoutParams().width, v.getLayoutParams().height). it works.I can use opt for createBitmap method? – whitebear May 20 '13 at 08:29
  • try this Bitmap b = Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(), bitmap.getHeight(), null, true); – Mr.Sandy May 20 '13 at 09:26
  • but you have already one bitmap with image then why you need to create another bitmap? – Mr.Sandy May 20 '13 at 09:27
  • thanks again! it was my misunderstaning,v.getLayoutParams().width returns -1 .... .its not because of memoryerror. – whitebear May 20 '13 at 09:38
  • you solved my problem, Thank you, I was decoding without "BitmapFactory.Options" – Naveed Ahmad Oct 15 '14 at 11:12
  • 1
    @naveed ahmed you are welcome to get solution here. I put it because many persons suffering with this issue and they can get solution here. – Mr.Sandy Oct 16 '14 at 07:46
0

This is a well known issue on Android. The problem is that manipulating bitmaps requires more memory than is available in most devices. You can use certain tricks however. Some discussions and solutions to help you:

Community
  • 1
  • 1
Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
0

if you want to save image to sdcard, try like this

public void onPictureTaken(byte[] data, Camera camera) {
            camera.startPreview();
            FileOutputStream outStream = null;
            try {
                outStream = new FileOutputStream(
                        "/mnt/sdcard/mypicture.png");
                outStream.write(data);
                outStream.close();
                Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
            } catch (FileNotFoundException e) {
                e.printStackTrace();

            } catch (IOException e) {
                e.printStackTrace();
            } finally {

            }

        }
Senthil
  • 1,244
  • 10
  • 25