Overlay images onto Camera preview SurfaceView

后端 未结 5 851
挽巷
挽巷 2020-11-28 02:51

I have a SurfaceView that is being used to draw images, and I would like to overlay them onto a live-feed from the phone\'s camera.

Currently, the

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 03:35

    I have had success with the following approach.

    First make a layout xml file that looks something like this (note the order of the two views):

    
    
        
        
    
        
        
    
    
    

    OverlayView is a subclass of SurfaceView with the drawing and animation thread implementations. The other SurfaceView will be the surface that handles the Camera preview. Inside of onCreate you should set up your views like this:

        mView = (OverlayView)this.findViewById(R.id.overlay);
        mView.getHolder().setFormat(PixelFormat.TRANSLUCENT); 
    
        mSurfaceView = (SurfaceView)this.findViewById(R.id.surface);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    

    You should add a SurfaceHolder.Callback implementation to the SurfaceHolder of mView that handles the animation thread. An example of implementing this within the subclass and using animation/drawing threads can be found in the old LunarLander example here: http://developer.android.com/resources/samples/LunarLander/src/com/example/android/lunarlander/LunarView.html

    Besides that you set up the camera SurfaceView the same way as this example: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html

提交回复
热议问题