Merging a camera image with an image from drawings

I am trying to combine 2 images, one is a raster image from the camera, the second is a .png file stored in drawings. What I did was that I used both images as bitmaps, and I tried to combine them using a 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 all the time I get the error "Raster size exceeds the size of the VM budget." I tried almost everything, but still, he continues to throw this error. Is there any other way to merge two images? What I need to do is simple - I need to take a picture and save it in combination with this. PNG image stored in drawings. For example, this application is very close to what I need - https://play.google.com/store/apps/details?id=com.hl2.hud&feature=search_result#?t=W251bGwsMSwyLDEsImNvbS5obDIuaHVkIl0 .

Thanks:)

0
source share
2 answers

See the code below to combine the two images. This method returns a 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;
    }

. , ...

+3

Source: https://habr.com/ru/post/1613236/


All Articles