get Default Front Facing Camera Instance - Android Camera

Android examples for Camera:Front Camera

Description

get Default Front Facing Camera Instance

Demo Code


//package com.java2s;
import android.annotation.TargetApi;

import android.hardware.Camera;
import android.os.Build;

public class Main {
    private static int mCameraId = -1;


    public static Camera getDefaultFrontFacingCameraInstance() {
        Camera camera = getDefaultCamera(Camera.CameraInfo.CAMERA_FACING_FRONT);
        if (camera != null)
            mCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
        return camera;
    }//from  w  w w  .j  a  v  a 2s  .  c  om

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    public static Camera getDefaultCamera(int position) {
        // Find the total number of cameras available
        int mNumberOfCameras = Camera.getNumberOfCameras();

        // Find the ID of the back-facing ("default") camera
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        for (int i = 0; i < mNumberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == position) {
                return Camera.open(i);

            }
        }

        return null;
    }
}

Related Tutorials