YOYOYO YOYOYO - 9 months ago 79
Android Question

Android Camera2 front camera

Hello i recently noticed that the Camera API is deprecated and i found the new API called Camera2.

I have read the documentation but i dont really understand that stuff.

So my question is how do i preview front camera with the new camera api?

Just a preview, not recording.

I want to use this new API because in the future im guessing the current Camera API will be replaced and stop working.

So i want to be prepared and just sit and watch while everyone panics. xD

Answer Source

first of all find out the id of your front camera (if it has of course)

    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
            try {
                return manager.getCameraIdList();
            } catch (CameraAccessException e) {
                return null;

than find out faceCamera like this:

CameraCharacteristics cameraCharacteristics = getCameraCharacteristics(cameraId);

        if (cameraCharacteristics == null)
            throw new NullPointerException("No camera with id " + cameraId);

        return cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT;

After all, you have to set the camera with that id:

CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
   try {
       characteristics = manager.getCameraCharacteristics(mCameraId);
   }  catch (CameraAccessException e) {

Note, these are just a tips for doing what you wanna do. For details how to start a preview, and not only, refer to: http://developer.android.com/samples/Camera2Basic/index.html