5

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){
    }
}

}

elichai2
  • 1,365
  • 3
  • 14
  • 28

2 Answers2

10

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().

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • Thanks!!!!,i want to draw 2 lines on the camera preview in the center of the screen from top to bottom how i do that? – elichai2 Jan 18 '13 at 13:04
  • 1
    @elichai2 That is unrelated to your current problem. Please open a new question for that, and let us know what you've tried. – Raghav Sood Jan 18 '13 at 13:06
0

You can try this:

private Camera openFrontFacingCameraGingerbread() 
{
    int Count = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for ( int camIdx = 0; camIdx < Count; 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 you also need to add permission in menifest file for camera.

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
baldguy
  • 2,090
  • 1
  • 16
  • 25