0

I am trying to create a Camera that defaults to the back camera (rather then the front) if it available, but I cannot get a preview to display. I have seen many guides on SO (and followed some of them to create what I have now) as well as the Android api, yet I couldnt find an easy way to specify which camera to use. Can someone give me a way to do that, or tell me what is wrong with the code below?

public Camera backCam()
{
    Camera cam;
    try{
        cam=Camera.open(0);
        return cam;
    }catch(Exception e){}
    try{
        cam=Camera.open(1);
        return cam;
    }catch(Exception e){}
cam=Camera.open();
return cam;
}
public void sneakyCamera()
{
    try{
        Log.i("aaa","init camera. total cams: "+Camera.getNumberOfCameras());
        Camera cam=backCam();

    SurfaceView view=new SurfaceView(getApplicationContext());
    cam.setPreviewDisplay(view.getHolder());
    cam.startPreview(); 


        Camera.PictureCallback mCall = new Camera.PictureCallback()
        {
            @Override
            public void onPictureTaken(byte[] data, Camera camera)
            {
                File picFile = new File("/mnt/sdcard/","psychPic"+getTime()+".jpg");
                FileOutputStream fos;
                try {
                    fos = new FileOutputStream(picFile);
                    fos.write(data);
                    fos.close();
                }  catch (IOException e) {
                    Log.i("aaa","pic tken error: "+e);
                }
            }
        };

    //shuttr callback, pic callback raw, pic callback 
    cam.takePicture(null, null,mCall);
    }catch(Exception e)
    {
        Log.i("aaa","Camera error: "+e);
    }
}
Rilcon42
  • 9,584
  • 18
  • 83
  • 167
  • Looks like you're way off track; do have a `CameraPreview` class or similar? Your `backCam()` method looks deadly. Also, I think most devices default to the back cam; maybe not?. – ChiefTwoPencils Jul 15 '14 at 18:10
  • @ChiefTwoPencils, The code I have above is all I thought I needed. I do not have a CamerPreview class because I thought the preview was handled by the setPreview() and startPreview() methods – Rilcon42 Jul 15 '14 at 18:16
  • You are getting a preview but it's the wrong side; right? I did it following some tuts as well but the ones I followed had me writing -> `class CameraPreview extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback`, that may not be necessary but will surely clean up your code. What looks weird to me is the callback in that method. – ChiefTwoPencils Jul 15 '14 at 18:20
  • Im not getting a preview at all with this code (I will edit the question above to make that more clear). I implemented the callback that way because that is how I saw it in a previous SO post... If I can find the post again I will link it – Rilcon42 Jul 15 '14 at 18:31
  • This is a common misunderstanding with Camera API. cameraId counts the cameras (0, 1, ..., numberOfCameras-1), but the value of CAMERA_FACING_BACK has no relation to this, and just happens to be 0. – Alex Cohn Jul 15 '14 at 18:36
  • Thanks, I will update that in my code. – Rilcon42 Jul 15 '14 at 18:38
  • OK, that doesn't surprise me. I started with the [`API Guide`](http://developer.android.com/guide/topics/media/camera.html) and altered and added to fit my needs. You can use this as the basis of your design. – ChiefTwoPencils Jul 15 '14 at 18:47
  • Chief, that is one awesome guide.... can't believe I missed it. I will post a solution when I finish it. Thanks! – Rilcon42 Jul 15 '14 at 20:20

1 Answers1

0

This sneakyCamera() is probably called from Activity onCreate(). This method creates a view, but to see preview, you have to add this view to the window hierarchy.

The easiest fix would be to add

 parentActivity.setContentView(view);

before the call

cam.setPreviewDisplay()
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • I wound up writing my own Preview class before I saw your answer. I am going to post my code as the answer even though you may be right I no longer have the original code to check it against. – Rilcon42 Jul 23 '14 at 21:36