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;
}
}
To have both images show, you need to change the alpha of the second image.
ReplyDeleteCanvas comboImage = new Canvas(cs);
comboImage.drawBitmap(c, new Matrix(), null);
Paint paint = new Paint();
paint.setAlpha(128);
comboImage.drawBitmap(s, new Matrix(), paint);
how can i set s bitmap inside c bitmap
Deletehow can i set s bitmap inside c bitmap
ReplyDeleteCanvas comboImage = new Canvas(cs);
DeletecomboImage.drawBitmap(s, new Matrix(), null);
comboImage.drawBitmap(c, new Matrix(), null);
And you can set your position at which position you want to set your bitmap.
Here in above example i set bitmap at fix position.
mComboImage.drawBitmap(c, 0f, 0f, null);
mComboImage.drawBitmap(s, 200f, 200f, null);
Thank you very much, Mr Kamaliya Yogesh! It works fine. But I have a new problem. Now I can merge two images(first image as a frame, second image as photo inside frame) into one image, but if I have a image view as icon , this icon can be drag and scale size, and I want to merge this icon into the image that has been merged before and save it to sdcard. Can You give me an example in this case? Thank you, Mr Kamaliya Yogesh!
Deletegreat tuts Mr Kamaliya... can i see ur full code? thanks before
ReplyDeleteprivate Bitmap CombineBitmap(){
DeleteBitmap c = null;
Bitmap s = null;
Bitmap newBitmap = null;
try {
c = BitmapFactory.decodeStream(
getContentResolver().openInputStream(source1));
s = BitmapFactory.decodeStream(
getContentResolver().openInputStream(source2));
int w;
if(c.getWidth() >= s.getWidth()){
w = c.getWidth();
}else{
w = s.getWidth();
}
int h;
if(c.getHeight() >= s.getHeight()){
h = c.getHeight();
}else{
h = s.getHeight();
}
Config config = c.getConfig();
if(config == null){
config = Bitmap.Config.ARGB_8888;
}
newBitmap = Bitmap.createBitmap(w, h, config);
Canvas newCanvas = new Canvas(newBitmap);
newCanvas.drawBitmap(c, 0, 0, null);
Paint paint = new Paint();
paint.setAlpha(125);
newCanvas.drawBitmap(s, 0, 0, paint);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return newBitmap;
}
}
Thank you very much, Mr Kamaliya Yogesh! It works fine. But I have a new problem. Now I can merge two images(first image as a frame, second image as photo inside frame) into one image, but if I have a image view as icon , this icon can be drag and scale size, and I want to merge this icon into the image that has been merged before and save it to sdcard. Can You give me an example in this case? Thank you, Mr Kamaliya Yogesh!
ReplyDeletehi, can i see this compelet project?
ReplyDeletei whant this app but i cant do it in my app plz help me
plz send me this app (all of this project)
my mail is: googhoh@gmail.com
tnx my friend
Fantastic, Work for me flawless.
ReplyDeleteGood work. Keep it up.
Its working for me :)
ReplyDelete