34

I have two images and i want to save one bitmap image over another exactly at the same point where it is present i also move image using gesture .

public Bitmap combineImages(Bitmap ScaledBitmap, Bitmap bit) {

        int X = bit.getWidth();
        int Y = bit.getHeight();

        Scaled_X = ScaledBitmap.getWidth();
        scaled_Y = ScaledBitmap.getHeight();

        System.out.println("Combined Images");

        System.out.println("Bit :" + X + "/t" + Y);

        System.out.println("SCaled_Bitmap :" + Scaled_X + "\t" + scaled_Y);

        overlaybitmap = Bitmap.createBitmap(ScaledBitmap.getWidth(),
                ScaledBitmap.getHeight(), ScaledBitmap.getConfig());
        Canvas canvas = new Canvas(overlaybitmap);
        canvas.drawBitmap(ScaledBitmap, new Matrix(), null);
        canvas.drawBitmap(bit, new Matrix(), null);

        return overlaybitmap;
    }

Any help would be greatly appreciated.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
Hector4888
  • 569
  • 1
  • 10
  • 20
  • have a look at my post http://stackoverflow.com/questions/6925756/how-to-draw-image-frame-for-camera-view-and-overlapping-the-image-frame-on-captu – dinesh sharma May 16 '12 at 11:24
  • yes i see that my bitmap bmp2 merge over bmp1 but my problem is that i want to save exactlly where i double tap bmp2 after pinchzooming bmp2 ..... using gesture and problem occur is that bmp2 save acc to size of bmp1....... – Hector4888 May 16 '12 at 11:33
  • @IntelliJAmiya please stop edit with "Any help would be greatly appreciated.". Please read http://stackoverflow.com/help/editing – rnrneverdies Dec 09 '14 at 05:53
  • try this http://stackoverflow.com/questions/4863518/combining-two-bitmap-image-side-by-side – parmar prit Nov 11 '16 at 11:04

2 Answers2

76

you can combine two bitmaps like this

public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    canvas.drawBitmap(bmp2, 0, 0, null);
    bmp1.recycle();
    bmp2.recycle();
    return bmOverlay;
}
Neo
  • 3,546
  • 1
  • 24
  • 31
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • if i want some padding so that both image are visible then? – Akram May 16 '12 at 10:50
  • 2
    bmp1(500 , 500) and bmp2(400,400) yes i try that but bmp1 show thier actual size but when i pinchzoom bmp2 to(40 , 50) it save's according to bmp1 and i want to save where i put the image using gesture..... – Hector4888 May 16 '12 at 11:14
  • ok finaly i solve my problm canvas.drawBitmap(bit, parameter of canvas, parameter of canvas , null); – Hector4888 May 16 '12 at 13:20
  • I am trying this, but I am getting this error "at android.graphics.Canvas.throwIfRecycled(Canvas.java:1025)" – Amalan Dhananjayan Mar 25 '13 at 10:37
10

you can combine two bitmaps i.e a bitmap on another bitmap as an overlay
try this below code:

 public Bitmap bitmapOverlayToCenter(Bitmap bitmap1, Bitmap overlayBitmap) {
        int bitmap1Width = bitmap1.getWidth();
        int bitmap1Height = bitmap1.getHeight();
        int bitmap2Width = overlayBitmap.getWidth();
        int bitmap2Height = overlayBitmap.getHeight();

        float marginLeft = (float) (bitmap1Width * 0.5 - bitmap2Width * 0.5);
        float marginTop = (float) (bitmap1Height * 0.5 - bitmap2Height * 0.5);

        Bitmap finalBitmap = Bitmap.createBitmap(bitmap1Width, bitmap1Height, bitmap1.getConfig());
        Canvas canvas = new Canvas(finalBitmap);
        canvas.drawBitmap(bitmap1, new Matrix(), null);
        canvas.drawBitmap(overlayBitmap, marginLeft, marginTop, null);
        return finalBitmap;
    }  
Neelesh Atale
  • 526
  • 4
  • 12