Quantcast
Channel: Kyogs
Viewing all articles
Browse latest Browse all 18

Merge Images in Android

$
0
0

Merge two or more images in android by using Canvas its simple to merge image by using below code,
first create bitmap for particular image which you want to merge it.

get X and Y axis position for which area you want to merge images.

        mComboImage = new Canvas(mBackground);

       mComboImage.drawBitmap(c, x-axis position in f, y-axis position in f, null);

        mComboImage.drawBitmap(c, 0f, 0f, null);
        mComboImage.drawBitmap(s, 200f, 200f, null);
      
        
        mBitmapDrawable = new BitmapDrawable(mBackground);
        Bitmap mNewSaving = ((BitmapDrawable)mBitmapDrawable).getBitmap();


set this new bitmap in imageview. 
imageView.setImageBitmap(mNewSaving);


Here in this method two image bitmap combine in one bitmap which return bitmap of new merge image.Also save this image on sdcard.As  below code


public Bitmap combineImages(Bitmap c, Bitmap s) {
    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, new Matrix(), null);
    comboImage.drawBitmap(s, new Matrix(), 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; 
  } 
}



Viewing all articles
Browse latest Browse all 18

Trending Articles