Ask Your Question
1

Is it possible to use front camera with 2.4.2 for android?

asked 2012-07-23 20:37:57 -0500

mandroid gravatar image

For face detection I would like to use front facing camera, is it possible?

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
4

answered 2012-07-23 20:44:08 -0500

mandroid gravatar image

updated 2012-07-24 06:27:26 -0500

Andrey Pavlenko gravatar image

Found it opencv.willowgarage.com/wiki/Android2.3.0

mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID+1); // "+1" for front camera

edit flag offensive delete link more
2

answered 2013-03-16 05:12:32 -0500

Barry Thomas gravatar image

updated 2013-03-18 06:30:21 -0500

I was looking for the same thing and @StevenPuttemans directed me here. This is another solution, based on the format of the example apps in 2.4.4:

In your layout, use two views, initially both invisible, at the same location on the screen. One uses the default camera, one the second. This code assumes you have two cameras and that neither are locked or otherwise unavailable. You'll need to add your own code to check to see if a second camera is available and if not, don't allow this code to run:

<LinearLayout
  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:layout_width="match_parent"
  android:layout_height="match_parent" 
  android:screenOrientation="landscape"
  >

  <barry.opencvd2.OpenCVDemo2View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:visibility="gone"
    android:adjustViewBounds="true"
    android:gravity="center_vertical"
    android:id="@+id/java_surface_view0" />

  <barry.opencvd2.OpenCVDemo2View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:visibility="gone"
    opencv:camera_id="1" 
    android:adjustViewBounds="true"
    android:gravity="center_vertical"
    android:id="@+id/java_surface_view1" />

</LinearLayout>

In your onCreate, get a handle to the two views and set up listeners for both cameras:

@Override
public void onCreate(Bundle savedInstanceState) {
  //Log.d(TAG, "called onCreate");
  super.onCreate(savedInstanceState);
  getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

  setContentView(R.layout.tutorial2_surface_view);

  mOpenCvCameraView0 = (OpenCVDemo2View) findViewById(R.id.java_surface_view0);
  mOpenCvCameraView1 = (OpenCVDemo2View) findViewById(R.id.java_surface_view1);

  mOpenCvCameraView0.setVisibility(SurfaceView.VISIBLE);
  mOpenCvCameraView0.setCvCameraViewListener(this);
  mOpenCvCameraView0.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

  mOpenCvCameraView1.setVisibility(SurfaceView.GONE);
  mOpenCvCameraView1.setCvCameraViewListener(this);
  mOpenCvCameraView1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

  OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_4, this, mLoaderCallback);
  }

Also make sure the views are disabled in onPause and onDestroy:

@Override
public void onPause() {
  super.onPause();
  if (mOpenCvCameraView0 != null) 
      mOpenCvCameraView0.disableView();
  if (mOpenCvCameraView1 != null) 
      mOpenCvCameraView1.disableView();
  }

public void onDestroy() {
  super.onDestroy();
  if (mOpenCvCameraView0 != null) 
      mOpenCvCameraView0.disableView();
  if (mOpenCvCameraView1 != null) 
      mOpenCvCameraView1.disableView();
  }

In my menu I have an option for swapping cameras, here's what it does:

if (item.getItemId() == R.id.swapcameras) {
  if (iCamera == 0) {
    mOpenCvCameraView0.setVisibility(SurfaceView.GONE);
    mOpenCvCameraView0.disableView();
    mOpenCvCameraView1 = (OpenCVDemo2View) findViewById(R.id.java_surface_view1);
    mOpenCvCameraView1.setCvCameraViewListener(this);
    mOpenCvCameraView1.setVisibility(SurfaceView.VISIBLE);

    iCamera = 1;
    }
  else {
    mOpenCvCameraView1.setVisibility(SurfaceView.GONE);
    mOpenCvCameraView1.disableView();
    mOpenCvCameraView0 = (OpenCVDemo2View) findViewById (R.id.java_surface_view0);
    mOpenCvCameraView0.setCvCameraViewListener(this);
    mOpenCvCameraView0.setVisibility(SurfaceView.VISIBLE);

    iCamera = 0;
    }
}

If anyone wants the entire source code, just let me know.

edit flag offensive delete link more

Comments

can you please provide the Source code to abhishek.working@gmail.com

bunta gravatar imagebunta ( 2013-10-04 12:21:39 -0500 )edit

please can you send me the source code on rawan.taban94@gmail.com

rawantaban gravatar imagerawantaban ( 2017-09-20 09:51:20 -0500 )edit

Hi Mr Barry, can you please send me the source code of your answer to david.coronel@outlook.com? I've been breaking my head to use the FaceDetection classification with the front camera in a correct orientation, but not able to make it work.

Any help would be really appreciated. Thanks!

David C gravatar imageDavid C ( 2017-11-03 19:53:38 -0500 )edit
0

answered 2014-03-09 21:39:44 -0500

barry thomas: can you give me the source code for this tutorial, i try to make camera front, please

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2012-07-23 20:37:57 -0500

Seen: 4,521 times

Last updated: Mar 09 '14