How to switch between cameras (Front, Back)?

Asked

Viewed 583 times

1

I’m making a native Android application 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).

It’s just that I’m having a little trouble doing that job. Take the camera that is being used and switch to another one, type (Front --> Back/ Back --> Front). Could 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();
                }
            }
        }
    }
  • 1

    What have you ever tried to do?

  • I’m just checking to see if there are cameras on the device, Camera.Camerainfo cameraInfo = new Camera.Camerainfo(); if(numCamera < 0){

  • After that, it was to check which camera is running (Front or Rear). Done this, put function to switch between them, using a Button, for example.

  • 1

    You can [Dit] the question by placing the relevant code.

  • In fact, I wanted to do what every time I clicked on the button, the method identified which camera is running (whether it’s the front or the back) and then switched the camera, if I was using the rear camera would switch to the front and so vice versa.

1 answer

0

I hope this helps:

Choosing the camera

A device can have multiple cameras, a camera or none. To identify the exact number it is possible to 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 need to use the overloaded open() method, which takes an int as a parameter. This parameter indicates which camera ID can vary 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();

Also, when you directly access the camera, Android prompts you to declare a specific permission in your application, as can be seen below:

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

This explanation was taken from the site: http://www.softblue.com.br/blog/home/postid/15/UTILIZANDO_CAMERAS_EM_APLICACOES_ANDROID

Browser other questions tagged

You are not signed in. Login or sign up in order to post.