Overlay images in Android - java

I have two images that I want to merge into one. (Eg "House.png" on top of "street.png")
How do i achieve this in Android? I just want to merge the images and export them to a file.
This example Sets the image to an ImageView but i wish to export it.
This other example does not work in Android since the classes are not available.

I'd try something like:
public static Bitmap mergeImages(Bitmap bottomImage, Bitmap topImage) {
final Bitmap output = Bitmap.createBitmap(bottomImage.getWidth(), bottomImage
.getHeight(), Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawBitmap(bottomImage, 0, 0, paint);
canvas.drawBitmap(topImage, 0, 0, paint);
return output;
}
(not tested, I just wrote it here, might be some simple errors in there)
Basically what you do is create a 3rd empty bitmap, draw the bottom image on it and then draw the top image over it.
As for saving to a file, here are a few examples: Save bitmap to location

You can do like this...............
public Bitmap Overlay(Bitmap Bitmap1, Resources paramResources, Bitmap Bitmap2, int alpha)
{
Bitmap bmp1 = Bitmap.createScaledBitmap(Bitmap2, Bitmap1.getWidth(), Bitmap1.getHeight(), true);
Bitmap bmp2 = Bitmap.createBitmap(Bitmap1.getWidth(), Bitmap1.getHeight(), Bitmap1.getConfig());
Paint localPaint = new Paint();
localPaint.setAlpha(alpha);
Canvas localCanvas = new Canvas(bmp2);
Matrix localMatrix = new Matrix();
localCanvas.drawBitmap(Bitmap1, localMatrix, null);
localCanvas.drawBitmap(bmp1, localMatrix, localPaint);
bmp1.recycle();
System.gc();
return bmp2;
}

Related

Porterduff Modes(Multiply)

I wanted to use porterduff multiplication mode to the PNG image, but the background became Black, only the background in the PNG extension picture becomes Black, how can I fix this?
The background should be transparent when I use the multiplication mode I want.
This line of code did not work:
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
I myself wrote code as below but it didn't work and didn't show any pictures.
private Bitmap MultiplyBitmap(Bitmap bitmapmultiply){
Bitmap bitmap = Bitmap.createBitmap(bitmapmultiply.getWidth(), bitmapmultiply.getHeight(), Bitmap.Config.ARGB_8888);
Canvas cnvs = new Canvas(bitmap);
Paint pnt = new Paint();
pnt.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
cnvs.drawBitmap(bitmapmultiply, 0, 0, pnt);
Bitmap multiplybitmap = Bitmap.createBitmap(bitmapmultiply.getWidth(), bitmapmultiply.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(multiplybitmap);
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmapmultiply,0,0, null);
canvas.drawBitmap(bitmap, 0, 0, paint);
return multiplybitmap;
}
How can I remove the black background using Java? Thanks
Please, refer to the that PoterDuff.Mode link from Android webstie where you can find a lot of examples.
You could try something like this (just adapt the code for your needs):
Kotlin
val bmpDrawable = bmp.toDrawable(res)
bmpDrawable = DrawableCompat.setTint(bmpDrawable, Color.BLACK)
val tintedBmp = bmpDrawable.bitmap
return jokeDao.insert(entity.toDbJoke())
Java
Drawable bmpDrawable = bmp.toDrawable(res)
DrawableCompat.setTint(bmpDrawable, Color.BLACK)
Bitmap tintedBmp = bmpDrawable.bitmap

How to add small bitmap in front of large bitmap image

we need to merge two images with background large image and in front of that a small bitmap and I am using this code then the result is like this what is wrong and how to solve this problem. here the image of the small image is not visible but in small image place a large image is placed
**
Java code
**
Bitmap backgroundimage = BitmapFactory.decodeResource(getApplicationContext().getResources(),
R.drawable.rec_);
BitmapDrawable mBitmapDrawable= new BitmapDrawable( overlay(bitmap,backgroundimage));
mBitmapDrawable.setGravity(CENTER);
toolbar_layout.setBackground(mBitmapDrawable);
private 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, new Matrix(), null);
return bmOverlay;
}

Error writing text on image

I try to write a text on my image:
private Bitmap drawText() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.test);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(5);
canvas.drawText("Some Text here", 5, 5, paint);
Bitmap resBitmap = Bitmap.createBitmap(canvas.getWidth(),
canvas.getHeight(), Config.RGB_565);
canvas.setBitmap(resBitmap);
return resBitmap;
}
the result is a completely black image. What am I doing wrong ?
Disclaimer: I'm not an Android developer. I've never written code like this. It's just my interpretation of the documentation...
I suspect you actually want to set the bitmap to draw onto much earlier, and then draw the other bitmap into the canvas. So something like:
private Bitmap drawText() {
// Load the existing image to get some dimensions
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.test);
// Create a result bitmap and a canvas which draws onto it
Bitmap resBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.RGB_565);
Canvas canvas = new Canvas(resBitmap);
// Draw the existing image into the canvas
canvas.drawBitmap(bitmap, 0f, 0f, null);
// Draw text on top
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(5);
canvas.drawText("Some Text here", 5, 5, paint);
return resBitmap;
}
u set background of canvas from this
paint.setColor(Color.BLACK);
and by default text color is also black i.e u can see only that black screen
try from this
paint.setColor(Color.GREEN);
change the Background color and foreground color of the canvas.
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
canvas.drawPaint(paint);
paint.setColor(android.R.color.black);
paint.setTextSize(20);
canvas.drawText("Some Text", 10, 25, paint);
ensure that the color of the text must be different from the background color

Create a removable canvas overlay

Im having two bitmaps as I want to blend together.
Im using a canvas to achieve this. The following code will create a resulting image where the mask is 50% blended into to background.
Bitmap output = Bitmap.createBitmap(picture.getWidth(),
picture.getHeight(), Config.ARGB_8888);
Paint p = new Paint();
Paint maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
maskPaint.setAlpha(127);
Canvas c = new Canvas(output);
c.drawBitmap(picture, 0, 0, p);
c.drawBitmap(mask, 0, 0, maskPaint);
return output;
I have also been expermenting if im able to remove parts of the bitmap, using Xfermode. I have done this with the following code:(This will create a hole, a square)
int height = BitmapHandler.getMainBitmap().getHeight();
int width = BitmapHandler.getMainBitmap().getWidth();
Bitmap bmOverlay = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Paint p = new Paint();
p.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
Canvas c = new Canvas(bmOverlay);
c.drawBitmap(BitmapHandler.getMainBitmap(), 0, 0, null);
c.drawRect(30, 30, 100, 100, p);
return bmOverlay;
Now, Im wondering if, using a canvas, I am able to draw a background and a mask and at the same time being able to remove parts of the mask and let the background "shine" through.
Thanks!

Canvas as ImageView/Bitmap

I cant get my head around this problem I have. I have 2 images which were added to a canvas and will be treated as one object, now I need to return this canvas as bitmap/drawable, since
Here is code how I added 2 bitmaps into a canvas
Bitmap image1=BitmapFactory.decodeResource(getResources() ,R.drawable.icon1);
Bitmap image2=BitmapFactory.decodeResource(getResources() R.drawable.icon2);
Rect srcRect = new Rect(0, 0, image.getWidth(), image.getHeight());
Rect dstRect = new Rect(srcRect);
dstRect.offset(15, 0);
canvas.drawBitmap(image, srcRect, dstRect, null);
dstRect.offset(image.getWidth(), 0);
canvas.drawBitmap(image2, srcRect, dstRect, null);
//return???????????
Please someone help.
Tnx in advance!
You can create a Bitmap to draw into.
Bitmap image1=BitmapFactory.decodeResource(getResources() ,R.drawable.icon1);
Bitmap image2=BitmapFactory.decodeResource(getResources() R.drawable.icon2);
Bitmap result = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);//Create the canvas to your image
Rect srcRect = new Rect(0, 0, image.getWidth(), image.getHeight());
Rect dstRect = new Rect(srcRect);
dstRect.offset(15, 0);
canvas.drawBitmap(image, srcRect, dstRect, null); //draw on it
dstRect.offset(image.getWidth(), 0);
canvas.drawBitmap(image2, srcRect, dstRect, null);
return result;//result will have the drawed images from the canvas
Where did you get the canvas from? If from a bitmap, then that obj will now have whatever you drew on the canvas applied to it.
Canvas is just a way to draw onto a bitmap or drawable that it is backed by. So if you create a Bitmap called result and then get your canvas from that you can just return that.
As in..
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
...do your stuff...
return result;

Categories

Resources