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
Camera2 is the latest low-level Android camera package and replaces the deprecated Camera class. Camera2 provides in-depth controls for complex use cases, but requires you to manage device-specific configurations. You can read about specific Camera2 classes and functions in the reference documentation.
PreviewView is a subclass of FrameLayout . To display the camera feed, it uses either a SurfaceView or TextureView , provides a preview surface to the camera when it's ready, tries to keep it valid as long as the camera is using it, and when released prematurely, provides a new surface if the camera is still in use.
The android. hardware. camera2 package provides an interface to individual camera devices connected to an Android device. It replaces the deprecated Camera class.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With