Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Following code is working perfectly for camerapreview.

Layout for camera preview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:opencv="http://schemas.android.com/apk/res-auto"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.example.bakri.opencvcamera_00.MainActivity">


   <org.opencv.android.JavaCameraView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/java_camera_view"/>

 </RelativeLayout>

Code for camera preview

     @Override public class MainActivity extends AppCompatActivity implements 
       CameraBridgeViewBase.CvCameraViewListener2,View.OnTouchListener 
       { 

             private final String TAG = MainActivity.class.getSimpleName(); 
             private CameraBridgeViewBase mOpenCvCameraView;
             private Mat rgba;

             private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this)
             {
                    @Override
                    public void onManagerConnected(int status) 
                    {
                      switch (status) 
                       {
                          case LoaderCallbackInterface.SUCCESS:
                           Log.i(TAG, "OpenCV loaded successfully");

                           mOpenCvCameraView.setOnTouchListener(MainActivity.this);
                           mOpenCvCameraView.enableView();

                           break;
                           default:
                              super.onManagerConnected(status);
                              break;
                        }
                     }
              };

            @Override
            protected void onCreate(Bundle savedInstanceState)
            {
                     super.onCreate(savedInstanceState);
                     setContentView(R.layout.activity_main);

                     mOpenCvCameraView = (CameraBridgeViewBase) new JavaCameraView(this, 1);

                     mOpenCvCameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_FRONT);

                     mOpenCvCameraView.setVisibility(CameraBridgeViewBase.VISIBLE);
                     mOpenCvCameraView.setCvCameraViewListener(this);
             }
            @Override
            protected void onResume() 
            {
                   super.onResume();

                   if (!OpenCVLoader.initDebug()) 
                   {
                        Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for                     
                        initialization");
                        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this,                                                       
                        mLoaderCallback);
                   } 
                   else
                   {
                          Log.d(TAG, "OpenCV library found inside package. Using it!");
                          mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
                   }
             }
             @Override
             public void onCameraViewStarted(int width, int height)
              {
                         Log.d(TAG, "onCameraViewStarted");

              }

             @Override
             public void onCameraViewStopped() 
             {
                        Log.d(TAG, "onCameraViewStopped");
             }

            @Override
            public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
            {
                     Mat rgba = inputFrame.rgba();
                     Log.d(TAG, "onCameraFrame");
                     return rgba;
            }

            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                    Log.d(TAG, "onTouch");
                    return false;
            }
      }

Following code is working perfectly for camerapreview.

Layout for camera preview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:opencv="http://schemas.android.com/apk/res-auto"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.example.bakri.opencvcamera_00.MainActivity">


   <org.opencv.android.JavaCameraView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/java_camera_view"/>

 </RelativeLayout>

Code for camera preview

     @Override public class MainActivity extends AppCompatActivity implements 
       CameraBridgeViewBase.CvCameraViewListener2,View.OnTouchListener 
       { 

             private final String TAG = MainActivity.class.getSimpleName(); 
             private CameraBridgeViewBase mOpenCvCameraView;
             private Mat rgba;

             private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this)
             {
                    @Override
                    public void onManagerConnected(int status) 
                    {
                      switch (status) 
                       {
                          case LoaderCallbackInterface.SUCCESS:
                           Log.i(TAG, "OpenCV loaded successfully");

                           mOpenCvCameraView.setOnTouchListener(MainActivity.this);
                           mOpenCvCameraView.enableView();

                           break;
                           default:
                              super.onManagerConnected(status);
                              break;
                        }
                     }
              };

            @Override
            protected void onCreate(Bundle savedInstanceState)
            {
                     super.onCreate(savedInstanceState);
                     setContentView(R.layout.activity_main);

                      mOpenCvCameraView = (CameraBridgeViewBase) new JavaCameraView(this, 1);
findViewById(R.id.java_camera_view);

                     mOpenCvCameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_FRONT);

                     mOpenCvCameraView.setVisibility(CameraBridgeViewBase.VISIBLE);
                     mOpenCvCameraView.setCvCameraViewListener(this);
             }
            @Override
            protected void onResume() 
            {
                   super.onResume();

                   if (!OpenCVLoader.initDebug()) 
                   {
                        Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for                     
                        initialization");
                        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this,                                                       
                        mLoaderCallback);
                   } 
                   else
                   {
                          Log.d(TAG, "OpenCV library found inside package. Using it!");
                          mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
                   }
             }
             @Override
             public void onCameraViewStarted(int width, int height)
              {
                         Log.d(TAG, "onCameraViewStarted");

              }

             @Override
             public void onCameraViewStopped() 
             {
                        Log.d(TAG, "onCameraViewStopped");
             }

            @Override
            public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
            {
                     Mat rgba = inputFrame.rgba();
                     Log.d(TAG, "onCameraFrame");
                     return rgba;
            }

            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                    Log.d(TAG, "onTouch");
                    return false;
            }
      }

Following code is working perfectly for camerapreview.

Layout for camera preview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:opencv="http://schemas.android.com/apk/res-auto"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.example.bakri.opencvcamera_00.MainActivity">


   <org.opencv.android.JavaCameraView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/java_camera_view"/>

 </RelativeLayout>

Code for camera preview

     @Override public class MainActivity extends AppCompatActivity implements 
       CameraBridgeViewBase.CvCameraViewListener2,View.OnTouchListener 
       { 

             private final String TAG = MainActivity.class.getSimpleName(); 
             private CameraBridgeViewBase mOpenCvCameraView;
             private Mat rgba;

             private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this)
             {
                    @Override
                    public void onManagerConnected(int status) 
                    {
                      switch (status) 
                       {
                          case LoaderCallbackInterface.SUCCESS:
                           Log.i(TAG, "OpenCV loaded successfully");

                           mOpenCvCameraView.setOnTouchListener(MainActivity.this);
                           mOpenCvCameraView.enableView();

                           break;
                           default:
                              super.onManagerConnected(status);
                              break;
                        }
                     }
              };

            @Override
            protected void onCreate(Bundle savedInstanceState)
            {
                     super.onCreate(savedInstanceState);
                     setContentView(R.layout.activity_main);


                     mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.java_camera_view);

                     mOpenCvCameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_FRONT);

                     mOpenCvCameraView.setVisibility(CameraBridgeViewBase.VISIBLE);
                     mOpenCvCameraView.setCvCameraViewListener(this);
             }
            @Override
            protected void onResume() 
            {
                   super.onResume();

                   if (!OpenCVLoader.initDebug()) 
                   {
                        Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for                     
                        initialization");
                        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this,                                                       
                        mLoaderCallback);
                   } 
                   else
                   {
                          Log.d(TAG, "OpenCV library found inside package. Using it!");
                          mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
                   }
             }
             @Override
             public void onCameraViewStarted(int width, int height)
              {
                         Log.d(TAG, "onCameraViewStarted");

              }

             @Override
             public void onCameraViewStopped() 
             {
                        Log.d(TAG, "onCameraViewStopped");
             }

            @Override
            public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
            {
                     Mat rgba = inputFrame.rgba();
                     Log.d(TAG, "onCameraFrame");
                     return rgba;
            }

            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                    Log.d(TAG, "onTouch");
                    return false;
            }
      }

Following code is working perfectly for camerapreview.

Layout for camera preview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:opencv="http://schemas.android.com/apk/res-auto"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.example.bakri.opencvcamera_00.MainActivity">


   <org.opencv.android.JavaCameraView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/java_camera_view"/>

 </RelativeLayout>

Code for camera preview

     @Override public class MainActivity extends AppCompatActivity implements 
       CameraBridgeViewBase.CvCameraViewListener2,View.OnTouchListener 
       { 

             private final String TAG = MainActivity.class.getSimpleName(); 
             private CameraBridgeViewBase mOpenCvCameraView;
             private Mat rgba;

             private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this)
             {
                    @Override
                    public void onManagerConnected(int status) 
                    {
                      switch (status) 
                       {
                          case LoaderCallbackInterface.SUCCESS:
                           Log.i(TAG, "OpenCV loaded successfully");

                           mOpenCvCameraView.setOnTouchListener(MainActivity.this);
                           mOpenCvCameraView.enableView();

                           break;
                           default:
                              super.onManagerConnected(status);
                              break;
                        }
                     }
              };

            @Override
            protected void onCreate(Bundle savedInstanceState)
            {
                     super.onCreate(savedInstanceState);
                     setContentView(R.layout.activity_main);


                     mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.java_camera_view);

                     mOpenCvCameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_FRONT);

                     mOpenCvCameraView.setVisibility(CameraBridgeViewBase.VISIBLE);
                     mOpenCvCameraView.setCvCameraViewListener(this);
             }
            @Override
            protected void onResume() 
            {
                   super.onResume();

                   if (!OpenCVLoader.initDebug()) 
                   {
                        Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for                     
                        initialization");
                        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this,                                                       
                        mLoaderCallback);
                   } 
                   else
                   {
                          Log.d(TAG, "OpenCV library found inside package. Using it!");
                          mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
                   }
             }
             @Override
             public void onCameraViewStarted(int width, int height)
              {
                         Log.d(TAG, "onCameraViewStarted");

              }

             @Override
             public void onCameraViewStopped() 
             {
                        Log.d(TAG, "onCameraViewStopped");
             }

            @Override
            public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
            {
                     rgba = inputFrame.rgba();
                     Log.d(TAG, "onCameraFrame");
                     return rgba;
            }

            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                    Log.d(TAG, "onTouch");
                    return false;
            }
      }