8

I have a Canvas object at start. I need to change color of some pixels depending on their current color. How can I do that in a proper way?

Details:

I have my own class extended from ImageView. In onDraw(Canvas canvas) method I draw something with third party class and have only Canvas object with result. I need after that change color of some pixels depending on their current color.

Kara
  • 6,115
  • 16
  • 50
  • 57
Dmytro Zarezenko
  • 10,526
  • 11
  • 62
  • 104
  • You'd need to iterate through each pixel, get its current color, and set its new color. – user1706950 Sep 28 '12 at 17:33
  • I know, and how? :) For drawing I can use `canvas.drawPoint(x, y, paint)`, but how get color of the pixel? – Dmytro Zarezenko Sep 28 '12 at 17:34
  • And BTW, I need the proper solution, I mean efficient and fastest. – Dmytro Zarezenko Sep 28 '12 at 17:35
  • How did you draw on the canvas? – Simon Sep 28 '12 at 18:00
  • Draw is not a problem, how can I get `Color` of the pixel in (x,y) of the `Canvas`? – Dmytro Zarezenko Sep 28 '12 at 18:46
  • @Simon Have added the details in question. – Dmytro Zarezenko Sep 28 '12 at 19:20
  • The reason for me asking is that I know of know way of getting a pixel from a "Canvas", since the Canvas does not actually do anything except hold the drawing calls for the backing bitmap. Since I know of no way to get the backing bitmap from the canvas, I ask how the you draw on the canvas, e.g. if you do canvas.drawBitmap(), perhaps we can work with the bitmap? – Simon Sep 28 '12 at 19:33
  • 1
    What Simon says is correct. What you want to be doing is manipulating a bitmap, then just drawing that bitmap to canvas each time. That way you can get the pixel from bitmap. The bitmap can be the same size of canvas too. So its all a given. It seems like you have the idea on how to do it. Take a look here: http://stackoverflow.com/questions/4013725/converting-a-canvas-into-bitmap-image-in-android – IAmGroot Sep 28 '12 at 19:44

3 Answers3

4

Assuming that you have android.graphics.Canvas object called canvas and X & Y are points where you want to change pixel, so here you go

Call :

canvas.drawPoint(X, Y, paint);

Here is how you initalize Object of class android.graphics.Paint i.e paint

Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);

Search more on this Link to change multiple pixels at different positions, there are lot many functions that will help you achieve what you want. Best luck :-)

Aman J
  • 1,825
  • 1
  • 16
  • 30
  • 1
    No, situation is different. I don't know X,Y. I can detect coordinates only by current pixel color. For ex. if some pixel is green then paint it as blue. – Dmytro Zarezenko Oct 18 '12 at 15:16
0

There's probably a dozen ways to do this. If you want to do the Canvas approach, there's a way to draw to a Bitmap object. You can then draw the object to another Canvas. The Bitmap object may also have functions to modify pixels.

Bitmap also lets you get a copy into a buffer, and if you know about how pixels are stored, that would be a very fast way of image manipulation. I'm not sure if Canvas itself has that

Joe Plante
  • 6,308
  • 2
  • 30
  • 23
0

I recommend looking at Faster way to set a (PNG) bitmap color instead of pixel by pixel. It has code to get and set bitmap colors pixel by pixel (in the question), as well as a suggestion for an alternative to a pixel by pixel approach (in the answer). Also possibly useful: Explanation of the method getPixels for a Bitmap in Android.

Community
  • 1
  • 1
hBrent
  • 1,696
  • 1
  • 17
  • 38