Question

I am trying to overlay 2 images on on top of other. One image i getting from other class:

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

The other image is from drawable:

Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.topshow);

Then I get the overlay:

Bitmap overLay = (overlay(bitmap,icon));

Overlay func:

public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) 
{
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    canvas.drawBitmap(bmp2, 0, 0, null);
    return bmOverlay;
}

And save the overlay as jpg:

overLay.compress(Bitmap.CompressFormat.JPEG, 80,new FileOutputStream(file_name + ".jpg"));

The problem is the image I get look not so good. On the border of the overlay image is interference and it looks like low quality.

Both images are 288x384. They both look good before the overlay. What can you suggest me?

Was it helpful?

Solution

Try this code. I hope it will help you.

            Bitmap bMap = null;
            Bitmap tempbMap = null;
            tempbMap = Constants.wholeBitmap;

            bMap = Bitmap.createScaledBitmap(tempbMap, 320, 320, true);

            int BORDER_WIDTH = 0;
            int BORDER_COLOR = Color.parseColor("#00000000");
            Bitmap res = Bitmap.createBitmap(bMap.getWidth() + 2 * BORDER_WIDTH,
                                             bMap.getHeight() + 2 * BORDER_WIDTH,
                                             bMap.getConfig());
            Canvas c = new Canvas(res);
            Paint p = new Paint();

            p.setColor(BORDER_COLOR);
            c.drawRect(0, 0, res.getWidth(), res.getHeight(), p);
            p = new Paint(Paint.FILTER_BITMAP_FLAG);
            c.drawBitmap(bMap, BORDER_WIDTH, BORDER_WIDTH, p);

            Bitmap bMapFinal = Bitmap.createBitmap(res, 0, 0, res.getWidth(), res.getHeight(), null, true);

            Bitmap bitmapOverlay = BitmapFactory.decodeResource(getResources(), R.drawable.overlay_3_large);

            int width = 293;
            int height = 55; 

            int w = (int) (bMapFinal.getWidth()/2);
            int h = (int) ((w * height) / width);

            Bitmap scaled = Bitmap.createScaledBitmap(bitmapOverlay, w, h, true);

            c.drawBitmap(scaled, bMapFinal.getWidth() - scaled.getWidth(), bMapFinal.getHeight() - scaled.getHeight(), p);

            Bitmap bMapFinal_new = Bitmap.createBitmap(res, 0, 0, res.getWidth(), res.getHeight(), null, true);

            ivImageMain.setImageBitmap(null);
            ivImageMain.destroyDrawingCache();

            Constants.finalBitmapForShare = bMapFinal_new;

            ivImageMain.setImageBitmap(bMapFinal_new);

Thank you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top