To draw picture on canvas - android

I have written the code to draw the text on image its working fine I am capturing the image in potrait mode but application crashes when I am capturing the image in landscape mode,I am getting exception Java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
Canvas canvas = new Canvas(photo);
Typeface tf = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Style.FILL);
paint.setTypeface(tf);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(12);
canvas.drawBitmap(photo, 0, 0, paint);
canvas.drawText(topaste, 10, 115, paint);
image.setImageBitmap(photo);

Basically the canvas object needs a fresh bitmap to draw to, passing in your immutable image defeats the point of the later draw operation. The following code creates a new bitmap for the canvas. You will need to replace the width and height variables to match your use case:
Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
Canvas canvas = new Canvas(photo);

Related

Rotate only text not whole canvas

I'm trying to draw text and draw bitmap via canvas, I want to rotate only text but whole canvas is rotating, any ideas?
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(100, 100, conf);
Canvas c = new Canvas(bmp);
Paint color = new Paint();
color.setTextSize(35);
color.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
color.setColor(Color.BLUE);
canvas.drawBitmap(drawable.getBitmap, 0, 0, null);
canvas.drawText("Test", 30, 30, color);
return bmp;
I have google map and marker method setRotation rotates bmp
You want to call canvas.save() before you apply your transformation to the Canvas. When you are done drawing your text you call canvas.restore(). Calling restore() returns the Canvas to the state it had at the time you called save().

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.drawBitmap() ignoring the tile mode of the paint

I want to draw subparts of a bitmap, but at a different size. If the size is bigger than the source rectangle in the bitmap, then I want that section of the bitmap to tile to fill the destination area. However, instead of getting tiled they are getting stretched.
I set up all the variables as follows:
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Rect srcRect = ...
Rect dstRect = ...
Paint p = new Paint();
p.setShader(new BitmapShader(b, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT));
And then in the draw() method I draw as follows:
canvas.drawBitmap(b, srcRect, dstRect, p);
What am I doing wrong? How should I draw srcRect to dstRect such that my subpart of the bitmap gets tiled?
I discovered the problem, put succinctly: shaders on bitmaps don't work that way.
To draw a rectangle tiled with a specific Bitmap you have to use Canvas.drawRect(), with a Paint that has a BitmapShader. However, Android dev can't ever be as simple as that.
First you have to cut out the srcRect to a separate Bitmap (caching this somewhere since I don't think this is a cheap operation), like so:
Bitmap t = Bitmap.createBitmap(b, srcRect.left, srcRect.top, srcRect.right-srcRect.left, srcRect.bottom-srcRect.top);
Then you have to create the Paint and the BitmapShader:
BitmapShader bs = new BitmapShader(t, TileMode.REPEAT, TileMode.REPEAT);
Paint p = new Paint();
p.setShader(bs);
Then you can finally draw to the destination rectangle, but first you have to set up a translation matrix for the shader or else it won't start from the correct place and might bugger up completely if your tile mode is CLAMP:
Matrix m = new Matrix();
m.postTranslate(dstRect.left, dstRect.right);
p.getShader().setMatrix(m);
canvas.drawRect(dstRect, p);

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.

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

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);
}

Categories

Resources