Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Front Camera API

I got Activity that call another class that have a Camera Preview. The problem is that it's open the back camera and i need the front. how can i do that in default it will open the front camera(i looked in google but every thing i tried the app crashed when it opened). Here is the Activity:

package com.elichai.tfillin;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.widget.FrameLayout;

public class CameraActivity extends Activity {

   private Camera mCamera;
   private CameraPreview mPreview;

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

       mCamera = getCameraInstance();

       mPreview = new CameraPreview(this, mCamera);
       FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
       preview.addView(mPreview);
   }
   public static Camera getCameraInstance(){
       Camera c = null;
       try {
           c = Camera.open(); 
       }
       catch (Exception e){
       }
       return c; 
   }

} Here Is the other Class:

package com.elichai.tfillin;

import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;
    int cameraId=0;

   @SuppressWarnings("deprecation")
   public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);        
    }

    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
       mCamera.release();
    }

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    mCamera.setDisplayOrientation(90);
   if (mHolder.getSurface() == null){
      return;
    }

    try {
        mCamera.stopPreview();
    } catch (Exception e){
    }

    try {
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();

    } catch (Exception e){
    }
}

}

like image 730
elichai2 Avatar asked Jan 18 '13 12:01

elichai2


1 Answers

Your current Camera.open() has no parameter in it, meaning that it will open the default camera which is almost always the rear facing one.

You should iterate through the available cameras and find out the ID of the front facing one and use that to open it. Something like:

private Camera openFrontFacingCamera() 
{
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
        Camera.getCameraInfo( camIdx, cameraInfo );
        if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT  ) {
            try {
                cam = Camera.open( camIdx );
            } catch (RuntimeException e) {
                Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
            }
        }
    }

    return cam;
}

And then use it in your app as follows:

public static Camera getCameraInstance() {
   Camera c = null;
   try {
       c = openFrontFacingCamera();
   }
   catch (Exception e){
   }
   return c; 
}

Seeing as all both the methods do is open and return the Camera instance, you could easily simplify your code by directly calling openFrontFacingCamera() instead of getCameraInstance().

like image 56
Raghav Sood Avatar answered Sep 17 '22 22:09

Raghav Sood