Getting Bitmap from Custom SurfaceView
Asked Answered
G

1

9

I have this code in a class that extends surface view and implements runnable I am able to use the class basically allows you to draw to the canvas using different colors and such. I'm trying to get a method that will allow me to save the image after it is drawn and this is the method. No matter what i do i just get a black image with nothing on it. Any ideas?

I have cache drawing enabled

Objective get a bitmap image from a custom SurfaceView I have exhausted options of looking at some other post on here and didn't find anything to work. Here is hoping that there is recently a new solution to this. Many thanks

public Bitmap getImage() {
            Bitmap bitmap = Bitmap.createBitmap(this.getWidth(),
                    this.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            this.draw(canvas);
            return bitmap;
        }
Guttapercha answered 6/1, 2013 at 11:7 Comment(9)
What do you expect this.getWidth() and this.getHeight() to give?Aerometry
You are creating a new Bitmap, that has nothing on it. Then you create a Canvas from that blank bitmap. You have not done any drawing, how can you expect a drawing? Just a note: if you are overriding the onDraw() method of the SurfaceView, be sure to call setWillNotDraw(false) in your class constructor.Aerometry
i expected to get the surfaceview width and height since that is the class i am extending.Guttapercha
What i thought was happening was that i create a new blank bitmap that has the dimension of my surfaceview. The i have a new canvas that will draw to my bitmap. Then i call this.draw to draw to my canvas which then draws to the bitmap? am i wrong in thinking? im guessing i must beGuttapercha
from developer.android.com Construct a canvas with the specified bitmap to draw into. The bitmap must be mutable. The initial target density of the canvas is the same as the given bitmap's density. Parameters bitmap Specifies a mutable bitmap for the canvas to draw into.Guttapercha
This is the definition for draw-----Manually render this view (and all of its children) to the given Canvas. The view must have already done a full layout before this function is called. When implementing a view, implement onDraw(android.graphics.Canvas) instead of overriding this method. If you do need to override this method, call the superclass version.Guttapercha
Yes, you are right. What I'm saying is, your new Bitmap is blank, hence black. Try loading an image resource to your Bitmap first, using BitmapFactory.decodeResource(). What if your code is working correctly, loading the Bitmap, but you are seeing a blank screen, because you have drawn nothing so far. Check this #3693560Aerometry
but i know i have drawn to my view because i can see the drawing that i made. My surface View has an image on it. With different colours and such but it's the retrieving the image that isn't working.Guttapercha
Finally founds a solution that works. My God will post 2moroGuttapercha
A
1

Your question became clear only by your last comment. In the code that you posted above, you are returning with return bitmap. That will return the local variable bitmap. This local variable is totally blank. You may be drawing to your bitmap or associating an image to it, somewhere else in the code. But the instance of bitmap in your above code, is blank and only local to the function. You cannot expect it to return your updated, latest bitmap.

Now, after your comment I googled "getting a bitmap of current surfaceview" and it led me to this SO answer: Create Bitmap from SurfaceView

In that question, it was apparently solved by extending View instead of SurfaceView. Drawing cache only works for View.

Update: Follow the below tutorials. Based on the code pasted by you, it's not clear what the error is. There are a lot of things that need be done for drawing to SurfaceView, and I'm not sure if you have done those, and I cannot ask for every such missing item. I followed below tutorials for my basic graphics projects. You need to read them and see if you have missed anything.

Tutorials on Canvas drawing:

Playing with Graphics in android all parts.

Android 2D

Aerometry answered 6/1, 2013 at 12:25 Comment(5)
There must be a way. I've seen others use it successfully. Just not the coded. I used a view before it didn't work because the performance of it wasn't as quick. What i tried to do now was to make a field Image and canvas. Then i placed the image into the canvas and when i call onDraw i just send in my canvas instead of the one they wanted to. Still didn't work... any idea what draws to the surfaceView and when it is called. So i can insert my canvas instead of the one that it uses.Guttapercha
For not letting SurfaceView itself draw to the canvas, and letting your own onDraw() to do the task, call setWillNotDraw(false) from your class constructor.Aerometry
Thanks ye i already included it. Sigh still can't get the image out of it. I have a log inside onDraw and im only seeing it called when i first open the application. It's not called when i am drawing to the surface view? when is it called by anychance?Guttapercha
When you call invalidate() your onDraw() is called again. What I'd suggest is, do your drawings in a new Thread. In the run() method of the Thread, you can update the Canvas and call invalidate(). This will call onDraw() and show updated canvas. run() is also called once on thread creation, so you have to implement a while(true) infinite loop inside run() to keep updating your drawing.Aerometry
I wont be able to call invalidate because only the Thread that created it will be able to access it. just triedGuttapercha

© 2022 - 2024 — McMap. All rights reserved.