Android Camera2 front camera

前端 未结 4 1577
刺人心
刺人心 2021-02-19 02:46

I recently noticed that the Camera API is deprecated and I found the new API called Camera2.

I have read the documentation but I don\'t really understand it.

So

4条回答
  •  野的像风
    2021-02-19 03:15

    We can get the Characteristics of the cameras in our device

       private void getCameraCharacteristics (){
           try {
           CameraManager manager=(CameraManager)getSystemService(Context.CAMERA_SERVICE);
               for(String id : manager.getCameraIdList()){
                   Log.e(TAG, "Camara: Id " + id );
                   CameraCharacteristics cameraCharacteristics = manager.getCameraCharacteristics(id);
                   List> listaCaracteristicas = cameraCharacteristics.getKeys();
                   for(CameraCharacteristics.Key caracteristica : listaCaracteristicas){
                       Log.i(TAG,  "caracteristic: " + caracteristica.getName()  + " : " + cameraCharacteristics.get(caracteristica));
                   }
                   Log.i(TAG, listaCaracteristicas.toString());
               }
           } catch (CameraAccessException e) {
               e.printStackTrace();
           }
    
       }
    

    one of this characteristics is android.lens.facing, so based on this value we can get the Frontal camera:

     if(cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT){
                  //Frontal camera
      }
    

    This is a method to get this value:

    private String getIdFrontalCamera () {
        try {
            CameraManager manager=(CameraManager)getSystemService(Context.CAMERA_SERVICE);
            for(String id : manager.getCameraIdList()){
                CameraCharacteristics cameraCharacteristics = manager.getCameraCharacteristics(id);
                //Seek frontal camera.
                if(cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT){
                    Log.i(TAG, "Camara frontal id " + id);
                    return id;
                }
            }
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
        return "0";
    }
    

    most of the times the id of the frontal camera is 1.

提交回复
热议问题