0

I am trying to merge 3 imageViews into one bitmap i am using canvas and here is the function

private Bitmap createSingleImageFromMultipleImages() {

    Bitmap formBitmap = getBitmapFromImageView(formView);
    Bitmap idFrontBitmap = getBitmapFromImageView(idFrontView);
    Bitmap idBackBitmap = getBitmapFromImageView(idBackView);

    Bitmap allBitmaps = null;

    int width, height = 0;

    width = formBitmap.getWidth() + idFrontBitmap.getWidth() + idBackBitmap.getWidth();
    if (formBitmap.getHeight() > idFrontBitmap.getHeight()) {
        height = formBitmap.getHeight();
    } else {
        height = idFrontBitmap.getHeight();
    }

    allBitmaps = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas comboImage = new Canvas(allBitmaps);

    comboImage.drawBitmap(formBitmap, formBitmap.getWidth(), 0f, null); //this is not drawn
    comboImage.drawBitmap(idFrontBitmap, formBitmap.getWidth(), 0f, null); //this is not drawn
    comboImage.drawBitmap(idBackBitmap, idFrontBitmap.getWidth(), 0f, null); //this is drawn

    return allBitmaps;
}

//this converts ImageView to bitmap

public Bitmap getBitmapFromImageView(ImageView imageView) {
    imageView.setDrawingCacheEnabled(true);
    Bitmap scaledBitmap = imageView.getDrawingCache();
    return scaledBitmap;
}

currently only one image is drawn the other parts are empty i have confirmed that the ImageView are not null

Screenshot of the result.

enter image description here

Christopher M.
  • 130
  • 1
  • 9

1 Answers1

1

As mentioned in the comments, you are drawing your bitmaps at the top of each other, hence only the last item is visible.

You will have to properly place the images, and not just draw them anywhere.

Canvas has multiple methods that can achieve this, one possibility is to use drawBitmap(bitmap, left, top, paint) like you do, but you should use different values for the offsets.

// first, x = 0
comboImage.drawBitmap(formBitmap, 0f, 0f, null);
// second, offset by first width
comboImage.drawBitmap(idFrontBitmap, formBitmap.getWidth(), 0f, null);
// last, offset by first and second width
comboImage.drawBitmap(idBackBitmap, formBitmap.getWidth() + idFrontBitmap.getWidth(), 0f, null);

This should work.

David Medenjak
  • 33,993
  • 14
  • 106
  • 134