Java 在android中将两个图像合并为一个,但两个图像大小相同
我有两张照片。一个是用相机拍摄的,另一个是从画廊浏览的。但是尺寸不同。我需要合并两个图像到一个图像。但两者都需要相同的尺寸。我已经编写了将两个图像合并为一个的代码。但它显示的图像大小不同。一个是(用相机拍摄的)小的。另一个(从画廊浏览)是大尺寸的。但我需要两者的尺寸相同 我的代码:Java 在android中将两个图像合并为一个,但两个图像大小相同,java,android,eclipse,Java,Android,Eclipse,我有两张照片。一个是用相机拍摄的,另一个是从画廊浏览的。但是尺寸不同。我需要合并两个图像到一个图像。但两者都需要相同的尺寸。我已经编写了将两个图像合并为一个的代码。但它显示的图像大小不同。一个是(用相机拍摄的)小的。另一个(从画廊浏览)是大尺寸的。但我需要两者的尺寸相同 我的代码: Bitmap cs = null; Bitmap c= bmp; Bitmap s = galerypic; int width, height = 0; if(c.getWidth() > s.get
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);
}
如果将两个图像的宽度和高度设置为与两个图像中较宽的图像相同,则可以按如下方式计算目标图像的宽度和高度:
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(第二个参数)指定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);
根据纵横比管理图像大小,以两幅图像中的小图像为基础,调整大图像的大小。请不要忘记按照要求接受工作答案。要做到这一点,请点击答案左边的勾号。我真的很感谢你。我挣扎了这一步。但是没有人给出任何想法。在正确的时间你救了我。Thanx很多,很好用