Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Camera Surface View

I am trying to create a surface view for a camera so it renders on the surface whenever is in the view of the camera. At the moment all I can see on my camera view is a black screen view. I have tried to look on Google and here but so far I haven't found what I am looking for. Anyone can suggest me some idea.

like image 304
Kitaro Avatar asked May 07 '12 12:05

Kitaro


People also ask

What is camera surface android?

Explanation: SurfaceView is a type of View which contains a SurfaceHolder. SurfaceHolder holds the surface on which we can display our media (generally frames). mCamera is a Camera object which will contains the camera instance.

What is the use of surface view in Android?

SurfaceView provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screen.

What is GL surface view?

The GLSurfaceView class provides helper classes for managing EGL contexts, interthread communication, and interaction with the activity lifecycle. You don't need to use a GLSurfaceView to use GLES. For example, GLSurfaceView creates a thread for rendering and configures an EGL context there.


1 Answers

I have written a class that can help you.

    public class Preview_can_work extends Activity {
        private SurfaceView surface_view;  
        private Camera mCamera;
        SurfaceHolder.Callback sh_ob = null;
        SurfaceHolder surface_holder        = null;
        SurfaceHolder.Callback sh_callback  = null;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            getWindow().setFormat(PixelFormat.TRANSLUCENT);

            surface_view = new SurfaceView(getApplicationContext());
            addContentView(surface_view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

            if (surface_holder == null) {
                surface_holder = surface_view.getHolder();
            }

            sh_callback = my_callback();
            surface_holder.addCallback(sh_callback);
        }

            SurfaceHolder.Callback my_callback() {      
                SurfaceHolder.Callback ob1 = new SurfaceHolder.Callback() {

                    @Override
                    public void surfaceDestroyed(SurfaceHolder holder) {
                          mCamera.stopPreview();
                          mCamera.release();
                          mCamera = null;
                    }

                    @Override
                    public void surfaceCreated(SurfaceHolder holder) {
                        mCamera = Camera.open();

                          try {
                               mCamera.setPreviewDisplay(holder);  
                          } catch (IOException exception) {  
                                mCamera.release();  
                                mCamera = null;  
                          }
                    }

                    @Override
                    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                            int height) {
                        mCamera.startPreview();
                    }
                };
                return ob1;
        }
    }

in your manifest file copy this code for camera permission

<uses-permission android:name="android.permission.CAMERA"/>

Explanation:

SurfaceView is a type of View which contains a SurfaceHolder. SurfaceHolder holds the surface on which we can display our media (generally frames).

mCamera is a Camera object which will contains the camera instance.

When you want to hold default Camera instance then you can simply call Camera.open();

Camera mCamera = Camera.open();

Now you have an open camera or you are having default camera instance. Now you need to capture frames from the camera and display it on a surface. But you cannot display it without any

surface. Here the surfaceView provides surfaceHolder and surfaceHolder provides surface to display camera frames. Now when surface will be created three callback functions will be

called.

1. public void surfaceCreated(SurfaceHolder holder)
2. public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
3. public void surfaceDestroyed(SurfaceHolder holder)

Note:- Surface will be destroyed when your application will go on pause.

surfaceCreated: surfaceCreated is a callback function which will be called when your surface will be created. In this, you can open your camera and set other attributes.

surfaceChanged: This will be called atleast one time when your surface will be created. After that it will be called whenever your surface will change(In device rotation). Here you can

start your preview because your surface have already created.

surfaceDestroyed: This will be called every time when your surface will destroy. Now if you dont have surface then where you can display you camera frames so I have released camera by using

mCamera.release(). This is very important because if your activity will be on pause and any other activity tries to open camera then it will not able to open it as you have

already open camera. Camera is a shared resource so one time only one application can use it. So remember one thing whenever you open a camera then always release it.

stopPreview: When you start preview then your camera starts capturing your frames and display it on a surface. Now if your surface have destroyed then you need to stop capturing frames

from camera so you have to call mCamera.stopPreview.

like image 90
Bharat Sharma Avatar answered Oct 04 '22 08:10

Bharat Sharma