How do I switch between the cameras (Front, Back)?

1

I'm doing a native Android app, using the device's cameras. After I start the first Activity, calling the camera (Back), I created a button, with the function of switching between the cameras (Front, Back).

I just have a little difficulty doing this. They take the camera that is being used and switch to another type (Front -> Back / Back -> Front). Could you help me solve this problem?

Code below - >

public void cameraFrontal(View view){
        int numCamera = Camera.getNumberOfCameras();
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        if(numCamera < 0){
            Log.i("Script", "Nenhuma câmera encontrada");
        }else {
            if(cameraManager.isOpen() == true && numCamera == 2){
                Log.i("Script", "Camera --> " + cameraInfo.facing);
                if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                    Toast.makeText(this, "Chamada da Câmera Frontal", Toast.LENGTH_LONG).show();
                }else if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                    Toast.makeText(this, "Chamada da Câmera Traseira", Toast.LENGTH_LONG).show();
                }
            }
        }
    }
    
asked by anonymous 18.08.2016 / 03:46

1 answer

0

I hope this helps:

Choosing the camera

A device can have multiple cameras, one camera, or none. To identify the exact number you can make the following call (available from Android 2.3):

int qtde = Camera.getNumberOfCameras();

The first step to working with a camera is to get an instance of a Camera object (class present in the android.hardware.Camera package). This can be done this way:

 Camera camera = Camera.open();

This call returns the reference to the device's rear camera (returns null if it does not exist). To reference other cameras, you must use the overloaded open () method, which receives an int as a parameter. This parameter indicates which camera ID, which can range from 0 to the number of cameras subtracted from 1.

An important detail regarding the camera is that it should be released after use. This is done through the following call:

 camera.release();

In addition, when you directly access the camera, Android requests that you declare a specific permission in your application, as can be seen below:

<uses-permission android:name="android.permission.CAMERA" />

This explanation has been removed from the site: link

    
18.08.2016 / 14:30