image appears black when trying drawing a text on it using canvas - android

I have a bitmap on which I'm trying to write a text using canvas.After setting the bitmap in a canvas and doing the necessary operations(writting a text on the canvas), I draw the resulting canvas on a ImageView.The big problem is that there is no image displayed...the screen turns black.Now, I know that the bitmap is returned ok because I displayed right before doing the canvas operations.
So,here is how I did it:
image= (ImageView) findViewById(R.id.imageview);
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
int heightOfOld=bitmap.getHeight();
int widthOfOld=bitmap.getWidth();
android.graphics.Bitmap.Config hasAlpha=bitmap.getConfig();
Bitmap bitmapResult=bitmap.createBitmap(widthOfOld, heightOfOld, hasAlpha);
Canvas c=new Canvas(bitmapResult);
Canvas c1=drawTextImage(c);
image.draw(c1);
And here is the method used for drawing text on the canvas:
private Canvas drawTextImage(Canvas c){
Paint paint=new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setTextSize(20);
c.drawText("Golden Stag", 30, 200, paint);
return c;
}
Could someone tell me where is the problem,please!?

Since the documentation for draw(Canvas) says:
"Manually render this view (and all of its children) to the given Canvas."
Maybe try with:
image.setImageBitmap(bitmapResult);
"Sets a Bitmap as the content of this ImageView."
Update: Example
I think this should work (can't test it though):
image = (ImageView) findViewById(R.id.imageview);
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);
Canvas c = new Canvas(bitmap);
drawTextImage(bitmap);
image.setImageBitmap(bitmap);
private Canvas drawTextImage(Bitmap b){
Canvas c = new Canvas(b);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setTextSize(20);
c.drawText("Golden Stag", 30, 200, paint);
}

Related

How to draw a text at the corner of a bitmap android

I have a bitmap and I am trying to draw a text over the bitmap at the top right corner. But at the first place I am not able to draw any text over it. I am converting a layout to bitmap and then trying to draw text over it. But its not working out. Here is my code:
private Bitmap viewToBitmap(LinearLayout layout) {
Bitmap bitmap = Bitmap.createBitmap(layout.getWidth(), layout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
layout.draw(canvas);
canvas=new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
paint.setTextSize(14.f);
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText("Hello Android!", 0, 0, paint);
return bitmap;
}
Please pay much attention to the text position & alignment.
private Bitmap viewToBitmap(View view)
{
Bitmap result = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
view.draw(canvas);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
paint.setTextSize(14);
paint.setTextAlign(Paint.Align.RIGHT);
canvas.drawText("Hello Android!", view.getWidth(), 14, paint); // draw watermark at top right corner
return result;
}

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.

Draw a circle on an existing image

I'm trying to draw a circle on an image which placed as res/drawable/schoolboard.png. the image fills the activity background. the following does not work:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.schoolboard);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
Canvas canvas = new Canvas(bitmap);
canvas.drawCircle(60, 50, 25, paint);
ImageView imageView = (ImageView)findViewById(R.drawable.schoolboard);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(bitmap);
any help will be highly appreciated. thanks.
There are some errors in your code:
first of thing you cannot give reference Id for drawable in findViewById
so I think you mean something like that
ImageView imageView = (ImageView)findViewById(R.id.schoolboard_image_view);
schoolboard_image_view is the image id in your xml layout (check your layout for the right id)
BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.schoolboard,myOptions);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawCircle(60, 50, 25, paint);
ImageView imageView = (ImageView)findViewById(R.id.schoolboard_image_view);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap);
Please make sure to use the right image Id for:
ImageView imageView = (ImageView)findViewById(R.id.schoolboard_image_view);
First of all you need to create a new bitmap, because bitmap from BitmapFactory.decodeResource() method is immutable. You can do this with the following code:
Bitmap canvasBitmap = Bitmap.createBitmap([bitmap_width], [bitmap_height], Config.ARGB_8888);
Use this bitmap in Canvas constructor. Then draw your bitmap on canvas.
Canvas canvas = new Canvas(canvasBitmap);
canvas.drawBitmap(bitmap, 0, 0, bitmapPaint);
canvas.drawCircle(60, 50, 25, paint);
Also R.drawable.schoolboard is not a correct view id.
ImageView imageView = (ImageView)findViewById(R.drawable.schoolboard);

Android : Constructing bitmap from RECT

Can we construct a bitmap from a rect.
I draw a bitmap in a rect and want strokes drawn on the bitmap image become part of the image.
I am wondering if I can construct a bitmap from a Rect so the new bitmap has the old image and the strokes as a single image.
Thank You
You can always take a canvas to help you create an already decoded bitmap the way you want:
Bitmap originalBmp = null;//Here goes original Bitmap...
ImageView img = null;//Any imageview holder you are using...
Bitmap modifiedBmp = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);//Configure with your proper size and color
Canvas canvas = new Canvas(modifiedBmp);
//At this point the modified bitmap has the original one, starting from here, you can add any overlay you want...
canvas.drawBitmap(originalBmp, 0, 0, new Paint());
//And do all the other modifications you want here...
canvas.drawLines(new float[]{}, null);
canvas.drawCircle(x, y, radius, null);
//At this point the modified bitmap will have anything you added
img.setImageBitmap(modifiedBmp);
// IF YOU ARE OVERRIDING ONDRAW METHOD
public void onDraw(Canvas canvas){
//Here DO your DRAW BITMAP NOTE: paint must be already created...
canvas.drawBitmap(bt, 0, 0, paint);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawRect(30, 30, 80, 80, paint);
super.onDraw(canvas);
}
Regards!
Yes you can , Using canvas you can draw something on your old bimtap .
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
// do some canvas drawing
canvas.drawBitmap(bitmap, rect, rect, paint);

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