Android Drawing text onto a bitmap - android

I'm trying to write some text across a bitmap in Android. I've followed several guides and attempted several different code variations but I either get something obscure, a blank screen or the unaltered bitmap. This is the basic idea of what I've been playing around with.
public Bitmap writeOnDrawable(Context gContext, String path, String text) {
Bitmap bitmap = BitmapFactory.decodeFile(path);
Bitmap bmOverlay = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bmOverlay);
Paint paint = new Paint();
paint.setColor(Color.CYAN);
paint.setTextSize(20);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
canvas.drawBitmap(bitmap, 0, 0, null);
canvas.drawPoint(30, 50, paint);
canvas.drawText("Text", 33, 53, paint);
return bmOverlay;
}
I pass this function the variables and then use the returned bitmap by turning it into a BitmapDrawable and making it a background of a layout. This particular version of the function returns only my initial, unaltered bitmap with no "Text" text across it.
I've attempted variations such as copying the original image:
Bitmap drawableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Drawing the original image onto the canvas:
canvas.drawBitmap(drawableBitmap, screenHeight, screenWidth, paint);

I solved the problem using android-maps-util library.
add compile 'com.google.maps.android:android-maps-utils:0.4' dependency to your gradle file.
Use IconGenerator to create bitmap with text and use it in your BitmapDescriptor to create marker on your map.

Related

Android - Black BG color issue in canvas masking

I an creating an android application in which i am going to crop a bitmap image using path in canvas.
I am able to cut the bitmap using path but it leaves black background on the remaining portion of the bitmap.
Below is my code to cut a bitmap with path and mask in canvas.
public Bitmap cropBitmap(Path path){
Bitmap maskImage = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas maskCanvas = new Canvas(maskImage);
maskCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
Paint pathPaint = new Paint();
pathPaint.setAntiAlias(true);
pathPaint.setXfermode(null);
pathPaint.setStyle(Style.FILL);
pathPaint.setColor(Color.WHITE);
maskCanvas.drawPath(path,pathPaint);
Bitmap resultImg = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas mCanvas = new Canvas(resultImg);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
mCanvas.drawBitmap(bitmap, 0, 0, null);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mCanvas.drawBitmap(maskImage, 0, 0, paint);
return resultImg;
}
and below is the input image with path.
and below is the result which i am getting right now.
I want to remove that black background portion.
that black portion should be transparent.
Is there any way i can remove that black portion and make it transparent?

Draw Text Over Bitmap

I'm trying to put text over map marker but it always appears under it.
First I convert drawable to bitmap and then draw text on it. drawable to bitmap conversion works fine, I only have a problem with text overlay.
I have already tried these:
Adding text to a bitmap in memory in Android
https://stackoverflow.com/a/7328777/3423468
https://stackoverflow.com/a/8831182/3423468
and many more with no luck.
This is my current method:
Bitmap drawableToBitmap(Drawable drawable)
{
var bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
var canvas = new Canvas(bitmap);
if (shouldDrawText)
{
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStrokeWidth(40);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern
//canvas.drawBitmap(bitmap, 0, 0, paint);
canvas.drawText("Testing...", 10, 10, paint);
}
drawable.SetBounds(0, 0, canvas.Width, canvas.Height);
drawable.Draw(canvas);
return bitmap;
}
Any ideas what I'm doing wrong?
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern
Here you use SRC_OVER, which means the source will be over the DST will be under. The DST is the new pixels to be drawn.
You should use DST_OVER to draw the new pixels on top of the old pixels.
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER)); // Text Overlapping Pattern
See here an overview of how porterduff works

Android clip Picture to Bitmap. Is Possible?

I have a Picture object, loaded from an SVG file, and I have set hardwareAccelerated=false to make it works on all devices.
Since there is a bug on android 4.0.4, I have to convert the Picture to Bitmap and I do that, in this way:
...
...
public void draw(Canvas canvas) {
...
...
//myPicture size is 9000x5000 but I want to display only this portion
clipRect.set(50, 50, 370, 530);
Bitmap bmp = getBitmapFromPicture(myPicture, clipRect);
canvas.drawBitmap(bmp, 0, 0, null);
bmp.recycle();
...
...
}
public static Bitmap getBitmapFromPicture(Picture picture, RectF clipRect) {
Bitmap bitmap = Bitmap.createBitmap(Math.round(clipRect.width()), Math.round(clipRect.height()), Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(picture);
}
Now I want to clip the Picture because I want to display only the visible screen part of it.
But the canvas.drawPicture does not accept srcRect parameter.
How is it possible to achieve this?
EDIT:
By translate the canvas: canvas.translate(-50, -50) it seems that translate the bitmap, too.
You need to set a transform on the Canvas.
To move the portion of the picture at 50,50 down so it is on the bitmap (ie. at 0,0), just do:
canvas.translate(-50, -50);
So your method becomes:
public static Bitmap getBitmapFromPicture(Picture picture, RectF clipRect)
{
Bitmap bitmap = Bitmap.createBitmap(Math.round(clipRect.width()),
Math.round(clipRect.height()),
Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
canvas.translate(-clipRect.left, -clipRect.top);
canvas.drawPicture(picture);
}

How to mask only certain parts of a bitmap in Android

I want to be able to use the following image to mask other bitmap. So basically, I'm trying to get the mask image and replace it's black content with another bitmap. Like this for example:
I'm able to use this mask and create a round version of the original bitmap, but that does not retain that orange border around it. Any thoughts on how I can accomplish this effect? Thanks.
The code that I am using (that only creates a round image) is the following:
private static Bitmap applyMask(ImageView imageView, Bitmap mainImage) {
Canvas canvas = new Canvas();
Bitmap result result = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
canvas.setBitmap(result);
Paint paint = new Paint();
// resize image fills the whole canvas
canvas.drawBitmap(mainImage, null, new Rect(0, 0, 50, 50), paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawBitmap(sMaskImage, 0, 0, paint);
paint.setXfermode(null);
return result;
}
I use below code snipped to set masked images, this sample "bm" is my bitmap and "mMaskSource" is the resource id of mask object position in my drawable folder.
Bitmap mask = BitmapFactory.decodeResource(getResources(), mMaskSource);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(),
Config.ARGB_8888);
Canvas mCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
bm = Bitmap.createScaledBitmap(bm, mask.getWidth(), mask.getHeight(),
true);
mCanvas.drawBitmap(bm, 0, 0, null);
mCanvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
And finally you can use "result" bitmap object however you wish.

Canvas to ImageView problem in Android

I want to display Canvas contents on ImageView in android,
but ImageView displaying blank.
Bitmap imgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Canvas canvas = new Canvas();
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
canvas.drawBitmap(imgBitmap, 0, 0, paint);
paint.setColor(Color.BLACK);
paint.setAlpha(100);
canvas.drawRect(0, 0, 100, 100, paint); // transparent black on image
imgView.draw(canvas);
what is the problem? and what should I do?
When ImageView.draw() is called its actually putting the contents of the ImageView into the provided canvas, you have it logically backwards here. Instead use Canvas(Bitmap) constructor instead (so your canvas will draw on to the bitmap) then ImageView.setImageBitmap() with the same bitmap given to the canvas. You can use Bitmap.createBitmap(int, int, Bitmap.Config) to create the size of Bitmap you want. And remember if you have the canvas draw outside the Bitmap's bounds it is clipped.

Categories

Resources