Friday, March 23, 2012

Combining 2 Images in Android using Canvas ?


Below are the methods to canvert the two bitmap in to one..........


 private BitmapDrawable createUserMarker(Bitmap bmp1, Bitmap bmp2) {
Bitmap bitmapStar = bmp1;
Bitmap bmOverlay = Bitmap.createBitmap(bmp2.getWidth(), bmp2.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bmOverlay);
canvas.drawARGB(0x00, 0, 0, 0);
canvas.drawBitmap(bmp2, 0, 0, null);
canvas.drawBitmap(bitmapStar, 2, 2, null);
BitmapDrawable dr = new BitmapDrawable(bmOverlay);
dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());
return (dr);
    }


OR


 public Bitmap combineImages(Bitmap c, Bitmap s) { 
// can add a 3rd parameter 'String loc' if you want to save the new
// image - left some code to do that at the bottom 
   Bitmap cs = null; 
 
   int width, height = 0; 
    
   if(c.getWidth() > s.getWidth()) { 
     width = c.getWidth(); 
     height = c.getHeight() + s.getHeight(); 
   } else { 
     width = s.getWidth(); 
     height = c.getHeight() + s.getHeight(); 
   } 
 
   cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
 
   Canvas comboImage = new Canvas(cs); 
 
   comboImage.drawBitmap(c, 0f, 0f, null); 
   comboImage.drawBitmap(s, 0f, c.getHeight(), null); 
 
   // this is an extra bit I added, just incase you want to save the new image somewhere and then return the location 
   /*String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png"; 
 
   OutputStream os = null; 
   try { 
     os = new FileOutputStream(loc + tmpImg); 
     cs.compress(CompressFormat.PNG, 100, os); 
   } catch(IOException e) { 
     Log.e("combineImages", "problem combining images", e); 
   }*/ 
 
   return cs; 
 } 




No comments:

Post a Comment