can an image be manipulated using canvas - android

Is it possible to manipulate images using canvas? How do we get the image onto the canvas?
#Override
protected void onDraw(Canvas canvas) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Bitmap mBitmap = bitmap.copy(bitmap.getConfig(), true);
canvas = new Canvas(mBitmap);
Matrix matrix = new Matrix();
canvas.drawBitmap(mBitmap, matrix, mPaint);
}
I'm unable to see the image on the screen. canvas.drawBitmap() shouldn't be necessary since I'm using the constructor and passing mBitmap.

Yes you can, but you don't "get the image onto the Canvas," a Canvas is just an interface to draw onto a Bitmap. To do so, simply create a Canvas and give a reference to your mutable Bitmap:
Canvas c = new Canvas(myBitmap);
Very easy :)

You shouldn't do this in your onDraw method. Try to create an ImageView that should display the image and then set the bitmap via setImageBitmap(bitmap).
In general you are doing somethings wrong in your code.
Loading an image every time something should be drawn to the screen will slow down your application a lot. Try to load the image only once and then just edit and set it to the imageview.
You are assigning a new Canvas to the parameter you got. But this won't change anything on the screen. Think of it like somebody puts a paper in front of you and says:"Please write your message on the paper". Now you take another blank paper and write on it. But the other person only knows about his paper and will use this paper to read your message.

Related

How to draw an image on top a bitmap and combine as one bitmap in Android Java

I am developing an Android application using Java. I am doing some image manipulating in my application. First of all, I like to save I have no knowledge about image processing. But, I am trying to get into it. What I would like to do now is draw a simple image on a bitmap and save it as one bitmap.
I am loading an image from asset folder as a bitmap like this.
Bitmap rectBitmap = BitmapFactory.decodeStream(istr);
Let's say the photo is a just simple rectangle like this.
Then I would like to draw a bitmap (triangle shape) using coordinates point. The image would be some this.
My imagination of code would be like this.
rectBitmap.drawOnTop(coorPointOneValues, coorPointTwoValues, coorPointThreeValues);
Coordinate point values would be x and y value since I am working on the 2D coordinate system.
Then I would like to save the image something like this after drawing traingle
rectBitmap = rectBitmap.saveBitmap();
How can I do it? The scenario mentioned is a possible way that I can think of. If it is not possible, what would be the other way around?
You can do it like this.
1, read or create your bitmap:
Bitmap rectBitmap = BitmapFactory.decodeStream(istr);
Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2, create a canvas on the bitmap:
Canvas canvas = new Canvas(bitmap);
3, draw something:
canvas.drawColor(Color.RED)
canvas.drawRect / canvas.drawLine / canvas.drawArc ...
//for triangle shape you can use drawPath
4, save the bitmap:
bitmap.compress(CompressFormat format, int quality, OutputStream stream)

Canvas to Bitmap

My program is a simple flood-fill game application for android, namely, the user can paint and draw something on the canvas. Now I want to provide a sharing option to the users. I guess that I must begin with copying the canvas to a bitmap object. I could not find a satisfactory answer because it is generally suggested "to create a new canvas, then..." but I got a canvas like that,
Canvas canvas = holder.lockCanvas();
then I use it. So, how can I copy my current canvas to a bitmap object?
Thanks
As suggested Maulik.J, I looked at converting a canvas into bitmap image in android again.
I could not understand from a bit close expression of this link. But, then I saw below text taken from http://developer.android.com/guide/topics/graphics/2d-graphics.html#draw-with-canvas . So, I solved this problem with the help of Maulik.J and the following text:
The Bitmap is always required for a Canvas. You can set up a new Canvas like this:
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
Now your Canvas will draw onto the defined Bitmap. After drawing upon it with the Canvas, you can then carry your Bitmap to another Canvas with one of the Canvas.drawBitmap(Bitmap,...) methods. It's recommended that you ultimately draw your final graphics through a Canvas offered to you by View.onDraw() or SurfaceHolder.lockCanvas()
I created a second Canvas object having a bitmap which has same dimensions with my real canvas, I drew all my illustrations on this second temporary canvas. Then, I drew its bitmap on my real canvas.
Thanks.

Android creating a circle bitmap without drawing it on canvas

My question is: is there any way to create a circle and then make it bitmap and not to display it on canvas??For example make a black circle then convert it to a bitmap but not using the canvas.setBitmap(Bitmap bitmap).Thanks in advance.
You can easily get a bitmap object using Bitmap static method (Bitmap.create) and then use that object to draw anything on it by instantiating a canvas object with bitmapObject.
your code should look something like this:
Bitmap bitmap = Bitmap.create(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
//draw on your canvas.

create a bitmap with a bitmap inside

I know - the title may sounds strange. Let me explain:
I created an image to show you, what I'm talking about:
I got an image (Bitmap (1)), that as the size of 150w/200h.
Now I need to make the bitmap bigger ((2) 400w/400h), but the original image must have the same size. So that the image is embedded in white background.
I think one way to solve it is this:
* create a big bitmap
* create a canvas for it
* draw the original bitmap on the canvas
* draw the canvas
* generate a bitmap of the canvas
The problem for me is, that it must be done in a background thread without drawing a view.
I hope you understand me.
You can use the code bellow to achive it. Where smallBitmap is your original image and bigBitmap is the final image:
Bitmap bigBitmap = Bitmap.createBitmap(width, height , Bitmap.Config.ARGB_8888);
canvas = new Canvas(bigBitmap);
canvas.drawBitmap(smallBitmap, left, top, new Paint());
Regards.
This should do the trick.
Create a thread and in that thread object:
Create a new bitmap.
Create a canvas based on that bitmap.
Draw your bitmap to that canvas
and voila!
I hope this helps.

how to convert a paint image to bitmap

I am able to save the source image but not able save the image with colorfilter:
paint.setColorFilter(new ColorMatrixColorFilter(cm));
If this image is converted into bitmap it can be saved easily,
but I don't know how to do this. Is there anyone to give solution?
Have your original bitmap.
Create a new clean Bitmap that has the same width/height as your original bitmap.
Create a new Canvas using this clean Bitmap.
Set your paint object, etc for this new Canvas.
Draw your original bitmap into this new Canvas.
Since this new Canvas is backed by a Bitmap (point 3.), any drawing you do in this Canvas will become part of the new Bitmap (point 2.). Now just call 'compress' on this Bitmap from point 2. and the bitmap will be saved as a jpg/png.

Categories

Resources