Ask Your Question
0

How to display Android camera preview[SOLVED]

asked 2016-10-07 03:55:08 -0600

AMEMB gravatar image

updated 2019-12-13 21:21:51 -0600

supra56 gravatar image

I am trying to open the Android camera using Opencv. I referred to some posts such as: http://stackoverflow.com/questions/17... and http://docs.opencv.org/2.4/doc/tutori... and as some of these posts suggested, I created the below posted layout with "org.opencv.android.CameraBridgeViewBase" item as shown below in the layout section.

At run time, if the following line was active the App crashs and logCat says: "java.lang.InstantiationException: Can't instantiate abstract class org.opencv.android.CameraBridgeViewBase" setContentView(R.layout.activity_main);

When I comment that line out as shown in the code in onCreate() method and run the code, logCat displays the following message:

Log.d(TAG, "onCameraFrame");

which means the Camera is previewing, but actually there is nothing displayed on the screen, the screen is black.

please let me know why "setContentView(R.layout.activity_main);" causes the App to crash and how to preview the camera display correctly

layout:

<?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.CameraBridgeViewBase
    android:id="@+id/cameraBridgeViewBase_surfaceView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:visibility="gone"
    opencv:show_fps="true"  opencv:camera_id="any" />
</RelativeLayout>

code:

@Override public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2,View.OnTouchListener {
    private final String TAG = MainActivity.class.getSimpleName();
    private CameraBridgeViewBase mOpenCvCameraView;

    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 = (CameraBridgeViewBase) findViewById(R.id.cameraBridgeViewBase_surfaceView);
        mOpenCvCameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_FRONT);
        setContentView(mOpenCvCameraView);
        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) {
        Log.d(TAG, "onCameraFrame");
        return null;
    }

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

}
edit retag flag offensive close merge delete

Comments

for external camera,What is the camera id ? plz help me...

Abdur Rahman gravatar imageAbdur Rahman ( 2019-05-13 12:34:35 -0600 )edit

Why do you return null for onCameraFrame? What if you return inputFrame.rgba()?

sheep94lion gravatar imagesheep94lion ( 2019-12-12 23:39:57 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-12-13 00:40:00 -0600

Akhil Patel gravatar image

updated 2019-12-13 03:25:38 -0600

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;
            }
      }
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-10-07 03:55:08 -0600

Seen: 5,143 times

Last updated: Dec 13 '19