Front face camera android. [duplicate]

2019-04-15 23:20发布

问题:

This question already has an answer here:

  • How do I open the “front camera” on the Android platform? 8 answers

I am developing a demo in which I have to check if device has front camera if device has front camera then I have to open it to capture a image.

I searched a lot but didn't find any solution. Please help me by giving a solution.

Thanks in advance.

回答1:

My solution for use front face camera :

private Camera openFrontFacingCameraGingerbread() {
    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("Your_TAG", "Camera failed to open: " + e.getLocalizedMessage());
            }
        }
    }
    return cam;
}

Complete tutorial for use camera -> HERE

And my result in picture :