How to draw overlay on SurfaceView used by camera on Android?

I have a simple program that draws a Camera preview in SurfaceView . What I'm trying to do is use the onPreviewFrame method, which is called every time a new frame is pulled into the SurfaceView , to execute the invalidate method, which should call the onDraw fact, the onDraw method onDraw called, but nothing is printed (I think preliminary viewing the camera overwrites the text I'm trying to draw).

This is a simplified version of the SurfaceView subclass:

 public class Superficie extends SurfaceView implements SurfaceHolder.Callback { SurfaceHolder mHolder; public Camera camera; Superficie(Context context) { super(context); mHolder = getHolder(); mHolder.addCallback(this); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } public void surfaceCreated(final SurfaceHolder holder) { camera = Camera.open(); try { camera.setPreviewDisplay(holder); camera.setPreviewCallback(new PreviewCallback() { public void onPreviewFrame(byte[] data, Camera arg1) { invalidar(); } }); } catch (IOException e) {} } public void invalidar(){ invalidate(); } public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { Camera.Parameters parameters = camera.getParameters(); parameters.setPreviewSize(w, h); camera.setParameters(parameters); camera.startPreview(); } @Override public void draw(Canvas canvas) { super.draw(canvas); // nothing gets drawn :( Paint p = new Paint(Color.RED); canvas.drawText("PREVIEW", canvas.getWidth() / 2, canvas.getHeight() / 2, p); } } 
+61
android camera surfaceview
May 29 '10 at 5:05 a.m.
source share
3 answers

SurfaceView probably doesn't work like a regular View in this regard.

Instead, follow these steps:

  • Place your SurfaceView inside a FrameLayout or RelativeLayout in your layout XML file, since both of them allow you to stack widgets on the Z axis
  • Move the drawing logic to a separate custom View class
  • Add an instance of a custom class view to the XML layout file as a child of a FrameLayout or RelativeLayout , but if it appears after SurfaceView

This will cause your custom View class to float over SurfaceView .

See here for an example project that displays pop-up panels above the SurfaceView used to play videos.

+77
May 29 '10 at 11:11
source share

Try calling setWillNotDraw(false) from surfaceCreated :

 public void surfaceCreated(SurfaceHolder holder) { try { setWillNotDraw(false); mycam.setPreviewDisplay(holder); mycam.startPreview(); } catch (Exception e) { e.printStackTrace(); Log.d(TAG,"Surface not created"); } } @Override protected void onDraw(Canvas canvas) { canvas.drawRect(area, rectanglePaint); Log.w(this.getClass().getName(), "On Draw Called"); } 

and calling invalidate from onTouchEvent :

 public boolean onTouch(View v, MotionEvent event) { invalidate(); return true; } 
+16
May 16 '13 at 15:04
source share

I think you should first call the super.draw() method before doing anything in the surfaceView drawing method.

+1
Sep 20 2018-11-11T00:
source share



All Articles