我有两张照片。一个是由相机拍摄的,另一个是从画廊浏览。但是大小不一样。我需要将两个图像合并为一个图像。但两者都需要相同的大小。我编写了将两个图像合并为一个的代码。但它显示不同的图像大小。一个是(用相机拍摄)很小。另一个(从画廊浏览)是大尺寸。但我需要两者都是相同的大小。
我的代码:
Bitmap cs = null;
Bitmap c= bmp;
Bitmap s = galerypic;
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);
// String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";
//putStream os = null;
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(String.format("/sdcard/merged.jpg"));
// os = new FileOutputStream(loc + tmpImg);
cs.compress(CompressFormat.PNG, 100, outStream);
} catch(IOException e) {
Log.e("combineImages", "problem combining images", e);
}
答案 0 :(得分:1)
如果您将两个图像的宽度和高度设置为与两者中较宽的一个相同,则可以计算目标图像的宽度和高度,如下所示:
int width, height = 0;
if(c.getWidth() > s.getWidth()) {
width = c.getWidth();
height = c.getHeight() * 2;
} else {
width = s.getWidth();
height = s.getHeight() * 2;
}
cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(cs);
然后使用接受源和目标Rect的DrawBitmap版本,指定目标矩形以将其缩放为。您可以为源Rect(第二个参数)指定null以绘制整个位图:
Rect dest1 = new Rect(0, 0, width, height / 2); // left,top,right,bottom
comboImage.drawBitmap(c, null, dest1, null);
Rect dest2 = new Rect(0, height / 2, width, height); // left,top,right,bottom
comboImage.drawBitmap(s, null, dest2, null);