1

I´m trying to merge 2 images, one is bitmap from camera, second one is .png file stored in drawables. What I did was that I used both images as bitmaps and I tried to merge them by using canvas, something like this:

Bitmap topImage = BitmapFactory.decodeFile("gui.png");
Bitmap bottomImage = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);

Canvas canvas = new Canvas(bottomImage);
canvas.drawBitmap(topImage, 0, 0, null);

But I keep getting "Bitmap size exceeds VM budget" error all the time. I tried nearly everything, but still, it keeps throwing this error. Is there another way of merging 2 images? What i need to do is simple - I need to take photo and save it merged with that .PNG image stored in drawables. For example this app is very close to what i need - https://play.google.com/store/apps/details?id=com.hl2.hud&feature=search_result#?t=W251bGwsMSwyLDEsImNvbS5obDIuaHVkIl0.

Thanks :)

andrejbroncek
  • 421
  • 5
  • 17

2 Answers2

4

See the below code for combining two images. This method returns combined bitmap

public Bitmap combineImages(Bitmap frame, Bitmap image) {
        Bitmap cs = null;
        Bitmap rs = null;

        rs = Bitmap.createScaledBitmap(frame, image.getWidth() + 50,
                image.getHeight() + 50, true);

        cs = Bitmap.createBitmap(rs.getWidth(), rs.getHeight(),
                Bitmap.Config.RGB_565);

        Canvas comboImage = new Canvas(cs);

        comboImage.drawBitmap(image, 25, 25, null);
        comboImage.drawBitmap(rs, 0, 0, null);
        if (rs != null) {
            rs.recycle();
            rs = null;
        }
        Runtime.getRuntime().gc();
        return cs;
    }

You can change height and width as per your requirements Hope this will help...

nidhi_adiga
  • 1,114
  • 1
  • 16
  • 27
0

How large are the images? I've only encountered this problem when trying to load large images into memory.

Is the byte array your decoding actually an image?

From a quick look at the android docs you can capture an image using the default camera app which may work in this situation.

http://developer.android.com/training/camera/photobasics.html

Also see this question: Capture Image from Camera and Display in Activity

Edit: You may also need to scale the image from the camera down if it is very large. See the end of the android page I linked to for details on that.

Community
  • 1
  • 1
Lyndon Armitage
  • 417
  • 4
  • 11
  • Well, I used this sample code for my purposes - http://android-er.blogspot.sk/2011/01/start-camera-auto-focusing-autofocus.html It does exactly what I need - previews image from camera, takes photo on buttonclick, autofocus on screen click. Problem is with merging that photo that comes out of this code with my .PNG image stored in drawables. – andrejbroncek Feb 02 '13 at 14:58