Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to display Android camera preview

I am trying to open the Android camera using Opencv. I referred to some posts such as: http://stackoverflow.com/questions/17165777/open-the-android-native-camera-using-opencv and http://docs.opencv.org/2.4/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#dev-with-ocv-on-android 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;
}

}

How to display Android camera preview

I am trying to open the Android camera using Opencv. I referred to some posts such as: http://stackoverflow.com/questions/17165777/open-the-android-native-camera-using-opencv and http://docs.opencv.org/2.4/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#dev-with-ocv-on-android 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;
}

}