Manoj
Manoj

Reputation: 3997

Android opencv camera rotation issue in mobile device

I have developed an android application for a specific tablet device of 800 * 480 screen resolution. The project objective is to capture the image(with face detection). For this I used opencv2.4.2 sdk to capture image(used opencv face detection sample). In that device the camera available in the right bottom corner. So I captured image with the reverse portrait mode(using transpose the image).

The code snippet is below:
    @Override
    protected Bitmap processFrame(VideoCapture capture) {
        FdActivity.mSquaredImage = 0;
        resolution = 0;
        capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
        capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME);        
        Core.transpose(mRgba, mRgba);
        Core.transpose(mRgba, mGray);
        .....
        .....
        return bmp;
    }

It is working fine in android tablet.

Now I want to use my application with mobile device. So I tested it with SAMSUNG GALAXY S II.

And I got one issue.

The following code is to open the camera.
    The code snippet is below:
    public boolean openCamera() {
        Log.i(TAG, "openCamera");
        synchronized (this) {
            releaseCamera();
            mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
            if (!mCamera.isOpened()) {
                mCamera.release();
                mCamera = null;
                Log.e(TAG, "Failed to open native camera");
                return false;
            }
        }
        return true;
    }

This code opens the front camera in tablet. But in SAMSUNG GALAXY S II it opens the back camera. For this I searched in opencv.org forum and I got the following solution,

        mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID+1);

Now It opens the front camera in SAMSUNG GALAXY S II. But in the tablet I got "can't open camera" error. what may be the problem. And I tested with HTC desire android device. But it shows only blank screen(camera not opened).

The new requirement is the user hold with the position of camera in the top of the device. If they rotate the device the screen and image shouldn't rotate.

It is very urgent. Please can someone help me to fix this?

Upvotes: 1

Views: 1564

Answers (1)

Julien Hirel
Julien Hirel

Reputation: 413

Android devices can have multiple cameras (front or back facing). The best way to handle this situation is to first check the number of cameras and the way they are facing instead of using a fixed camera ID. For example if you are looking for a front-facing camera you could use the following:

Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
int cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx < cameraCount; camIdx++) 
{
  Camera.getCameraInfo(camIdx, cameraInfo);
  if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) 
  {
    mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID+camIdx);
    break;
  }
}

Upvotes: 2

Related Questions