Android Camera Surface View

前端 未结 2 637
感情败类
感情败类 2020-12-05 05:38

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 blac

相关标签:
2条回答
  • 2020-12-05 06:13

    Make shure you added the permission :

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

    Also these window properties:

    getWindow().setFormat(PixelFormat.TRANSLUCENT);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                     WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    

    Post some code if that doesn't work in order to help you

    0 讨论(0)
  • 2020-12-05 06:15

    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.

    0 讨论(0)
提交回复
热议问题