0
public class MainActivity extends ActionBarActivity {
Camera cam = null;//camera object used all over
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //Added a button listener 
    Button mNext=(Button)findViewById(R.id.button1);
    mNext.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.e("Test", "one");
            FrontFacingCamera();

        }
    });

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

public Camera FrontFacingCamera() {
    //opening a camera 
    Log.e("Test", "Two");
    int cameraCount = 0;
    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);

                 Camera.Parameters params = cam.getParameters();

                 SurfaceView view = new SurfaceView(context);
                 try{
                      cam.setPreviewDisplay(view.getHolder());
                 }catch(IOException e){
                     throw new RuntimeException(e);
                 }

                 cam.startPreview();

            } catch (RuntimeException e) {
                Log.e("Test", "Camera failed to open: " + e.getLocalizedMessage());
            }
        }
    }
    return cam;
   //opened a camera
     }

}

Here I have just tried to call my method FrontFacingCamera(); then it should return me the preview. But instead it gives me nothing. I am trying to open and capture an image with my front camera but its not giving me preview that's why I am struck here.

Aayush
  • 147
  • 1
  • 12
  • have You set the uses feature in Your manifest? – Opiatefuchs Jun 11 '14 at 08:17
  • Yeah I did that. Still no effect. – Aayush Jun 12 '14 at 08:20
  • Could You check what value cameraCount is? Because, I know the most devices today have two camera. The standard back camera has number 0 and the front face camera has number 1. But if cameraCount returns 2 (because there are two cams) it couldn´t work. And why You are using a loop? Do You want to open every camera at the same time? – Opiatefuchs Jun 12 '14 at 08:53
  • ohh yeah, I figured it out. thanks for your replies. My loop was a problem. – Aayush Jun 23 '14 at 05:22

0 Answers0