Android Camera2 front camera

﹥>﹥吖頭↗ 提交于 2020-05-12 11:13: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 my question is: how do I preview the front camera with the new camera api?

Just a preview, not recording.

I want to use this new API because in the future I'm guessing the current Camera API will be replaced and stop working.

So I want to be prepared and just sit and watch while everyone panics. XD


回答1:


First of all, find out the id of your front camera (if it has one of course)

    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
            try {
                return manager.getCameraIdList();
            } catch (CameraAccessException e) {
                return null;
            }

Then find the faceCamera like this:

CameraCharacteristics cameraCharacteristics = manager.getCameraCharacteristics(cameraId);

        if (cameraCharacteristics == null)
            throw new NullPointerException("No camera with id " + cameraId);

        return cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT;

Lastly, you have to set the camera with that id:

CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
   try {
       characteristics = manager.getCameraCharacteristics(mCameraId);
   }  catch (CameraAccessException e) {
       e.printStackTrace();
   } 

Note, these are just tips on how to do what you wanna do. For details on how to start a preview and more, refer to: http://developer.android.com/samples/Camera2Basic/index.html




回答2:


0 for Back 1 for Front

For Back Camera, we have to do this inside openCamera method:

cameraId = manager.getCameraIdList()[0];

For Facing Front camera, we have to add this below line inside openCamera method:

cameraId = manager.getCameraIdList()[1];

I have added all the codes and screenshot here




回答3:


I used the code from the Google Camera2 API sample with some changes to get the front and back cameras working including saving the images locally and changing the shape of the TextureView by using an overlay.

I dealt with image rotation using exif interface too.

There is quite a bit of code so I'm going to post a link to my GitHub repo:

https://github.com/craigspicer/Camera2API

https://nullparams.com/camera-2-api-tutorial/




回答4:


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<CameraCharacteristics.Key<?>> 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.



来源:https://stackoverflow.com/questions/32462486/android-camera2-front-camera

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!