Open front camera android studio 5.1

-1

I would like to know if you can guide me in the problem I have, I have an android 5.1 API level 22 device and I am trying to open the front camera from the action of a button, however, despite all the attempts I have made, I have only I managed to open the rear camera and then move to the front camera, but I want to go directly into the front camera.

To open the rear camera I have made several types of intents and these work without problems but the front camera can not open it.

Greetings

    
asked by eloy lopez 06.11.2018 в 00:00
source

1 answer

-2

REVIEW THIS VERY COMPLETE POST OF ONE OF OUR COMPANIONS.

link

Likewise, I enclose the text of this post for you to visualize, please visit this post and give the answer credits to our partner.

I add a way to do it, first add the permissions within AndroidManifest.xml:

We create the CameraView class that is based on your original code:

import android.content.Context; import android.hardware.Camera; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView;

import java.io.IOException;

public class CameraView extends SurfaceView implements SurfaceHolder.Callback {

private SurfaceHolder mHolder;
private Camera mCamera;

public CameraView(Context context, Camera camera){
    super(context);

    mCamera = camera;
    mCamera.setDisplayOrientation(90);
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    try {
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();
    } catch (IOException e) {
        Log.d("ERROR", "Camera error on surfaceCreater" + e.getMessage());
    }

}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
    if(mHolder.getSurface() == null)
        return;

    try{
        mCamera.stopPreview();
    } catch (Exception e){
        Log.e("ERROR",  e.getMessage());
    }

    try{
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();
    } catch (IOException e) {
        Log.d("ERROR", "Camera error on surfaceChanged " + e.getMessage());
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    mCamera.stopPreview();
    mCamera.release();
}

}

To load the view we create an activity, in which we first define the layout, activity_camera.xml:

<FrameLayout
    android:id="@+id/camera_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</FrameLayout>

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imgClose"
    android:layout_gravity="right|top"
    android:background="@android:drawable/ic_menu_close_clear_cancel"
    android:padding="20dp"/>

We define our activity, which must be registered within your AndroidManifest.xml:

import android.hardware.Camera; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageButton;

public class CameraActivity extends AppCompatActivity {

private static final String TAG = "CameraActivity";


private Camera mCamera = null;
private CameraView mCameraView = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

    try{
        mCamera = Camera.open();
    } catch (Exception e){
        Log.d("ERROR", "Failed to get camera: " + e.getMessage());
    }

    if(mCamera != null) {
        mCameraView = new CameraView(this, mCamera);
        FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
        camera_view.addView(mCameraView);//agrega la vista CameraView()
    }

    //boton para cerrar la aplicación.
    ImageButton imgClose = (ImageButton)findViewById(R.id.imgClose);
    imgClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Cierra la actividad.
            finish();
        }
    });

}

}

When you run our application, the camera will open:

enter the description of the image here

Finally I commented that this example uses the Camera class which is obsolete from the API 21, you can still use it, but the use of the Camera2 class is recommended

For Android 6.0 I add method to request permissions.

private void checkCameraPermission () {     int permissionCheck = ContextCompat.checkSelfPermission (             this, Manifest.permission.CAMERA);     if (permissionCheck! = PackageManager.PERMISSION_GRANTED) {         Log.i ("Message", "You do not have permission for the camera!");             ActivityCompat.requestPermissions (this, new String [] {Manifest.permission.CAMERA}, 225);     } else {         Log.i ("Message", "You have permission to use the camera.");     } }

    
answered by 06.11.2018 в 00:09