Reputation: 63
I am trying to join images and form 1 single image.This is done to send the image to the server.
//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);
But the problem I am facing is my "bitmap" is coming as null.
Can anyone please help me joining images.
Upvotes: 1
Views: 2404
Reputation: 949
In this case you can use canvas like as follows
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;
}
Upvotes: 3