Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to show openCV native camera in Android ?

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 1.11k
    Comment on it

    In previous blog we added openCV android sdk and native libraries in our project so that we can use sdk.

    Here we will show camera in our Android app using openCV sdk.

    in your activity's xml file add this :

    <org.opencv.android.JavaCameraView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/cameraView"
            opencv:show_fps="true"
            opencv:camera_id="any" />

     

    Now in your activity first implement CvCameraViewListener2 and override its methods like this :

    public class MainActivity extends AppCompatActivity implements CvCameraViewListener2 {
    
        private static final String TAG = "MainActivity";
    
    //CameraBridgeViewBase used to load cameraview 
        private CameraBridgeViewBase mOpenCvCameraView;
    
        // These variables are used (at the moment) to fix camera orientation from 270degree to 0degree
        Mat mat1;
        Mat mat2;
        Mat mat3;
    
    //callback method of cameraview
        private BaseLoaderCallback mCallback = new BaseLoaderCallback(this) {
            @Override
            public void onManagerConnected(int status) {
                switch (status) {
                    case LoaderCallbackInterface.SUCCESS:
                    {
                        mOpenCvCameraView.enableView();
                    } break;
                    default:
                    {
                        super.onManagerConnected(status);
                    } break;
                }
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            setContentView(R.layout.xml_mainactivity);
            mOpenCvCameraView = (JavaCameraView) findViewById(R.id.cameraView);
            mOpenCvCameraView.setCvCameraViewListener(this);
        }
    
        @Override
        public void onPause()
        {
            super.onPause();
            if (mOpenCvCameraView != null)
                mOpenCvCameraView.disableView();
        }
    
        @Override
        public void onResume()
        {
            super.onResume();
            if (!OpenCVLoader.initDebug()) {
                OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mCallback);
            } else {
             mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
            }
        }
    
        public void onDestroy() {
            super.onDestroy();
            if (mOpenCvCameraView != null)
                mOpenCvCameraView.disableView();
        }
    
        public void onCameraViewStarted(int width, int height) {
    
            mat1 = new Mat(height, width, CvType.CV_8UC4);
            mat2 = new Mat(height, width, CvType.CV_8UC4);
            mat3 = new Mat(width, width, CvType.CV_8UC4);
        }
    
        public void onCameraViewStopped() {
            mat1.release();
        }
    
        public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
            mat1 = inputFrame.rgba();
            Core.transpose(mat1, mat3);
            Imgproc.resize(mat3, mat2, mat2.size(), 0,0, 0);
            Core.flip(mat2, mat1, 1 );
            return mat1;
        }
    }
    

    with this class you can implement openCV camera in your android app.

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: