我正在尝试加入图像并形成1个单一图像。这样做是将图像发送到服务器。
//Obtain the bitmaps from drawabl folder
Bitmap bm1 = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Bitmap bm2 = BitmapFactory.decodeResource(getResources(), R.drawable.img);
//Create a buffer
ByteBuffer buffer3 = ByteBuffer.allocate((bm1.getHeight()+bm2.getHeight()) * (bm1.getRowBytes()+bm2.getRowBytes()));
//copy the pixels to buffer
bm2.copyPixelsToBuffer(buffer3);
bm1.copyPixelsToBuffer(buffer3);
//Covert to byteArray
byte[] bytes = buffer3.array();
int leftovers = buffer3.remaining();
buffer3.compact();
//Finally forming a bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes , 0, bytes.length);
ImageView img = (ImageView) findViewById(R.id.imgV);
img.setImageBitmap(bitmap);
但是我面临的问题是我的“位图”即将为null。
谁能帮我加入图片。
最佳答案
在这种情况下,您可以如下使用画布
public Bitmap mergeBitmap(Bitmap bitmap1, Bitmap bitmap2) {
Bitmap mergedBitmap = null;
int w, h = 0;
h = bitmap1.getHeight() + bitmap2.getHeight();
if (bitmap1.getWidth() > bitmap2.getWidth()) {
w = bitmap1.getWidth();
} else {
w = bitmap2.getWidth();
}
mergedBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mergedBitmap);
canvas.drawBitmap(bitmap1, 0f, 0f, null);
canvas.drawBitmap(bitmap2, 0f, bitmap1.getHeight(), null);
return mergedBitmap;
}