Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to merge to two bitmap one over another

Tags:

java

android

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.

like image 487
Hector4888 Avatar asked May 16 '12 10:05

Hector4888


2 Answers

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; } 
like image 188
RajaReddy PolamReddy Avatar answered Sep 20 '22 01:09

RajaReddy PolamReddy


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;     }   
like image 33
Neelesh Atale Avatar answered Sep 19 '22 01:09

Neelesh Atale