KCN
KCN

Reputation: 462

Merging Two Images into one Image

I know this question was already answered but It was not helped me. My question is not to overlap the Images I want to join two seperate Images which are in the same size like which is shown below.

![I want like below image][1]

Here is the code: Used to combine Images

private Bitmap createSingleImageFromMultipleImages(Bitmap firstImage, Bitmap secondImage) {

        Bitmap result = Bitmap.createBitmap(firstImage.getWidth(), firstImage.getHeight(), firstImage.getConfig());
        Canvas canvas = new Canvas(result);
        canvas.drawBitmap(firstImage, 0f, 0f, null);
        canvas.drawBitmap(secondImage, 200, 200, null);
        return result;
    }

 Bitmap mergedImages = createSingleImageFromMultipleImages(firstImage, SecondImage);

                    im.setImageBitmap(mergedImages);

I am getting ovelapping image. Can anyone help.

Thanks.

Upvotes: 1

Views: 9841

Answers (2)

AKHIL V S
AKHIL V S

Reputation: 44

I used this solution from https://stackoverflow.com/a/25569617/12074613

private Bitmap getBitmap(View v) {
v.clearFocus();
v.setPressed(false);

boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);

// Reset the drawing cache background color to fully 
transparent
// for the duration of this operation
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);

if (color != 0) {
    v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if (cacheBitmap == null) {
    Toast.makeText(StopWarApp.getContext(), 
"Something went wrong",
            Toast.LENGTH_SHORT).show();
    return null;
}

Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);

return bitmap;
}

Upvotes: 0

caulitomaz
caulitomaz

Reputation: 2331

If you want to create a side-by-side merged image you will need to create a result bitmap with 2 times the width of first image, or, more scalably, the sum of the widths of the images:

Currently, you are creating a result image with width firstImage.getWidth(). They will clearly overlap or be off the canvas.

Also, you will need to place the second image at x == firstImage.getWidth()

Check out this code (untested):

private Bitmap createSingleImageFromMultipleImages(Bitmap firstImage, Bitmap secondImage) {
        Bitmap result = Bitmap.createBitmap(firstImage.getWidth() + secondImage.getWidth(), firstImage.getHeight(), firstImage.getConfig());
        Canvas canvas = new Canvas(result);
        canvas.drawBitmap(firstImage, 0f, 0f, null);
        canvas.drawBitmap(secondImage, firstImage.getWidth(), 0f, null);
        return result;
}

Bitmap mergedImages = createSingleImageFromMultipleImages(firstImage, secondImage);

im.setImageBitmap(mergedImages);

Upvotes: 3

Related Questions