I want to create a mask on camera surface view. see image below. mask is resizable. image will be clicked only by unblurred area. can anybody give idea how to create suh mask? thanks in advance.
You can't draw on the Surface of the SurfaceView that is receiving the camera preview.
You have two basic options: draw on the View part of the SurfaceView, treating it as a custom view, or create a second SurfaceView and layer it on top of the camera surface.
For the latter, you would use setZOrderMediaOverlay() to position the Surface above the camera Surface layer but below the View UI layer. You can use Canvas or GLES to draw on it. You can find an example of an activity with three SurfaceViews in Grafika's "multi-surface test".
You can draw on a surfaceview with camera preview. You need to set the
setWillNotDraw(false);
in surfaceCreated override.
See: Extended SurfaceView's onDraw() method never called
So from there on, do all your drawing in onDraw as normal.
Related
I have a function that will keep on drawing on the surfaceView to simulate the animation of a character on the surfaceView. But after adding a imageView on top of the SurfaceView, the SurfaceView stop drawing.
setZOrderOnTop(true) you have added in the surfaceview init.
because surfaceview should be always on top to draw anything,
Although after that you can't put image on top of that, for that you can use other question ref
add image to surface view in android
for that. let me know if that helps
I tried to apply some 3D transformation (such as setRotationX) on a surfaceview which is used for camera previewing, but only the frame changes and the content don't.
A SurfaceView has two parts, the Surface and the View. The Surface is a separate layer that is rendered and composited independently. The View part is, by default, a transparent rectangle that creates a "hole" in the View layer, so that you can see through the Views to the Surface behind it.
The transformation you mention (setRotationX()) is a View method, but the camera preview is sent to the Surface. That's why the frame changed but the preview itself didn't.
You can send your preview to a TextureView, which can take an arbitrary transformation matrix (setTransform()), by using the Camera.setPreviewTexture() method. Or you can send it through a SurfaceTexture to an OpenGL ES texture, which can be rendered on the SurfaceView's Surface, using whatever GLES transformations you want. For an example of the latter, see Grafika's "texture from Camera" Activity.
I'm struggling to understand the process of drawing to SurfaceView and therefore the whole Surface/Canvas/Bitmap system, which is used in Android.
I've read all articles and API documentation pages, which I was able to find on android-developers site, a few tutorials of android graphics, LunarLander source code and this question.
Please tell me, which of these statements are true, which are not, and why.
Canvas has its own Bitmap attached to it. Surface has its own Canvas attached to it.
All View's of window share the same Surface and thus share the same Canvas.
SurfaceView is subclass of View, which, unlike other View's subclasses and View itself, has its own Surface to draw in.
There is also one additional question:
Why is there a need for a Surface class, if there is already a Canvas for high-level operations with bitmap. Give an example of a situation where Canvas is non-suitable for doing work which Surface can do.
Here are some definitions:
A Surface is an object holding pixels that are being composited to the screen. Every window you see on the screen (a dialog, your full-screen activity, the status bar) has its own surface that it draws in to, and Surface Flinger renders these to the final display in their correct Z-order. A surface typically has more than one buffer (usually two) to do double-buffered rendering: the application can be drawing its next UI state while the surface flinger is compositing the screen using the last buffer, without needing to wait for the application to finish drawing.
A window is basically like you think of a window on the desktop. It has a single Surface in which the contents of the window is rendered. An application interacts with the Window Manager to create windows; the Window Manager creates a Surface for each window and gives it to the application for drawing. The application can draw whatever it wants in the Surface; to the Window Manager it is just an opaque rectangle.
A View is an interactive UI element inside of a window. A window has a single view hierarchy attached to it, which provides all of the behavior of the window. Whenever the window needs to be redrawn (such as because a view has invalidated itself), this is done into the window's Surface. The Surface is locked, which returns a Canvas that can be used to draw into it. A draw traversal is done down the hierarchy, handing the Canvas down for each view to draw its part of the UI. Once done, the Surface is unlocked and posted so that the just drawn buffer is swapped to the foreground to then be composited to the screen by Surface Flinger.
A SurfaceView is a special implementation of View that also creates its own dedicated Surface for the application to directly draw into (outside of the normal view hierarchy, which otherwise must share the single Surface for the window). The way this works is simpler than you may expect -- all SurfaceView does is ask the window manager to create a new window, telling it to Z-order that window either immediately behind or in front of the SurfaceView's window, and positioning it to match where the SurfaceView appears in the containing window. If the surface is being placed behind the main window (in Z order), SurfaceView also fills its part of the main window with transparency so that the surface can be seen.
A Bitmap is just an interface to some pixel data. The pixels may be allocated by Bitmap itself when you are directly creating one, or it may be pointing to pixels it doesn't own such as what internally happens to hook a Canvas up to a Surface for drawing. (A Bitmap is created and pointed to the current drawing buffer of the Surface.)
Also please keep in mind that, as this implies, a SurfaceView is a pretty heavy-weight object. If you have multiple SurfaceViews in a particular UI, stop and think about whether this is really needed. If you have more than two, you almost certainly have too many.
Here is a very basic and simple conceptual overview of how interaction happens among the Window, Surface, Canvas, and Bitmap.
Sometimes, a visual representation helps a lot in understanding twisted concepts.
I hope this graphic could help someone.
A Bitmap is simply a wrapper for a collection of pixels. Think of it as an array of pixels with some other convenient functions.
The Canvas is simply the class that contains all the drawing methods. It is similar to the Graphics class in AWT/Swing if you are familiar with that. All the logic on how to draw a circle, or a box, etc is contained inside Canvas. A canvas draws on a Bitmap or an open GL container but there is no reason why in the future it could be extended to draw onto other types of rasters.
SurfaceView is a View that contains a Surface. A surface is similar to a bitmap (it has a pixel store). I do not know how it is implemented but I'd imagine it is a some kind of Bitmap wrapper with extra methods for things that are directly related to screen displays (That is the reason for a surface, a Bitmap is too generic). You can get a Canvas from your Surface which is really getting the Canvas associated with the underlying Bitmap.
Your questions.
1.Canvas has its own Bitmap attached to it. Surface has its own Canvas attached to it.
Yes, a canvas operates on a Bitmap (or an open GL panel). Surface gives you a Canvas that is operating on whatever Surface is using for its Bitmap style pixel store.
2.All View's of window share the same Surface and thus share the same Canvas.
No. You could have as many surface views as you want.
3.SurfaceView is subclass of View, which, unlike other View's subclasses and View itself, has its own Surface to draw in.
Yes. Just like ListView is a subclass of View that has its own List data structure. Each subclass of View does something different.
Is it possible to draw the camera in just a portion of the SurfaceView?
or draw different things on the same SurfaceView?
What do you want to achieve by that? You cannot send the camera preview to a portion of a SurfaceView, but you can adjust the size of this View, and put other views around it or even on top of it. The latter is an alternative to drawing different things on the same SurfaceView.
I'm learning from the Documentation of Suface, Suface Flinger, SurfaceView, SurfaceHolder and Bitmap. I have found following definitions:
Surface
Surface is a collection of pixels. A Surface is an object holding pixels that are being composited to the screen. Every window you see on the screen (a dialog, your full-screen activity, the status bar) has its own surface that it draws in to, and Surface Flinger renders these to the final display in their correct Z-order. A surface typically has more than one buffer (usually two) to do double-buffered rendering: the application can be drawing its next UI state while the surface flinger is compositing the screen using the last buffer, without needing to wait for the application to finish drawing.
SurfaceView
This class is used to present a live camera preview to the user.
A SurfaceView is a special implementation of View that also creates its own dedicated Surface for the application to directly draw into (outside of the normal view hierarchy, which otherwise must share the single Surface for the window)
Bitmap
A Bitmap is a wrapper for collection of pixels,it is simply an array of pixels with some other convenient functions.
Canvas
Canvas is a class that contains all the drawing methods. It is similar to Graphics class in AWT/Swing in Java. It has all the logic on how to draw, a box etc. A Canvas operates on a Bitmap.
But I'm not clear about SurfaceHolder and SurfaceFlinger and according to above definition bitmap is also container of pixel and Surface too.
Can you help to give me clear understanding the definition of all these objects?
A Canvas is a basic context for doing drawing with the graphics API. You can create your own Canvas wrapping a Bitmap for doing off-screen drawing, and of course the UI framework will pass Canvases to widgets for them to draw themselves into. All these widgets are subclasses of View. Or, if they can contain other widgets, then they subclass from ViewGroup (which is a subclass of View).
If you want to do high-frame-rate animations, then you need to subclass from SurfaceView (or its subclass GLSurfaceView, if you want to do on-screen OpenGL-ES rendering). Each SurfaceView has a SurfaceHolder, which has lockCanvas methods that you can call at any time to get a Canvas into which to draw an updated display and post back for the user to see immediately via unlockCanvasAndPost.
For more info, see 2D graphics concepts and Custom widget tips