Open the Android native Camera using OpenCV

前端 未结 1 1041
梦如初夏
梦如初夏 2021-01-31 12:32

I just need to know about how can I start with this actually I need to open the Android native camera using opencv.

Where can I find the related docs or any helping mat

1条回答
  •  一向
    一向 (楼主)
    2021-01-31 13:24

    Have a look at the shipped opencv samples in opencv/samples/android/, there you should be able to find a few good examples. Here is also a link to the docs that shows how to open the camera. Don't forget to request the permissions to access the camera.

    Short version, see the link for full details: Add a layout:

    
    

    Init procedure:

    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {
                    Log.i(TAG, "OpenCV loaded successfully");
                    mOpenCvCameraView.enableView();
                } break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
        }
    };
    
    @Override
    public void onResume()
    {
        super.onResume();
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
    }
    

    Implement CVFrameListener2 interface:

     private CameraBridgeViewBase mOpenCvCameraView;
    
     @Override
     public void onCreate(Bundle savedInstanceState) {
         Log.i(TAG, "called onCreate");
         super.onCreate(savedInstanceState);
         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
         setContentView(R.layout.HelloOpenCvLayout);
         mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.HelloOpenCvView);
         mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
         mOpenCvCameraView.setCvCameraViewListener(this);
     }
    
     @Override
     public void onPause()
     {
         super.onPause();
         if (mOpenCvCameraView != null)
             mOpenCvCameraView.disableView();
     }
    
     public void onDestroy() {
         super.onDestroy();
         if (mOpenCvCameraView != null)
             mOpenCvCameraView.disableView();
     }
    
     public void onCameraViewStarted(int width, int height) {
     }
    
     public void onCameraViewStopped() {
     }
    
     public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
         return inputFrame.rgba();
     }
    

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