[英]How do I merge Bitmap side-by-side
I want to merge two bitmaps side-by-side into one bitmap. 我想将两个位图并排合并为一个位图。 The following code is merge sub-bottom.
以下代码是合并子底部。 How do I merge side-by-side into one bitmap ?
我如何并排合并为一个位图?
public Bitmap mergeBitmap(Bitmap fr, Bitmap sc)
{
Bitmap comboBitmap;
int width, height;
width = fr.getWidth() + sc.getWidth();
height = fr.getHeight();
comboBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(comboBitmap);
comboImage.drawBitmap(fr, 0f, 0f, null);
comboImage.drawBitmap(sc, 0f , fr.getHeight(), null);
return comboBitmap;
}
public Bitmap mergeBitmap(Bitmap fr, Bitmap sc)
{
Bitmap comboBitmap;
int width, height;
width = fr.getWidth() + sc.getWidth();
height = fr.getHeight();
comboBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(comboBitmap);
comboImage.drawBitmap(fr, 0f, 0f, null);
comboImage.drawBitmap(sc, fr.getWidth(), 0f , null);
return comboBitmap;
}
This article goes through the process of combining 2 images one below the other(only works with PNG or JPG ). 本文介绍了将两个图像彼此叠加的过程(仅适用于PNG或JPG)。 It will involve passing 2 Bitmaps, which will then get combined using the Canvas class.
它将涉及传递2个位图,然后使用Canvas类将其合并。 You can do somme minor changes to get your two images side by side:
您可以进行一些小的更改以使两个图像并排显示:
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.getHeight() > s.getHeight()) {
width = c.getWidth() + s.getWidth(;
height = c.getHeight());
} else {
width = c.getWidth() + s.getWidth();
height = 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, c.getWidth(), 0f, 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.